Cocos2d engine V2.0

Source: Internet
Author: User

2.0 cocos2d Introduction

The original version of cocos2d was written in Python and then changed to objectivec, which was applied to the iPhone. Then someone in China changed it to C ++ version cocos2dx based on the design concept, this version can run on three platforms: iPhone, Widnows, and andriod. the latest version is 1.0.0.
The Lua script system is introduced. cocos2d is a fully open-source engine. developers can use this code without disclosing source code. cocos2d is widely used, including Zynga,
Namongmeng and other well-known game companies. cocos2d is not just a 2D graphics engine library, but also provides systems that support game logic. cocos2d provides a simple sound engine that supports playing music in MP3, wave, Ogg, and other file formats. cocos2d can be used with the physical engine box2d to achieve collision and other physical effects. the cocos2d engine provides a clear structure, with rich documentation and instances. easy to learn and use.

2.1 Basic Structure of cocos2d

Cocos2d uses the scenario tree structure to manage game objects. It divides a game into different scenarios, and the scenario can be divided into different layers. One layer can have any visible game objects.
The object can also execute action to modify its attributes. each time, a scenario runs independently, and a game process is completed by switching between different scenarios. cocos2d also uses reference counting to manage memory. Basically, all classes are derived from ccobject with reference counting mechanism. the basic framework class diagram is as follows:

A. ccapplication is responsible for platform-related initialization and message processing. It is a single-piece class.

B. ccdirector is mainly responsible for managing and switching scenarios and suspending the game. In addition, he is also responsible for initializing openes-related east and west. It is also a single-piece class.

C. ccnode, the node class, is the most important object in the game. In cocos2dk, any visible or infeasible game logic object is a node. The node has the following features:

A) is a container that can contain any number of other nodes.

B) You can execute an action (ccaction)

C) A task callback can be executed for a certain period of time.

D) Information about coordinate transformation, such as postion, scale, and Rotation

E) since any subnode can be added to a node and all nodes form a tree structure, the coordinate transformation of the parent node will affect the coordinates of the subnode in the world coordinate system, A node can have only one parent node.

D. ccaction, category, and ccation are like commands assigned to ccnode. They modify the attributes of ccnode at a specific time (or immediately), such as position, rotation, and scaling.

E. similar to the stage in PL, The ccscene scenario generally refers to the part that can run independently at a specific time point, including the visible monsters, terrain, and invisible logic scripts. for example, a specific game level, game menu, and on-site animation are all independent scenarios. the scenario in cocos2d is composed of different layers. because ccscene is a subclass of ccnode, you can use ccaction to modify its attributes.

F. A cclayer class is a layer class in a scenario. A scenario can have multiple layers, such as the HUD layer, game Object layer, background layer, and sky layer. A layer is a zone that can be drawn. you can add objects such as UI and sprite to it. in addition, the layer also receives input messages and can process input messages in the class of the layer. Like ccscene, cclayer is a subclass of ccnode and can also be used to modify its attributes.

2.2 Resource Management and File System

Resource management can be divided into two aspects: one is to manage some native (raw) resources, such as models, textures, scripts, fonts, etc. another type of resource is the game object allocated during game operation. they need
To be quickly accessed. generally, Game objects that are not referenced are not immediately released and are first cached somewhere. when loading new scenarios. will be released. in some large Rendering Systems, resource sources are divided into three phases: hard disk, memory, and video memory resources, which are managed by different policies and cache mechanisms, cocos2d does not explicitly propose the concept of resource management, but uses some cache mechanisms to cache loaded textures, frames, and animation. cocos2d also provides an autorelease mechanism for game objects allocated during runtime. During Game switching, the autorelease object will be reclaimed in memory. cocos2d adopts the reference counting mechanism. loop reference
Is a concern.

For the file system, cocos2d encapsulates a simple class ccfileutils. In addition to reading common files, this class can also read files from ZIP files. This class also reads plist files to generate a ccdictionary object.

2.3 Sprite and rendering system

The sprite of cocos2d consists of texure, frame, and animation. The main class relationships for rendering by openes are as follows:

A. Sprite can be seen as a cutting part of a 2D image at a certain time point.

B. the frame is composed of texture and a rectangle (QUAD ).

C. Texture indicates a two-dimensional texture that can be drawn.

