[Cocos2dx] main concepts of cocos2dx

Source: Internet
Author: User

[Cocos2dx] main concepts of cocos2dx

The main concepts in Cocos2dx include application, Director, scene, layer, Genie, animation, and action. The hierarchies are as follows:

 

CCDirector (Director) in the cocos2d-x engine, CCDirector class is the core of the entire game organization and control, game operation rules, in-game CCScene (scene), set (CCLayer) the activities of the role (CCSprite) are all managed by ccctor ctor. In the game, it specifies the game rules to enable orderly operation of the scene, scenario, and task in the game. In the entire game, there is usually only one director. at the beginning and end of the game, you must call the CCDirector method to initialize or destroy the game. in Cocos2d-x is a very important class for overall management, mainly to complete the following work:

 

Initialize and destroy games; manage scheduling scenarios; adjust and set OpenGL information; Obtain and set game details;

 

Usage: Director: getInstance ()-> MethodName

 

Class CCDirector inheritance relationship diagram

 

Attribute-related
// Obtain the currently running scenario. cocos2dx can run only one scenario Scene * getRunningScene (); // obtain the fps value double getAnimationInterval (); // set the fps value void setAnimationInterval (double dValue); // whether the fps is in the displayed status bool isDisplayStats (); // set whether to display the fps void setDisplayStats (bool bDisplayStats ); // get float getSecondsPerFrame () per frame second; // get openGLView GLView * getOpenGLView (); // set openGLView void setOpenGLView (GLView * pobOpenGLView ); // determine whether to suspend bool isPaused (); // obtain the number of all frames currently called unsigned int getTotalFrames (); // determine whether the cleanup event is received in the replaced scenario // replace mode, while bool isSendCleanupToScene (void) is not implemented in the push mode );
Window related
// Returns the Size const Size & getWinSize () const of the openGL view in points; // returns the Size of the openGL view in pixels. getWinSizeInPixels () const; // returns the visible size of the OpenGL view in points. size getVisibleSize () const; // returns the visible Size of Vec2 getVisibleOrigin () const during initialization of OpenGL views in points; // convert the coordinates of UIKit to an OpenGL coordinate using the current layout (horizontal or vertical) valid (multiple) touch coordinate Vec2 convertToGL (const Vec2 & point ); // a window composed of OpenGL coordinates converted to the coordinate points of UIKit calls the point Vec2 convertToUI (const Vec2 & point) above the valid node );
Scenario Management
// Enter Director's Main Loop Based on the given scenario and can only call it to run your first scenario void runWithScene (Scene * scene); // pause execution in a running scenario, pushing is a scenario where the stack is paused. The new scenario will be executed. void pushScene (Scene * scene); // a scenario will pop up from the queue. This scenario replaces the running scenario. void popScene (); // all scenarios are popped up from the queue until only the root scenario replaces the running scenario in the queue. void popToRootScene (); // pop up all scenarios from the queue until it reaches the level. void popToSceneStackLevel (int level); // replace a running scenario with a new one. Terminate a running scenario. Call the. void replaceScene (Scene * scene) when there is only one running scenario; // end the execution and release the running scenario. Void end (); // pause a running scenario. Void pause (void); // The scheduled timer in the resume pause scenario will be activated again. The "deferred execution" time will be 0 (if the game is not paused) void resume ();
Each node of CCCamera uses a camera. When the node scales, rotates, and positions change, the camera needs to be overwritten so that the node can be re-rendered through the camera.
In the game, the scenario is the level. The level consists of the role and background. In a movie, scenes are various scenes in the movie. Various scenes are mainly composed of activities and backgrounds of characters. In the Cocos2d-x engine, the scene is stored in the scene that needs to be rendered, the task role and menu, it can be used as a whole, rendering together, destroying together, and switching together.
That is, the implementer of the game level, which manages multiple cclayers for scenario presentation and related business logic processing;
A scenario is an interface that defines all the role layer information displayed.
Scene is an abstract concept and is used only as a subclass of a Node. Scene and Node are almost the same. The difference is that Scene's default anchor is located in the center of the screen. Currently, scene does not have any other logic. We do not need to perform too many operations or operations. More operations are basically completed on the set layer.
CCLayer)

Layer is a Node subclass that processes player event responses. Different from the scenario, a layer usually contains content directly displayed on the screen and can accept user input events, including touch and keyboard input. We need to add Sprite, text tag, or other game elements to the layer, and set the attributes of game elements, such as location, direction, and size, and set the action of game elements. After we have designed the layers, we only need to add the layers to the scene in order to display them. There are multiple layers that can interact in a scenario. Different role information is displayed on each layer.

 

The Layer is used as a container and responds to related events (such as touch clicks, user input, etc ...).

Class relationship

Parent class:

