Director Scene Layer and Sprite

Posted on 2013-09-14 10:59  Flex/AS Programmer  阅读(200)  评论(0编辑  收藏  举报

 

Scenes

A scene (implemented with the CCScene object) is more or less an independent piece of the app workflow. Some people may call them “screens” or “stages”. Your app can have many scenes, but only one of them is active at a given time.

For example, you could have a game with the following scenes: Intro, Menu, Level 1, Cutscene 1, Level 2, Winning cutscene, losing cutscene, High scores screen. You can think of each one of these scenes as a separate application that can be connected to other scenes with a small amount of "glue" code. For example, the intro scene might go to the menu scene when it finishes, and the scene for Level 1 might lead to cutscene 1 (if the player wins) or to the losing cutscene (if the player loses). An example of how scenes might flow in a game follows:

A cocos2d CCScene is composed of one or more CCNodes, added as children to the scene. Subclasses of CCNode, such as CCLayer and CCSprite, give the scene its appearance and behavior. Typically, you implement your screens as subclasses of CCLayer and add them to a blank instance of CCScene. Then, implement your other graphics and game objects as CCNodes and add them as children to the CCLayer you created.

Because scenes are a subclass of CCNode, they can be transformed manually or by using CCActions. See Actions for more information.

There is also a family of CCScene classes called transitions, implemented with the CCTransitionScene class. These allow you to create special transition effects when switching from one scene to another--for example, fading, sliding in from the side, and so on.

Director

The CCDirector is a shared (singleton) object that takes care of navigating between scenes. It knows which scene is currently active and allows you to change scenes by replacing the current scene or pushing a new one onto the scene stack. When you push a new scene onto the stack, the CCDirector pauses the previous scene but keeps it in memory. Later, when you pop the top scene from the stack, the paused scene resumes from its last state.

The CCDirector is also responsible for initializing OpenGL ES.

Layers

A CCLayer is a CCNode that knows how to handle touch events. Layers know how to draw themselves and may be semi-transparent, allowing players to see other layers behind them. CCLayers are very useful in defining your game's appearance and behavior, so you should expect to spend a lot of your programming time coding CCLayer subclasses that do what you need.

The CCLayer is where you define touch event handlers. By implementing a method to handle one of the touch events (ccTouchBegan, ccTouchMoved, ccTouchEnded, or ccTouchCancelled) a CCLayer can react to the player's interaction. These touch events are propagated to all the layers within a scene, from front to back, until some layer catches the event and accepts it.

While complex applications will require you to define custom CCLayer subclasses, cocos2d provides several predefined layers. Some examples include CCMenu (a simple menu layer), CCColorLayer (a layer that draws a solid color), and CCLayerMultiplex (a layer that lets you multiplex its children, activating one at a time while disabling the others).

Layers may contain any CCNode as a child, including CCSprites, CCLabels, and even other CCLayer objects. Because layers are a subclass of CCNode, they can be transformed manually or by using CCActions. See Actions for more information.

Multiple Layers Example:
 1    CCLayerGradient* layer1 = CCLayerGradient::create(ccc4(255, 0, 0, 255), ccc4(255, 0, 255, 255));
 2    layer1->setContentSize(CCSizeMake(80, 80));
 3    layer1->setPosition(ccp(50,50));
 4    addChild(layer1);
 5
 6    CCLayerGradient* layer2 = CCLayerGradient::create(ccc4(0, 0, 0, 127), ccc4(255, 255, 255, 127));
 7    layer2->setContentSize(CCSizeMake(80, 80));
 8    layer2->setPosition(ccp(100,90));
 9    addChild(layer2);
10
11    CCLayerGradient* layer3 = CCLayerGradient::create();
12    layer3->setContentSize(CCSizeMake(80, 80));
13    layer3->setPosition(ccp(150,140));
14    layer3->setStartColor(ccc3(255, 0, 0));
15    layer3->setEndColor(ccc3(255, 0, 255));
16    layer3->setStartOpacity(255);
17    layer3->setEndOpacity(255);
18    ccBlendFunc blend;
19    blend.src = GL_SRC_ALPHA;
20    blend.dst = GL_ONE_MINUS_SRC_ALPHA;
21    layer3->setBlendFunc(blend);
22    addChild(layer3);

Sprites

A cocos2d CCSprite is similar to sprites you find in other game engines. It is a 2D image that can be moved, rotated, scaled, animated, and undergo other transformations. Sprites (implemented using the CCSprite class) can have other sprites as children. When a parent is transformed, all its children are transformed as well. Because sprites are a subclass of CCNode, they can be transformed manually or by using CCActions. See Actions for more information.

References

cocos2d for iPhone:cocos2d Basic Concepts

Copyright © 2024 Flex/AS Programmer
Powered by .NET 8.0 on Kubernetes