D. Imgae corresponds to different types of images in the hard disk, such as JPG, PNG, and TGA. It knows how to read different types of images from files and generate buffered data for texture.

E. ccanimation is composed of a series of frames, similar to the film set of a movie.

F. ccanimate: animation action, which enables you to play the specified animation (ccanimation) on the sprite within the specified time)

2.3.1 texture and Image

Cctexture2d in cocos2d is used to describe a 2D texture, which knows how to draw itself to the screen. you can also set parameters such as texture filtering and anti-aliasing through this class. this class also provides an interface to create a string as a texture. the texture size of this class must be 2 ^ n. therefore, the texture size is not necessarily equal to the image size. cctexture2d:

A. Name, pointing to the texture address in the video memory

B. pixelformat: Specifies the texture format, such as rgba888 or rgb565.

C. pixelwidth, pixelheight, texture width and height

D. maxs, maxt, maximum coordinate of Texture

Now that we have a class texture rendering like cctexture2d, what else do we need the Imgae class to do? Because the image formats are diverse and different formats have different loading methods on different platforms, cocos2d uses a ccimage class to encapsulate the loading of images in different formats on different platforms. if the buffer in ccimage corresponds to the internal resource, the buffer corresponding to cctexture2d is the memory resource.

2.3.2 frame and animation

