Game Development Reference
In-Depth Information
How to do it...
Now your project template is ready for a physics-based mini game. We need to update the
game template project to get started with code game logic. Take the following steps to in-
tegrate the basic physics object in the game.
1. Open the file GameScene.m .This class creates a scene that will be plugged into
the games. Remove all code from this class and just add the following function:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
SKSpriteNode* background = [SKSpriteNode
spriteNodeWithImageNamed:@"bg.png"];
background.position =
CGPointMake(self.frame.size.width/2,
self.frame.size.height/2);
[self addChild:background];
}
}
This initWithSize method creates an blank scene of the specified size. The
code written inside the init function allows you to add the background image at
the center of the screen in your game.
2. Now when you compile and run the code, you will observe that the background
image is not placed correctly on the scene. To resolve this, open GameViewCon-
troller.m . Remove all code from this file and add the following function;
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
if (!skView.scene) {
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
GameScene * scene = [GameScene
sceneWithSize:skView.bounds.size];
Search WWH ::




Custom Search