官术网_书友最值得收藏!

Learning the basics of SpriteKit – The FlyingSpaceship tutorial

In this section we will learn and explore basic concepts of SpriteKit. We will also develop a mini game, which will help in understanding the concepts with some robust implementation. The best way to learn SpriteKit is to see it in action.

Getting ready

To build a SpriteKit game, firstly you need to understand the basic structure of a SpriteKit project. You can get started with a starter project having a SKScene and an SKNode placed on it. This would equip you with the setup to build your basic game.

How to do it...

To understand the basic concepts of game programming, let's create a new project with the SpriteKit game template with project name FlyingSpaceship. The project will demonstrate the structure of a SpriteKit project. The end goal of the project is that a Spaceship is visible on the screen and in the upcoming topics we can make it fly.

We will follow some of the same steps we performed in Chapter 1, iOS Game Development and finally add the Spaceship to the screen:

  1. Start your Xcode and navigate to File | New | Project. Then from the prompt window navigate to iOS | Application | SpriteKit Game and click on Next.
    How to do it...
  2. Fill all the project details in the prompt window and provide FlyingSpaceship as the project name with your Organization Name, Devices as iPhone, and Class Prefix as FS. Click on Next as shown in the following screenshot:
    How to do it...
  3. Select a location on drive to save the project and click on Create.
  4. As a result, the FSViewController and FSMyScene files will be created in the project having a Spaceship.png file also. The project directory should look something similar to the following screenshot:
    How to do it...
  5. Go to the General tab, uncheck Portrait from the device orientation so that the final orientation is landscape.
  6. Cut the code of typecasting UIView to SKView and presenting FSMyScene to SKView from (void)viewDidLoad of FSViewController.
  7. Implement - (void)viewWillLayoutSubviews and copy the code from viewDidLoad to viewWillLayoutSubviews.
  8. Finally, the code will look like this:
    How to do it...
  9. Now, let's go to FSMyScene.m, remove the default code added in the init method and also the method for touch detection.
  10. Make a property for a SKSpriteNode called spaceship in the private interface:
    @interface FSMyScene()
    @property (nonatomic, strong) SKSpriteNode*     spaceShipSprite;
    @end
  11. Add this spaceShipSprite to the FSMyScene file in its init method:
    self.spaceShipSprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
    self.spaceShipSprite.position = CGPointMake(self.spaceShipSprite.size.width, size.height/2);        
    [self addChild:self.spaceShipSprite];

    The default Spaceship.png provided is appropriate, so delete and add Spaceship.png provided in the Resources folder of the Starter kit.

  12. Now if you run the app, the spaceship doesn't look good on a black background, so give the sky color background color to FSMyScene file in its init method.
    self.backgroundColor = [UIColor colorWithRed:135.0/255.0 green:206.0/255.0 blue:235.0/255.0 alpha:1.0];

    So finally we have reached the goal and have placed a spaceship in the sky.

    The final FSMyScene class looks like this:

    How to do it...

In the preceding screenshot, you will observe an update: method in the .m file. This method is automatically invoked while rendering each frame on the screen. If the frame rate of the game is 60, then this method will be executed 60 times in a second. Any real-time calculations can be performed in this method, so actions such as calculating the player's location in real time can be handled in this method.

And the starter kit game, FlyingSpaceship, looks like this:

How to do it...

How it works...

The structure of SpriteKit is fundamentally derived and inherited from the UIKit framework. The operating system offers a smooth transition from UIKit to SpriteKit by just typecasting the UIKit view controller's view to a SpriteKit view called SKView. After this, you are ready to play with the SpriteKit stuff. As shown in the following block diagram, create a scene, add some nodes (that is Sprites as players, background stuff, and so on) to it and you have the game environment built. You can also make the environment more live by applying some actions (rotate, move, scale, and many more) to the nodes added.

Hence, in combination, this scene has different kinds of nodes with some actions applied, that make the basic structure of your SpriteKit and also the game you thought of building.

How it works...

There's more...

SpriteKit can be used for game development on iOS and OS X platforms. The available graphics hardware of the hosting device is used to render composite 2D images at high frame rates. There are several other features of SpriteKit, which support the following kinds of content, including:

  • Sprites that can be of any form such as untextured or textured rectangles
  • Text
  • Arbitrary CGPath-based shapes
  • Video

If you are curious to know more, then visit Apple's developer link https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Introduction/Introduction.html.

You can also play around with the sample you have just created by trying to change the location of the spaceship and applying various colors to its background.

主站蜘蛛池模板: 泌阳县| 广饶县| 景泰县| 抚宁县| 黎城县| 阜南县| 昔阳县| 百色市| 长乐市| 乐亭县| 呼玛县| 沅陵县| 漾濞| 河北区| 深水埗区| 普兰店市| 淳化县| 洪湖市| 鄂托克前旗| 盐亭县| 周宁县| 呼图壁县| 南宫市| 宝丰县| 西乡县| 枣阳市| 永胜县| 时尚| 南充市| 那坡县| 定日县| 北辰区| 呼伦贝尔市| 阜城县| 伊宁县| 泽州县| 仁寿县| 哈尔滨市| 绥化市| 个旧市| 紫云|