The most common classes in cocos2d can be roughly divided into two parts: the entity class and the behavior class. The so-called entity class is something that is very easy to embody and we can intuitively feel, such as tables, pictures, and people, these exist in reality. Entity classes can be said to simulate these things. Of course, they are also slightly abstract, such as menus and buttons, however, in any case, we can understand their existence well. Behavior classes are very abstract, and the subjects they encapsulate are not something (or attribute), but methods, an instance of this class represents a behavior pattern of the object class. Different behavior patterns require different classes to encapsulate. From the perspective of Chinese, the entity class is the subject (WHO), the behavior class is the predicate (what), and cocos2d mostly focuses on the two.
Classes in cocos2d use tree inheritance, that is, there are only a few base classes at the top, and the extended functions are implemented by inheriting the parent class layer by layer, this facilitates collaboration between classes, because many interfaces are common (all from the base class ). The base class of the object class is ccnode (all classes in cocos2d start with CC). It is the parent class of all object classes, the parameters and Methods encapsulated by cocos2d are the most efficient in the engine. They can be said to be the core of cocos2d (menus and genie are inherited from it)
Is a node level chart.
A node hierarchy chart is a hierarchy chart consisting of all currently active cocos2d nodes, also known as the scenario inheritance relationship diagram. In addition to the scenario itself, other nodes have only one parent node, but can have any child nodes; when a node is added to another node, the node scenario diagram is built.
If a game can run normally, the top layer must be a scene, followed by a layer. The nodes at the bottom layer of the layer are the main elements in the game. Most of them are Sprite.
The role of the node level is that the impact on a node will affect all its subnodes.
For example, when a node executes a method (that is, a value assignment method) or a behavior starting with a set, the results are not limited to this node, but also all its subnodes, for example, if the setposition method is executed on node 1, nodes 1, 2, 3, 4, 5, and 6 are displaced, however, if it is the setposition executed by node 2, only nodes 2, 4, and 5 will move, and nodes 1, 3, and 6 will not be affected. Making good use of this will make development more effort-saving, and vice versa will add a lot of trouble to yourself, so you need to use some brains when designing the tree structure (of course, the author thinks this is more advantageous than disadvantages ).
Most behavior classes inherit from ccation. Its subclass instances allow a ccnode object to execute an action, such as rotating, moving, or even custom behavior, A ccnode can execute multiple behaviors at the same time or execute a behavior sequence in sequence. With these classes, developers can easily add various gorgeous effects to the game.
With ccnode and ccaction, can we develop games? Not yet. We still need a stage to take over them. For example, we create an image and assign it a moving behavior, but we cannot see it because we didn't display it on the screen, the role of the stage is to display the actors on the screen to the audience. Cocos2d implements the stage in the following way:
The stage is divided into three parts: Director-scenario-layer. First, the display of the screen is in the unit of scenario. All the content we see through the monitor is in the same scenario, and the layer is the element of the scenario, just like the scenario is a stage, we watch the world in the game through it, and the layer is the background that forms the stage, such as distance, close view, and subtitles. After a story is told, we may need to switch to another stage to tell the next story. In drama, we usually pull the curtain, switch the background behind the scenes, and then open the curtain, cocos2d also follows this step, and the director completes this task. Directors are responsible for switching between scenarios. scenes are responsible for presenting elements in the game to players, while layers are responsible for accommodating elements and receiving player input information.
The following describes some basic concepts:
What is an anchor?
The default position of the anchor is in the geometric center of the texture image.
Generally, the position attribute set for the node is relative to the anchor. Therefore, when you set the node position, the node center position is the configured coordinate position.
Of course, you can also modify the position of the anchor, ranging from (0, 0))
In cocos2d, each initialization method kicks the dog both the instance method and the class method. In fact, the class method calls the instance method to initialize the object and creates an object that automatically releases the memory. For developers, you can simply use the class method to create an object. This is actually the factory method mode.
Ccnode
Main functions:
① Add and remove sub-nodes
② Scheduled messages through the timer (schedule unschedule, etc)
③ Execute actions (runaction, stopaction, etc)
Note that the default value of the ccnode is (0, 0), that is, in the lower left corner of the node.
You can use the _ cmd keyword to stop the reservation of the current method.
During node initialization, lazy loading is used to create objects such as Sprite, label, and menu used in the game scenario, if you do not want to display the node immediately, set the visible attribute of those nodes to no, which helps improve the game performance.
Ccscene scenario
It is a subclass of ccnode and an abstract class. In fact, ccscene and ccnode are almost identical. The only difference is that the ccscene anchor is located in the geometric center of the screen by default.
The ccscene class basically does not have any logic mechanism or special functions. Generally, ccscene is built as the parent node of all other nodes and is considered as the container of the cclayer object.
To switch between different scenarios, for example, view navigation, you must switch between scenarios based on ccscene.
Cctransitionscene scenario switching supports scenario transition
Cclayer class
It is a drawn area on the screen. It serves as a container for the sprite node and other nodes, and can accept information from both touch input and accelerator input.
Cclayercolor color layer: You can set the layer of fill color.
Cclayergradient gradient layer
Ccmenu menu class: inherited from cclayer. It is a menu management screen layer. The menu object is a collection class and consists of menuitem class instances.
Ccmenuitem instances form various buttons, and ccmenu only supports ccmenuitem nodes as their subnodes
Note: ccmenuitem is a basic class and cannot be used directly to create menus. It is mainly used to set the button status and process callback methods. You must use the ccmenuitem subclass to create custom menu items.
The ccmenuitem sub-classes include:
Ccmenuitemlabel
Ccmenuitematlasfont
Ccmenuitemfont
Ccmenuitemsprite
Ccmenuitemimage
Ccmenuitemtoggle
Cctexture texture class
When the game is running, all image files are loaded as GPU [graphic processing unit, translated as "graphics processor" in Chinese] Understandable OpenGL ES textures.
Cocos2d has a built-in texture cache manager (cctexturecache) to save these textures.
Note that the length and width of an image must be at the Npower of 2 when it is filled into the texture.
Cctexture (texture)
Cctextureatlas: a texture gallery contains all the images.
Cctexturecache (texture cache): Used as a single instance for loading and managing textures
Ccsprite genie
Generally, the genie comes from the image. Once the image is loaded into the memory, it is converted into a texture image.
Ccspritebatchnode genie form, including ccsprite
Ccspriteframe
Ccspriteframecache genie frame cache (Singleton), which contains ccspriteframe
Ccdirector director class
A singleton is used to save the global configuration settings of cocos2d and manage cocos2d scenarios.
The above part content reference http://www.xuanyusong.com/archives/category/ios/cocos2d_game