- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- Siddharth Shekar
- 417字
- 2021-07-16 09:43:44
Getting access to MainScene
When you launch the application, the scene created in SpriteBuilder will load up by default. We will have to make some minor changes to get access to the MainScene
file and load this as default instead.
Getting ready
As of now, both the MainScene.h
and MainScene.m
files have nothing in them. So, open them up and add the following code in them.
How to do it…
First, open up MainScene.h
and add the following highlighted code:
@interface MainScene :CCNode{ CGSizewinSize; } +(CCScene*)scene; @end
Next, in the MainScene.m
file, add the following:
#import "MainScene.h @implementation MainScene +(CCScene*)scene{ return[[self alloc]init]; } -(id)init{ if(self = [super init]){ winSize = [[CCDirectorsharedDirector]viewSize]; } return self; } @end
Then, navigate to the AppDelegate.m
file, which is in the iOS group under Source/Platforms
, as shown in the following screenshot:

Change the code in the startScene
function as highlighted here:
- (CCScene*) startScene
{
//Comment or delete line below as shown
//return [CCBReaderloadAsScene:@"MainScene"];
//add below line instead
return [MainScene scene];
}
Now, we have a complete blank project to work with.
If you build and run the project now, you will see nothing but a black screen. To make sure that we are actually ready to draw something and that it will get displayed onscreen, let's add some basic code to change the background color.
Add the following code to the init
function of the MainScene.m
file:
-(id)init{ if(self = [super init]){ winSize = [[CCDirectorsharedDirector]viewSize]; CGPoint center = CGPointMake(winSize.width/2, winSize.height/2); //Background CCNode* backgroundColorNode = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.0f green:1.0 blue:0.0]]; [selfaddChild:backgroundColorNode]; } return self; }
In the init
function, after initializing the super init
file, we will first get the screen size of the current device. Then, we will have a helper CGPoint
variable, which is used to calculate the center of the screen.
Then, we will create a new CCNode
and call it backgroundColorNode
and call the CCNodeColor
class and the nodeWithColor
function. In it, we will pass the red, green, and blue values. As I wanted a green background, I have the value of green as 1
and the rest are 0
.
Then, we will add backgroundColorNode
to the scene.
How it works…
Run the project to see the changes.
You will just see a green screen, which is the node that was just added to the scene. This can be used if you want to have a plain background, and instead of importing an image into the project, this is a quick way of having any colored background.

- 軟件安全技術(shù)
- Linux C/C++服務(wù)器開(kāi)發(fā)實(shí)踐
- JavaScript+jQuery網(wǎng)頁(yè)特效設(shè)計(jì)任務(wù)驅(qū)動(dòng)教程(第2版)
- Three.js開(kāi)發(fā)指南:基于WebGL和HTML5在網(wǎng)頁(yè)上渲染3D圖形和動(dòng)畫(huà)(原書(shū)第3版)
- Python從菜鳥(niǎo)到高手(第2版)
- Getting Started with Python Data Analysis
- C#程序設(shè)計(jì)
- 機(jī)器學(xué)習(xí)與R語(yǔ)言實(shí)戰(zhàn)
- HTML 5與CSS 3權(quán)威指南(第3版·上冊(cè))
- JBoss:Developer's Guide
- Getting Started with Nano Server
- HTML+CSS+JavaScript編程入門(mén)指南(全2冊(cè))
- FPGA嵌入式項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)
- Android Development Tools for Eclipse
- Java程序設(shè)計(jì)實(shí)用教程(第2版)