The main concepts in COCOS2DX include: Application, director, scene, layer, Sprite, animation, action. The hierarchy relationship is as follows:
Ccdirector (director)in the Cocos2d-x engine, the Ccdirector class is the organization and control core of the whole game, the game's running rules, the game's Ccscene (scene), the scene (Cclayer), the character (ccsprite), etc., are managed by ccdirector , which plays the rules of the game in order to make the scenes, scenes and tasks in the game run in an orderly fashion. Throughout the game, there is generally only one director, the beginning and end of the game, you need to call Ccdirector method to complete the game initialization or destruction work.in Cocos2d-xinis a very important class for overall management, mainly to accomplish the following tasks:
- Initialize the game and destroy the game;
- Manage Scheduling Scenarios ;
- Adjust Settings OpenGL Information ;
- get and set details about the game ;
How to use: Director::getinstance ()->methodname
Class CcdirectorInheritance diagram
Property-related
Get the current running scene, COCOS2DX can only run a scene scene* getrunningscene (); Gets the FPS value double getanimationinterval (); Set FPS value void Setanimationinterval (double dvalue); Whether FPS is a display state bool Isdisplaystats (); Sets whether to display fps void setdisplaystats (bool bdisplaystats); Gets the number of seconds per frame float getsecondsperframe (); Get Openglview glview* getopenglview (); Set Openglview void Setopenglview (Glview *pobopenglview); Determine if the bool Ispaused () is paused; Gets the number of frames that are called to the present unsigned int gettotalframes (); Determines whether the replaced scene receives the cleanup event//replace Way, and the push method does not have bool Issendcleanuptoscene (void);
Window Related
Returns the size of the OpenGL view in points of Const size& getwinsize () const; Returns the size, in pixels, of the OpenGL view sizes size getwinsizeinpixels () const;//returns the visible size of the OpenGL view in points. Size getvisiblesize () const;//returns the visible size VEC2 getvisibleorigin () const when the initialization of the OpenGL view is in point units; Uikit coordinates are converted to an OpenGL coordinate using the current layout (horizontal or vertical) of the active (multi) Touch coordinates Vec2 CONVERTTOGL (const vec2& point);//an OpenGL coordinate converted to Uikit coordinates The point consists of a window called a valid node above the dot Vec2 Converttoui (const vec2& point);
Scene Management related
The main loop that enters the director according to the given scene can only call him to run your first scene void Runwithscene (scene *scene); Pauses execution in a running scene, pushing a scene that pushes a pause on the stack. The new scene will be executed. void Pushscene (Scene *scene); POPs a scene from the queue. This scenario replaces the running scene. void Popscene (); Pop all the scenes from the queue until only the root scene is in the queue this scene will replace the running scene. void Poptorootscene ();//eject all scenes from the queue until it reaches Level.void poptoscenestacklevel (int level);//replace the running scene with a new one. Terminates a running scene. Call him only when there is a running scene. void Replacescene (Scene *scene); Ends the execution, releasing the running scene. void end ();//Pause a running scene. void pause (void); Resuming a paused scene the scheduled timer will be activated again. The "deferred execution" time will be 0 (if the game is not paused) void resume ();
Cccamera (camera)each node needs to use a camera, and when the node is scaled, rotated, and positioned, it needs to overlay the camera and let the node re-render it through the camera.
ccscene (Scene)In the game, the scene is the level. The level is composed of personas and backgrounds. In the film, the scene is a variety of scenes in the film, the various scenes are mainly composed of character activities and backgrounds. In the Cocos2d-x engine, the scene is stored in scenes that need to be rendered, task roles and menus, which can be rendered as a whole, together, destroyed, and used together by scene switching.
That is, the implementation of the game level, its management of these multiple cclayer, scene display, related business logic processing;
A scene is an interface that defines all the character layer information that is displayed
Scene is an abstract concept that is used only as a subclass of node. Scenes (scene) and node (nodes) are almost the same, unlike the scene's default anchor point at the center of the screen. Now scene has no other logic, do not need us to do too much operation and processing, more processing is basically done on the scene layer.
cclayer (layer)
A layer is a node subclass that handles the response of a player event. Unlike scenes, layers typically contain content that is rendered directly on the screen, and can accept user input events, including touch, keyboard input, and so on. We need to add sprites, text tags, or other game elements to the layer and set the properties of the game elements, such as position, direction and size, set the action of the game elements, and so on. After we have designed the layers, we just need to add the layers to the scene in order to be able to display them. There are multiple layers in a scene that can interact, and each layer displays different role information
Layer layers are used as containers on the one hand, and responses to related events ( such as touch clicks, user input, etc. ) are processed on the other. .
Class Relationships
Parent class:
- The layer class is a subclass of the node class that implements the touch-screen event broker (toucheventsdelegate) protocol.
Sub-class:
- Layercolor is a subclass of layer, which implements the Rgbaprotocol protocol.
- Layergradient is a subclass of Layercolor, which draws a gradient effect on the background. Layergradient it implements all the functions of the Layercolor class and adds the following new features: Gradient direction, gradient final color, interpolation mode.
- The Layermultiplex class is a scene layer class that enables its subclasses to be reused.
Common methods
Initializes the function of virtual bool init (); Allocates memory, by default creates a full-screen black scene layer static layer *create (void); Layer enters callback virtual void OnEnter (); Layer exit callback virtual void OnExit (); If the layer enters the accompanying conversion effect, the effect is completed with the callback virtual void onentertransitiondidfinish (); Touch Event (single point) virtual bool Ontouchbegan (Touch *touch, event *unused_event); virtual void ontouchmoved (Touch *touch, Event *unused_event); virtual void ontouchended (Touch *touch, Event *unused_event); virtual void ontouchcancelled (Touch *touch, Event *unused_event),//Touch event (single point and multipoint) virtual void Ontouchesbegan (const std:: vector<touch*>& touches, Event *unused_event); virtual void ontouchesmoved (const std::vector<touch*> & Touches, event *unused_event); virtual void ontouchesended (const std::vector<touch*>& touches, event * unused_event); virtual void ontouchescancelled (const std::vector<touch*>&touches, event *unused_event);// Register for touch distribution, default is addstandarddelegate virtual void registerwithtouchdispatcher (void); Determines whether the touch-screen event turns on bool Istouchenabled (); Turn the touch on or offScreen event void settouchenabled (bool value); Set/get touch mode void Settouchmode (touch::D ispatchmode mode) Touch::D ispatchmode gettouchmode ();//swallowstouches in touchscreen events Event. The default is True.void setswallowstouches (bool swallowstouches); bool Isswallowstouches () const; Set/Get whether to receive keyboard events bool Iskeypadenabled (); void setkeypadenabled (bool value); Returns the key and menu key callbacks that need to be set to receive the keyboard event void keybackclicked (void); void keymenuclicked (void);
Ccsprite (ELF)The character (Ccsprite) wizard is the main object of the whole game development process, including the protagonist and the enemy, NPC and so on, even a random floating cloud or bird is technically, is also a genie, because the genie in the Cocos2d-x, is a can change the picture, these changes include position change , rotate, zoom in and out, and move.
SpriteClass inheritance Relationships
The genie can actually say that the game is the main protagonist, the game we can manipulate the characters, pets waiting. The sprite we see is essentially a static two-dimensional diagram. Inside the Cocos2d-x, the sprite is represented by a class of sprites, which can be defined by a picture or a rectangular part of a picture.
Wizard CreationThe wizard provides several ways to create, as follows
Create a non-textured sprite that you can call the SetTexture method to set the texture after creation. More ... static sprite* create ();//Specify the image file name creation spritestatic sprite* Create (const std::string& filename);// Specify image file name and Rect parameter create Sprite static sprite* Create (const std::string& filename, const rect& Rect);// Specifies that the Texture2d texture object creates a sprite static sprite* createwithtexture (texture2d *texture);//Specify Texture2d Texture object and rect parameter to create a sprite Static sprite* createwithtexture (texture2d *texture, const rect& Rect, bool rotated=false);//Specify Sprite frame (spriteframe Create Spritestatic sprite* createwithspriteframe (spriteframe *spriteframe);//Specify Sprite frame name to create Spritestatic Sprite* Createwithspriteframename (const std::string& spriteframename);
Initialize related
Initializes an uninitialized empty sprite with virtual bool init (void); Specifies texture2d texture initialization Sprite, sprite size is the texture size virtual bool Initwithtexture (cctexture2d *ptexture); Specifies the texture2d texture object and the rect parameter to initialize the sprite virtual bool Initwithtexture (cctexture2d *ptexture, const ccrect& rect); Specifies the texture2d texture object, the rect parameter, and whether to rotate the initialization sprite virtual bool Initwithtexture (cctexture2d *ptexture, const ccrect& RECT, bool rotated); Specifies that the texture (texture) and rect parameters in Spriteframe initialization sprite,spriteframe will be used for this spritevirtual bool Initwithspriteframe ( Ccspriteframe *pspriteframe); Specifies the sprite frame name initialization spritevirtual bool Initwithspriteframename (const char *pszspriteframename); Specifies the image file initialization sprite virtual bool Initwithfile (const char *pszfilename); Specifies the image file name and rect parameter initialization of spritevirtual bool Initwithfile (const char *pszfilename, const ccrect& rect);
Property-related
Sets the scale of the node (x) virtual void Setscalex (float fscalex); Sets the scale of the node (y) virtual void Setscaley (float Fscaley); Sets the scale of the node (x, y) virtual void Setscale (float scaleX, float scaleY);//sets the position of the node in its parent coordinate system virtual void setposition (const vec2& Amp POS); virtual void setposition (float x, float y);//Set the rotation (angle) angle of the node virtual void setrotation (float rotation);// Sets the rotation angle of the x-axis of the node, which is expressed as horizontal rotation inclined to virtual void setrotationskewx (float rotationX);//sets the rotation angle of the y-axis node, which is represented as vertical rotation tilt virtual void Setrotationskewy (float RotationY);//Change the angle of the x-axis of the node, the unit is the degree of virtual void setskewx (float sx); Change the tilt angle of the y-axis of the node, in degrees virtual void setskewy (float sy); Deleting a child from the container, depending on the cleanup parameter also clears all active actions of virtual void removechild (node*, bool cleanup); Removing all children from the container, depending on the cleanup parameter also clears all active actions of virtual void Removeallchildrenwithcleanup (bool cleanup); Reorder a child, set a new value for z-axis virtual void Reorderchild (Node *child, int zOrder); Before painting, arrange all the child arrays once, rather than sorting each time you add or delete child nodes. This method can greatly improve the performance of virtual void Sortallchildren (); Add a child node to the container in virtual void AddChild (node *child); virtual void AddchilD (Node *child, int zOrder, int tag); virtual void AddChild (Node *child, int zOrder, int tag); Set a few zoom (x, y, z) virtual void Setscale (float fscale); Set the "z" axis coordinate system for the position, which is OPNEGL z fixed value virtual void Setvertexz (float fvertexz);
Ccaction (Action)Each character can implement motion, rotation, gradient, etc. through Ccaction
[COCOS2DX]COCOS2DX Main concept