How to Duplicate a Sprite in Sprite Kit and Have Them Behave Differently

Sprite Kit, make multiple sprites obey same method

Store all your ChargedBall objects in an array. Declare a property:

@property (nonatomic, strong) NSMutableArray *enemies.

Every time you create an enemy, add it to the enemies array.

- (void)spawnChargesWithNumber:(NSInteger)number
{
ChargedBall *enemy = [[ChargedBall alloc] ...];

// Your code ...

if (!_enemies)
{
_enemies = [[NSMutableArray alloc] init];
}

[_enemies addObject:enemy];
}

Then for each ChargedBall in array update it in update method:

for(ChargedBall *enemy in self.enemies)
{
[self checkForAndResolveCollisionsForCharge:enemy forLayer:self.walls];
}

Side note:

This does not apply to this question but it's nice to know.
If you wanted to call a method of each enemy, you could do it in one line using NSArray's makeObjectsPerformSelector: method. Example:

[_enemies makeObjectsPerformSelector:@selector(selfDestruct)];

How do I make a node move in sprite kit swift

To move a Sprite in Sprite-Kit you can use SKActions.

For example:

let action = SKAction.moveByX(3, y: 2, duration: 10)
  • This will make the sprite move 3 units along the x-axis and 2 units along the y axis in 10 seconds.

If you want your sprite to move to a specific place, you can do:

let action2 = SKAction.moveTo(location: CGPoint, duration: NSTimeInterval)

Hope this helped!

Whats the best structure to use when you have many characters that all have the same behavior and animation but different Sprite images?

Sorry for late the answer, but I didn't see this until now. What you are after can fairly easily be achieved in Spritebuilder, without the need of subclassing. At least, it works well if your characters are composed of a CCNode with animated sprites. Lets say you have a ccb file set up with all the animations called JungleCharacter:

Right click the JungleCharacter file and choose "Duplicate". Now, in the new file (lets say we call it SeaCharacter) you select each sprite and in the 'Item properties' pane (on the right hand side) you can change the sprite frame. So, if you'd have a sprite frame called "JungleCharacterLeftArm.png", you'd change it to the equivalent "SeaCharacterLeftArm.png".

If it's to tiresome to do this in Spritebuilder, you could opt to do it in your favorite text editor, since ccb files are xml files. You'll find them in the "Packages/SpriteBuilder Resources.sbpack" folder (right click and select "show package contents"). If you set up your image assets in a smart way like "Jungle/LeftArm.png", "Sea/LeftArm.png" you can then do a quick find and replace, replacing "Jungle" with "Sea" (you get the idea).

Hope this helps!

Dealing with different iOS device resolutions in SpriteKit

Adding the following code will fix your problem (code is in Swift):

scene.scaleMode = SKSceneScaleMode.ResizeFill

Now if you want to know why this fixes your problem, what your problem actually is, and how to handle multiple resolutions – I suggest you continue reading.

There are three things that can impact the position of nodes in your scene.

1) Anchor Point

Make sure your scene's anchor point is set to (0,0) bottom left. By default the scene's anchor point starts at (0,0) so i'm assuming that is not causing the issue.

2) Size
Check the size of your scene. I typically make my scene size match the size of the device (i.e. iPad, iPhone 4-inch, iPhone 3.5 inch), then I place another layer in the scene for storing my nodes. This makes me able to do a scrolling effect for devices with smaller resolutions, but it depends on your game of-course. My guess is that your scene size might be set to 320, 480 which could be causing the positioning problems on your iPhone 5s.

3) Scale Mode
The scale mode has a huge effect on the positioning of nodes in your scene. Make sure you set the scale mode to something that makes sense for your game. The scale mode kicks in when your scene size does not match the size of the view. So the purpose of the scale mode is to let Sprite Kit know how to deal with this situation. My guess is that you have the scene size set to 320,480 and the scene is being scaled to match the iPhone 5 view which will cause positioning problems identical to what you described. Below are the various scale modes you can set for your scene.

SKSceneScaleMode.AspectFill

The scaling factor of each dimension is calculated and the larger of
the two is chosen. Each axis of the scene is scaled by the same
scaling factor. This guarantees that the entire area of the view is
filled, but may cause parts of the scene to be cropped.


SKSceneScaleMode.AspectFit

The scaling factor of each dimension is calculated and the smaller of
the two is chosen. Each axis of the scene is scaled by the same
scaling factor. This guarantees that the entire scene is visible, but
may require letterboxing in the view.


SKSceneScaleMode.Fill

Each axis of the scene is scaled independently so that each axis in
the scene exactly maps to the length of that axis in the view.


SKSceneScaleMode.ResizeFill

The scene is not scaled to match the view. Instead, the scene is
automatically resized so that its dimensions always matches those of
the view.


Conclusion

It looks like you want to remove the scaling of your scene, that way your positions in the scene will match the actual positions in the view. You can either set your scene's size to match the view size, in which case no scaling will take place. Or you can set your scene's scale mode to ResizeFill which will always make the scene's size match your view's size and it won't scale anything. In general I would stay away from any scaling and instead adjust the interface and the scene size to best suit each device. You may also want to add zoom and/or scrolling to allow devices with smaller resolutions to achieve the same view field.


But what if I want to scale my scene?

If however you need to scale your scene, but you still want positions to be relative to the view (i.e. You want (0,0) to be the bottom left of screen even when scene is cutoff) then see my answer here


Additional Info
See answer here for sample code showing how I layout nodes dynamically.

See answer here for more details about scaling to support multiple devices.



Related Topics



Leave a reply



Submit