The Layer class is a subclass of the Node class. It implements the TouchEventsDelegate protocol. Subclass:
LayerColor is a subclass of Layer, which implements the RGBAProtocol protocol. LayerGradient is a subclass of LayerColor. It draws gradient effects on the background. LayerGradient implements all functions of the LayerColor class and adds the following new features: Gradient Direction, gradient final color, and interpolation mode. The LayerMultiplex class is a set layer class that can reuse its child classes. Common Methods
// Initialize the function virtual bool init (); // allocate memory. By default, a full-screen black set Layer static Layer * create (void) is created ); // The layer enters the callback virtual void onEnter (); // The layer exits the callback virtual void onExit (); // If the layer enters the adjoint conversion effect, call back virtual void onEnterTransitionDidFinish () when the effect is complete; // 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 multiple points) virtual void onTouchesBegan (const std: vector
 
  
& Touches, Event * unused_event); virtual void onTouchesMoved (const std: vector
  
   
& Touches, Event * unused_event); virtual void onTouchesEnded (const std: vector
   
    
& Touches, Event * unused_event); virtual void onTouchesCancelled (const std: vector
    
     
& Touches, Event * unused_event); // registers for touch distribution. The default value is addStandardDelegate virtual void registerWithTouchDispatcher (void); // determines whether the touch screen Event enables bool isTouchEnabled (); // enable or disable the Touch screen event void setTouchEnabled (bool value); // set/obtain the Touch mode void setTouchMode (Touch: DispatchMode mode) Touch: DispatchMode getTouchMode (); // swallowsTouches event in the touch screen event. The default value is true. void setSwallowsTouches (bool swallowsTouches); bool isSwallowsTouches () const; // you can specify whether to receive the keyboard event bool isKeypadEnabled (); void setKeypadEnabled (bool value ); // callback of the Return key and menu key. You must set void keyBackClicked (void) and void keyMenuClicked (void) to receive Keyboard Events );
    
   
  
 
The CCSprite is the main object of game development and processing, including the main character and enemy, the NPC, and even a random cloud or bird. Technically speaking, it is also the genie, because the genie in the cocos2d-x is a picture that can be constantly changing, these changes include location change, rotation, zoom in and out and motion.
Sprite class inheritance relationship

 

The genie is actually the main character of the game. We can operate on the characters and pets waiting in the game. The genie we see is essentially a static two-dimensional graph. In the Cocos2d-x, the Sprite class is used to represent the Sprite, which can be defined by a picture or a rectangular part of an image.

The wizard creation wizard provides multiple creation methods as follows:
// Create a texture-free Sprite. You can call the setTexture method to set the texture after the creation. More... static Sprite * create (); // specify the image file name to create Spritestatic Sprite * create (const std: string & filename ); // specify the image file name and Rect parameter to create Sprite static Sprite * create (const std: string & filename, const Rect & rect ); // specify the Texture2D texture object to create the Sprite static Sprite * createWithTexture (Texture2D * texture); // specify the Texture2D texture object and Rect parameter to create the Sprite static Sprite * createWithTexture (Texture2D * texture, const Rect & rect, bool rotated = false); // specify Sprite frame (SpriteFrame) to create Spritestatic Sprite * createWithSpriteFrame (SpriteFrame * spriteFrame ); // specify the Sprite frame name to create Spritestatic Sprite * createWithSpriteFrameName (const std: string & spriteFrameName );
Initialization
// Perform virtual bool init (void) initialization on uninitialized empty Sprite; // specify the Texture2D texture to initialize Sprite. The Sprite size is the virtual bool initWithTexture (CCTexture2D * pTexture ); // specify the Texture2D texture object and Rect parameter to initialize Sprite virtual bool initWithTexture (CCTexture2D * pTexture, const CCRect & rect ); // specify Texture2D texture object, Rect parameters, and whether to rotate Sprite virtual bool initWithTexture (CCTexture2D * pTexture, const CCRect & rect, bool rotated); // specify SpriteFrame to initialize Sprite, the texture (texture) and Rect parameters in SpriteFrame will be used for this Spritevirtual bool initWithSpriteFrame (CCSpriteFrame * pSpriteFrame); // specify the Sprite frame name to initialize Spritevirtual bool transform (const char * numeric ); // specify the image file to initialize Sprite virtual bool initWithFile (const char * pszFilename); // specify the image file name and Rect parameter to initialize Spritevirtual bool initWithFile (const char * pszFilename, const CCRect & rect );
Attribute-related
// Set the node Scale (x) virtual void setScaleX (float fScaleX); // set the node Scale (y) virtual void setScaleY (float fScaleY ); // set the node Scale (x, y) virtual void setScale (float scaleX, float scaleY ); // set the position of a node in its parent coordinate system. virtual void setPosition (const Vec2 & pos); virtual void setPosition (float x, float y ); // set the rotation angle of the node virtual void setRotation (float rotation); // set the rotation angle of the X axis of the node, which is displayed as horizontal rotation and tilting virtual void setRotationSkewX (float rotationX ); // Set the Rotation Angle of the Y-axis node to vertical rotation and tilt virtual void setRotationSkewY (float rotationY); // change the tilt angle of the X axis of the node, in the unit of virtual void setSkewX (float sx ); // change the y-axis tilt angle of the node, in the unit of virtual void setSkewY (float sy); // delete a child from the container, depending on the cleanup parameter, all activities are cleared at the same time. virtual void removeChild (Node * child, bool cleanup); // delete all children from the container, depending on the cleanup parameter, all activities are cleared at the same time. virtual void removeAllChildrenWithCleanup (bool cleanup); // sorts a child again and sets a new Z axis value. virtual void reorder Child (Node * child, int zOrder); // sort all Child arrays once before painting, instead of sorting each time a child Node is added or deleted. This method can greatly improve the performance of virtual void sortAllChildren (); // Add a subnode to the container virtual void addChild (Node * Child); virtual void addChild (Node * child, int zOrder, int tag); virtual void addChild (Node * Child, int zOrder, int tag); // you can specify the zOrder (x, y, z) virtual void setScale (float fScale); // sets the position's "z" axis coordinate system, which is the OpneGL Z fixed point value virtual void setVertexZ (float fVertexZ );
Each role can use CCAction to implement motion, rotation, and gradient.

 

Related Article

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.