星期三, 10月 12, 2011

cocos2d Basic Concepts

http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:basic_concepts

cocos2d Basic Concepts

There are some basic concepts introduced in this library that you will need to know when developing a cocos2d application:

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 define every one of these scenes more or less as separate apps; there is a bit of glue between them containing the logic for connecting scenes (the intro goes to the menu when interrupted or finishing, Level 1 can lead you to the cutscene 1 if finished or to the losing cutscene if you lose, etc.).

A cocos2d CCScene is composed of one or more layers (implemented with the CCLayer object), all of them piled up. Layers give the scene an appearance and behavior; the normal use case is to just make instances of Scene with the layers that you want.

There is also a family of CCScene classes called transitions (implemented with the CCTransitionScene object) which allow you to make transitions between two scenes (fade out/in, slide from a side, etc).

Since scenes are subclasses of CCNode, they can be transformed manually or by using actions. See Actions for more detail about actions.

Director

The CCDirector is the component which takes care of going back and forth between scenes.

The CCDirector is a shared (singleton) object. It knows which scene is currently active, and it handles a stack of scenes to allow things like “scene calls” (pausing a Scene and putting it on hold while another enters, and then returning to the original). The CCDirector is the one who will actually change the CCScene, after a CCLayer has asked for push, replacement or end of the current scene.

The CCDirector is also responsible for initializing OpenGL ES.

Layers

A CCLayer has a size of the whole drawable area, and knows how to draw itself. It can be semi transparent (having holes and/or partial transparency in some/all places), allowing to see other layers behind it. Layers are the ones defining appearance and behavior, so most of your programming time will be spent coding CCLayer subclasses that do what you need.

The CCLayer is where you define event handlers. Events are propagated to layers (from front to back) until some layer catches the event and accepts it. FIXME

Although some serious apps will require you to define custom CCLayer classes, cocos2d provides a library of useful predefined layers (a simple menu layer: CCMenu, a color layer: CCColorLayer, a multiplexor between other layers: CCMultiplexLayer, and more ).

Layers can contain CCSprite objects, CCLabel objects and even other CCLayer objects as children.

Since layers are subclass of CCNode, they can be transformed manually or by using actions. See Actions for more detail about actions.

Sprites

A cocos2d' sprite is like any other computer sprite. It is a 2D image that can be moved, rotated, scaled, animated, etc.

Sprites (implemented using the CCSprite class) can have other sprites as children. When a parent is transformed, all its children are transformed as well.

Since sprites are subclass of CCNode, they can be transformed manually or by using actions. See Actions for more detail about actions.

沒有留言: