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

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.

主站蜘蛛池模板: 临潭县| 南漳县| 名山县| 石林| 石城县| 潮州市| 灵川县| 正定县| 沛县| 永春县| 临澧县| 门源| 奈曼旗| 新绛县| 灵璧县| 偏关县| 辽阳市| 北辰区| 黄大仙区| 安多县| 信宜市| 宝应县| 集贤县| 澎湖县| 黎平县| 巴林右旗| 白玉县| 瑞安市| 上饶市| 九江县| 同仁县| 连城县| 五家渠市| 新津县| 尉氏县| 黎平县| 鹤庆县| 瓦房店市| 祁门县| 金堂县| 九寨沟县|