As mentioned above, the frame stores a texture and rectangle. In cocos2d, a name is usually given to the frame, which is saved in ccspriteframecache in the form of a dictionary for easy reading at any time. ccspriteframecache supports reading images from plist (similar to XML) files, generating a series of Frames Based on the files, and generating a name for each frame. plist files can be generated using many third-party tools, such as zwoptex (http://zwoptexapp.com/), texturepacker (http://code-and-web.de/texturepacker );

The ccanimation class is used to implement animation on the ccspreite object. It stores a series of frames and delays between frames. You can use ccanimationaction to play an animation on the sprite.

2.3.3 rendering process

Since cocos2d only processes 2D images, rendering uses a fixed pipeline. at that time, you can also directly use the openes function to complete some advanced special effects. rendering-related classes include cctexutre2d, cctexutreatlas, ccsprite, ccrendertexture, and ccrendertexture. in addition, ccnode also has a camera class, but generally
They are not used. Some camera features are used only when some 3D effects are implemented.

2.4 Particle System

Cocos2d supports two types of particle systems: Point (ccparticlesystempoint) and square (ccparticlequadssytem) particle systems. Quad systems support some additional features.

A. It can be rotated.

B. It can be of any size, and the point size cannot exceed 64

C. The quad-type particle system can be scaled as a whole.

D. Quad particle systems consume more memory and memory than point particle systems

Particle systems also support reading data from plist files, which can be generated using third-party tools such as particle designer (http://particledesigner.71squared.com/), and

2.5 UI and font

In cocos2d, only simple UI implementation is supported. Buttons, labels, and toggle UIS are supported. In addition, simple text input boxes (textfield) are also supported. The class relationship diagram is as follows:

A. ccmenu is derived from cclayer and can process input messages. ccmenu contains multiple ccmenuitem objects. After receiving the input message, it distributes the input message to the corresponding menuitem Based on the touch position and executes the callback function.

B. ccmenu also provides an interface for alignment menu items

C. the base class of the ccmenuitem menu item. Currently, cocos2d only derives the ccmenuitemlabel, ccmenuitemsprite, and ccmenutoggle classes from ccmenuitem. of course, you can also derive a new class to implement other menu items, such as checkbox.

 

Cocos2d supports two types of fonts, one is platform-related TTF fonts, and the other is platform-independent art fonts that can be passed through the font Editor (such as http://glyphdesigner.71squared.com /) you can define the shape and size by yourself. It is similar to Sprite and convenient to use.

2.6 simple tilemap

Cocos2d supports a tmx (tilemap XML) format map that can be generated by tilemapeditor (http://www.mapeditor.org/), tmx map has the following features

A. Supports orthogonal (orthogonal), hexagonal (hexagonal), and 45-degree isometric maps.

B. Support Object groups, objects, and attributes

Map-related classes are as follows:

A. The above class diagram is simplified. The implemented class also includes objectgroup, property, tilesetinfo, etc.

B. A tiledmap has multiple layers.

C. A layer has multiple tile (expressed by Sprite)

D. Since each tile is regarded as a Sprite object, each tile can be independent, moved, rotated, scaled, or even executed.

E. layer stores a two-dimensional array corresponding to the tileid. The corresponding tile (sprite) is created based on the tile Id only when necessary, which can save storage space, it can also improve rendering efficiency.

F. tile can be added or deleted at runtime.

G. Scheduler

2.7 game logic support

As mentioned earlier, cocos2d is not only a 2D rendering engine, but also a system that supports game logic. First, all the visible objects, both the scenario and layer are saved based on the data structure of the scenario tree. operations performed on root (generally scene) will affect the root child node. if the root node is scaled or translated, all the child nodes of the root node are scaled and
Translation. all node classes inherit an interface selectorprotocol, which has a series of callback functions that can be called back under specific conditions to complete the corresponding logical processing. among them, the most basic and important thing is
The following describes the schedule task system.

2.7.0 Schedule System

A scheule system is also called a task system or an event system. A task is a scheduled event in a time slice. in cocos2d, ccnode can create a scheduled task. the callback function is used to complete a specific logic. the related class diagram is as follows:

A. ccschedule can add the selectorprotocol interface object to the task list.

B. Each cctimer frame checks whether the corresponding task has reached the execution time point. If so, the callback function is executed.

C. ccnode is derived from selecotrprotocol and implements the callback function as needed to implement the corresponding Logic

2.7.1 action Introduction

Action is like some commands that can be executed. Normally, an action modifies some attributes of a ccnode object and allows a ccnode object to execute some actions, such as changing its location, orientation. allows a sprite to play an animation. if this action is completed within a certain period of time, we call it intervalaction. If it is executed immediately, we call it instantaction. for example, ccmoveby moves a ccnode object within a certain period of time. so it is
The subclass of intervalaction. While the cccallfunc implements the immediate execution of a shelf callback function. Therefore, it is a subclass of instantaction.

The action design adopts the combination mode and decoration mode. A series of actions can be combined to execute ccnodec objects, just like issuing a series of commands to ccnode. For example, the following code

Ccactioninterval * Move = ccmoveby: actionwithduration (0.5f, CCP (0,160 ));

Ccactioninterval * rotate = ccrotateby: actionwithduration (2,360 );

Ccactioninterval * scale = ccscaleby: actionwithduration (2, 5 );

Ccactioninterval * opacity = ccfadeout: actionwithduration (2 );

Ccactioninterval * fadein = ccfadein: actionwithduration (2 );

Ccactioninterval * scaleback = ccscaleto: actionwithduration (1, 1 );

Ccfinitetimeaction * seq0 = ccsequence: Actions (move, rotate, scale, opacity, fadein, scaleback, null );

Actor-> runaction (seq0 );

The Code is interpreted as follows:

A. The actor moves 0.5 units to y within 160 seconds,

B. Let the actor rotate 360 degrees in 2 seconds

C. Enlarge actor2 seconds by 5 times

D. Fade out within 2 seconds

E. Fade in within 2 seconds

F. scale to the original size within 1 second

2.8 network support

Cocos2d does not provide a dedicated network library

2.9 script support

The latest version of cocos2dx provides Lua script support. In version 1.1.0, it integrates tolua ++ to export all class member functions to the script. in addition, some script callback functions are added. For example, the menu message processing can be completed by executing a script. you can also add the script function for the scheduled task to be called back and forth. although cocos2d has a simple example to illustrate how to use Lua, the specific efficiency remains to be further verified.

2.10 sound engine support

Cocos2d uses a simple sound engine that supports playing audio files in WAV, MP3, Ogg, and CAF formats. However, this engine does not provide volume settings.

2.11 physical engine support

Cocos2d can be combined with box2d and chipmunk physical engines to simulate collision, gravity, and other physical effects. there are a lot of examples of code that can be used in combination with box2d and chipmunk for learning and research.

2.12 disadvantages of cocos2d

Cocs2d not only processes 2D rendering, but also tightly coupled the entire rendering system with the game logic. Therefore, coco2d rendering has to use its entire logic-related code.
It is difficult to design a logical framework while using its rendering library. small games can be used for fast iteration and development. but for some large games, in order to use the system to run more stably, the structure is clearer. it is easier to expand. Generally, each module is independent. to facilitate the development and maintenance of different personnel division.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.