[Cpp]
<Pre name = "code" class = "cpp"> start with the game and enter the menu options to briefly introduce the use of the four concepts mentioned in the next section </pre>
<Pre> </pre>
<Pre name = "code" class = "cpp"> Director) </pre> The following methods are used in the initial stage: <br>
ReplaceScene <br>
PopScene <br>
PushScene <br>
RunwithScene <br>
<Br>
Scenes. Then this object is returned for Director to operate. <Br>
Generally, you can see its location <br>
+ (CCScene *) scene <br>
{<Br>
CCScene * scene = [CCScene node]; // The init is actually executed and released automatically. <br>
CCLayer * layer = [MenuLayer node]; <br>
[Scene addChild: layer]; <br>
Return scene; <br>
} <Br>
A scene can contain multiple layers. <Br>
Layers: as I said in the book, almost 99% of the workload is writing this thing. <Br>
You can create various seed layers and Sprites on the layer. To put it bluntly, they are all nodes. <Br>
CCMenu * menu = [CCMenu menuWithItems: item1, item2, nil]; <br>
Menu. tag = 100; <br>
CGSize winSize = [[CCDirector shareddire] winSize]; <br>
<Br>
Menu. position = CGPointMake (winSize. width/2, winSize. height); <br>
[Self addChild: menu]; <br>
A menu layer is simply put on the layer. <Br>
<Br>
Sprites: Sprite can understand the node with the smallest unit (of course, it is only a self-concept) <br>
<P> what is the main character of the game? It can all be regarded as Sprites. </p>
<P> <br>
</P>
<P> cocoaChina has a more official explanation of these four concepts: <a href = "http://www.cocoachina.com/gamedev/gameengine/2010/0419/1116.html"> http://www.cocoachina.com/gamedev/gameengine/2010/0419/1116.html </a> </p>
<P> I think it is well written and understandable </p>
<P> <br>
</P>
<P> Well, after understanding the above four most important elements, we started coding and applied them. </p>
<P> our first helloword sent greetings to my family by the way. </p>
<P> </p>
<Pre name = "code" class = "cpp">-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
// Create the main window
Window _ = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds];
CCGLView * glView = [CCGLView viewWithFrame: [window _ bounds]
PixelFormat: kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8
DepthFormat: 0 // GL_DEPTH_COMPONENT24_OES
PreserveBackbuffer: NO
Sharegroup: nil
MultiSampling: NO
NumberOfSamples: 0];
Director _ = (CCDirectorIOS *) [CCDirector shareddire];
Director _. wantsFullScreenLayout = YES;
[Director _ setDisplayStats: YES];
[Director _ setAnimationInterval: 1.0/60];
[Director _ setView: glView];
[Director _ setDelegate: self];
[Director _ setProjection: kCCDirectorProjection2D];
// [Director setProjection: kCCDirectorProjection3D];
If (! [Director _ enableRetinaDisplay: YES])
CCLOG (@ "Retina Display Not supported ");
NavController _ = [[UINavigationController alloc] initWithRootViewController: director _];
NavController _. navigationBarHidden = YES;
[Window _ addSubview: navController _. view];
[Window _ makeKeyAndVisible];
[CCTexture2D setDefaultAlphaPixelFormat: kCCTexture2DPixelFormat_RGBA8888];
CCFileUtils * sharedFileUtils = [CCFileUtils sharedFileUtils];
[SharedFileUtils setEnableFallbackSuffixes: NO]; // Default: NO. No fallback suffixes are going to be used
[SharedFileUtils setiPhoneRetinaDisplaySuffix: @ "-hd"]; // Default on iPhone RetinaDisplay is "-hd"
[SharedFileUtils setiPadSuffix: @ "-ipad"]; // Default on iPad is "ipad"
[SharedFileUtils setiPadRetinaDisplaySuffix: @ "-ipadhd"]; // Default on iPad RetinaDisplay is "-ipadhd"
[CCTexture2D PVRImagesHavePremultipliedAlpha: YES];
// All the above are automatically generated by the template, that is, some global parameters are configured.
// Start a scenario
[Director _ runWithScene: [MenuLayer scene];
Return YES;
} </Pre> <br>
<P> </p>
<P> the above Code is basically generated by the template itself, which is related to some settings. For the sake of conciseness, I have deleted all comments. You can see the comments after creating them. </P>
<P> This is also the first impression of Director, the Director, the props for the whole play, the actors, the lights, the audios, and so on. </P>
<P> then, runWithScene starts in the first scenario. </P>
<P> the first scenario here is the menu selection scenario. </P>
<P> </p>
<Pre name = "code" class = "cpp"> @ implementation MenuLayer
+ (CCScene *) scene
{
CCScene * scene = [CCScene node]; // actually, init is executed and automatically released.
CCLayer * layer = [MenuLayer node];
[Scene addChild: layer];
Return scene;
}
-(Void) playNewGame
{
[[CCDirector shareddire] replaceScene: [HelloWorldLayer scene];
}
-(Void) test
{
CCNode * node = [self getchildbytag: 100];
NSAssert ([node isKindOfClass: [CCMenu class], @ "error ");
// Create an item using text
CCMenuItemFont * item1 = [CCMenuItemFont itemWithString: @ "Quit game" target: self selector: @ selector (playNewGame)];
[Node addChild: item1];
[Node alignItemsVerticallyWithPadding: 10];
}
-(Id) init
{
Self = [super init];
If (self ){
// Create an item using text
CCMenuItemFont * item1 = [CCMenuItemFont itemWithString: @ "Start new game" target: self selector: @ selector (playNewGame)];
// Use the genie to create an item
CCSprite * item2_nor = [CCSprite spriteWithFile: @ "icon.png"];
CCSprite * item2_sel = [CCSprite spriteWithFile: @ "icon.png"];
CCMenuItemSprite * item2 = [CCMenuItemSprite itemWithNormalSprite: item2_nor selectedSprite: item2_sel];
// Create a menu
CCMenu * menu = [CCMenu menuWithItems: item1, item2, nil];
Menu. tag = 100;
CGSize winSize = [[CCDirector shareddire] winSize];
Menu. position = CGPointMake (winSize. width/2, winSize. height );
[Self addChild: menu];
[Menu alignItemsVerticallyWithPadding: 20];
// Action 1
Id ac = [CCMoveTo actionWithDuration: 2 position: CGPointMake (winSize. width/2, winSize. height/2)];
CCRepeat * repe = [CCRepeat actionWithAction: ac times: 4];
// Action 2
Id bc = [CCCallFunc actionWithTarget: self selector: @ selector (test)];
CCSequence * seq = [CCSequence actions: repe, bc, nil];
[Menu runAction: seq];
}
Return self;
}
@ End </pre> <br>
From the code above, we can see that scene is called when a scenario is returned.
<P> </p>
<P> A class method. I also noted this Code. The object in + (void) scene is created by node. In fact, it is an init, but the memory management is handed over to the management pool, rather than before </p>
<P> assume that, after init, there must be a place to manually release. Cocos2d does this for convenience. </P>
<P> the following code shows that the scene scenario is an abstract concept. The only thing it does is add it to the presentation layer after instantiation. </P>
<P> subsequent basic encoding logic operations are completed on the layer. </P>
<P> let's look back at the presentation layer, that is, my menuLayer. In the scene method, we can see that it is also node instantiated. In the source code, we can see that node is actually like this: </p>
<P> </p>
<Pre name = "code" class = "cpp"> # pragma mark CCNode-Init & cleanup
+ (Id) node
{
Return [[self alloc] init] autorelease];
} </Pre> <br>
Therefore, we need to initialize things in the init method of this layer.
<P> </p>
<P> if you have a basic understanding of the scenario and layer, let's look at Sprites. </p>
<P> In init, we add the last node (menu) to the menuLayer. </p>
<P> The Code clearly shows that two button nodes are created first (one is purely represented by node, and the other is represented by Sprites) and added to the menu node, then add the menu node to the top-level menuLayer (in other words, it is just a name. Do not consider it as having two menu layers ~). </P>
<P> it's that simple. </P>
<P> <br> www.2cto.com
</P>
<P> Well, above, we are familiar with how these four concepts are presented in the entire cocos2d project. </P>
<P> In fact, the best way to learn is not to read or read, but to write one side by yourself. You can also make some simple improvements, such as providing multiple scenarios, replacing replace, push, and pop in scenarios. </P>
<P> <br>
</P>
<P> PS: 1. In the first piece of code, we can see that the template has a navigation, because the helloword generated by the template itself jumps to the gameCenter (or gameKit ~~) of the system ~~). It is just the form of this example. </P>
<P> <span style = "white-space: pre"> </span> 2. in fact, I have never noticed that the file name and the class name in the file can be different (of course, I have written the class before, but I did not pay attention to it ). </P>
<P> <br>
</P>
<P> because we know that a scenario can contain multiple layers, we can have files to make it more intuitive. For example, the menus starting from the game above are used as scenarios. </P>
<P> the file is named MenuScene, and the class name in the file is named MenuLayer. This is more in line with our conventional approach. </P>
<P> <br>
</P>
<P> <span style = "white-space: pre"> </span> </p>
Author: Nono_Love_Lilith