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

Adding a MainMenu Scene

To create the main menu scene, first create a new project and follow the steps in Chapter 1, Sprites and Animations, to load the MainScene file upon loading the game. Also, don't forget to make changes to the CCBReader.m file and change the folder search parameter.

Getting ready

For this step, no additional steps are required. Later in the chapter, we will discuss how to make more complex scenes with the custom init function.

For now, we will use MainScene as the MainMenu scene and start populating it with text, buttons, and functions to transition between the scenes.

How to do it…

As of now, we have a MainMenu.h file that looks similar to the following:

@interface MainScene : CCNode

+(CCScene*)scene;

@end

We also have a MainScene.m file, as follows:

#import "MainScene.h"

@implementation MainScene

+(CCScene*)scene{

  return[[self alloc]init];

}

-(id)init{

    if(self = [super init]){

      winSize = [[CCDirector sharedDirector]viewSize];

    }

    return  self;
}

@end

There is nothing new here. We can add the background image as we did in the first chapter in the init function to get the scene to look better. We will add the following code to the init function:

-(id)init{

  if(self = [super init]){

    winSize = [[CCDirector sharedDirector]viewSize];

 CCSprite* backgroundImage = 
 [CCSprite spriteWithImageNamed:@"Bg.png"];

 backgroundImage.position = 
 CGPointMake(winSize.width/2,
 winSize.height/2);

 [self addChild:backgroundImage];

  }

  return  self;
}

How it works…

Build and run it to make sure that there are no errors because we will use this file as the main menu scene.

After running the scene, you will see the background image that you had learned to add in Chapter 1, Sprites and Animations.

Scenes are building blocks for creating interfaces for any games. A scene may contain any number of sprites, text labels, menus, and so on in any combination, and it is up to the developer's needs to populate a scene with these elements.

This is just to show how scenes are created and how the basic game scene looks. Later in the chapter, we will discuss how to customize our scenes.

Now, we will add text labels, buttons, and menus, and start populating our main menu scene next.

主站蜘蛛池模板: 汉沽区| 巴林右旗| 峡江县| 炉霍县| 潞城市| 德化县| 当涂县| 永寿县| 永和县| 登封市| 壤塘县| 满洲里市| 辉县市| 化州市| 阿图什市| 渑池县| 合作市| 通河县| 邢台县| 运城市| 泰来县| 喀喇沁旗| 鄂尔多斯市| 孙吴县| 镇巴县| 蕉岭县| 阿拉善右旗| 双江| 仁怀市| 全南县| 宁河县| 冷水江市| 曲松县| 平远县| 乌恰县| 祁门县| 镇巴县| 岱山县| 宜良县| 公主岭市| 潮安县|