Game Development Reference
In-Depth Information
How it works…
Before we were creating the sprite using images from App Bundle , but now we will be
using texture atlas to get the images and passing to the sprites. The preceding texture atlas
named FSGame.atlas holds multiple images of coins and spaceship in it. The internal
code loads the frames and stores them in an array.
• SpriteKit first searches for the image file, if not found it searches inside the texture
atlases build into the app bundle. If we want to work explicitly with texture atlases,
use the SKTextureAtlas class. It fetches the texture atlas by specifying its
name:
SKTextureAtlas *textureAtlas = [SKTextureAtlas
atlasNamed:@"FSGame"];
• Then we can use the atlas object to get the required image for creation of sprites.
SKTexture* spaceShipTexture = [textureAtlas
textureNamed:@"Spaceship.png"];
Now we will understand how the coins are converted into collectables. For moving the
coins, its initial and final positions are to be decided.
• The initial position in x dimensions, is fixed to the width of the frame plus half of
its coin so that it is added outside the screen and y dimensions are randomly chosen
from 0 to 320 using the arc4random() function.
CGFloat coinInitialPositionX = self.frame.size.width +
coinSprite.size.width/2;
CGFloat coinInitialPositionY = arc4random() % 320;
CGPoint coinInitialPosition =
CGPointMake(coinInitialPositionX,
coinInitialPositionY);
• For the final position, the x dimension is set to negative of half of its own width
and the y dimension is the same as the initial position x .
CGFloat coinFinalPositionX = -coinSprite.size.width/
2;
CGFloat coinFinalPositionY = coinInitialPositionY;
Search WWH ::




Custom Search