Cocos2d-x Study Notes (5)

Source: Internet
Author: User
Cocos2d-x basic element Introduction: 1, CCDirector: The main manager is to control the game flow of the main components, including setting the game display window, FPS display, default Frame Rate upper limit, texture color Bit Width, switching the current game scenario, pausing or resuming the scenario. Access the site through CCDirector * pDirectorCCDirector: sharedDirector ();

Cocos2d-x basic element Introduction: 1, CCDirector: The main manager is to control the game flow of the main components, including setting the game display window, FPS display, default Frame Rate upper limit, texture color Bit Width, switching the current game scenario, pausing or resuming the scenario. Access the site through CCDirector * pDirector = CCDirector: sharedDirector ();

Cocos2d-x basic element Introduction:

1. CCDirector: General Manager

This is the main component that controls the game process, including setting the game display window, FPS display, default Frame Rate upper limit, texture color width, switching the current game scenario, pause or resume the scenario. Use CCDirector * pDirector = CCDirector: shareddire(); to access the scenario.

(1) runWithScene (CCScene * scene): starts the game and runs the scene scenario. This method is mainly used when the main program starts for the first time when it starts the home scene.

(2) replaceScene (CCScene * scene): directly replaces the current scene with the input scene to switch the screen. The current scene is released.

(3) pushScene (CCScene * scene): Pause the currently running scene and press it into the generation execution scenario stack. Then, set the passed scene to the current running scenario.

(4) popScene: releases the current scenario, then pops up the stack top scenario from the generation execution scenario stack, and sets it to the current running scenario. If the stack is empty, the application is directly ended.

(5) pause: pause all timers and actions in the current running scenario, and the scenario will still be on the screen.

(6) resume: resume the paused timer and action in the current running scenario, and use it with pause.

(7) end: end the scenario and exit the application at the same time.

2. CCScene: Scenario

The scenario does not have any special functions. It serves as a layer container and process control.

3. CCLayer: Layer

void addChild(CCNode* child);void addChild(CCNode* child,int zOrder);void addChild(CCNode* child,int zOrder,int tag);
Child is the added node. For a scenario, a node is a layer. to specify the order, different zOrder values can be used. zOrder represents the order of elements under the node, the larger the value, the closer the display order is, the default value is 0. Tag is the element identification code. If the child node sets the tag value, you can use the tag value in the parent node to find it. A very important part of CCLayer is to accept user input events, including touch, accelerometer, and keyboard input.

4. CCSprite: genie inherited from CCNode

(1) texture: a texture is an image, which is displayed by the genie. The Cocos2d-x uses the 3D drawing library OpenGL.

(2) create genie: The genie is created by texture. First, create the genie in the initialization method of the layer, set attributes, and add them to the layer. You can create genie in multiple ways. We use the factory method to create the genie:

CCSprite * fish = CCSprite: create(”fish.png "); // fish.png is the file name of the texture used by the sprite. The genie not only displays a complete texture, but also a part, as shown below:

CCSprite * smallFish = CCSprite: create(”fish.png ", CCRectMake (100,100, 100); // This line of code shows the 100 * pixel size section in the upper left corner of the texture.

(3) set the sprite attributes: parameters such as the location, direction, and scaling ratio of the genie are all Sprite attributes. First, get the screen size code:

CCSize size = CCDirector: shareddire()-> getWinSize (); // obtain the screen size. CCPoint pos = cpp (size. width/2, size. height/2); // determine the sprite position fish-> setPosition (pos); // set the sprite position
(4) Add a genie to the Layer

After setting the genie attribute, add the genie to the layer. In fact, both CCSprite and CCLayer inherit from CCNode. Adding addChild of other game elements to an element is a method included in CCNode, therefore, you can also use addChild to add the genie to the layer: this-> addChild (fish );

(5) common members

I. Initialization Method:

A. Use image files

static CCSprite* create(const char *pszFileName);static CCSprite* create(const char *pszFileName,const CCRect& rect);bool initWithFile(const char *pszFileName);bool initWithFile(const char *pszFileName,const CCRect& rect);
Rect is an optional option, used to specify the texture part of the sprite, using a coordinate system similar to cpp

B. Use CCTexture2D (texture class)

static CCSprite* create(CCtexture2D *pTexture);static CCSprite* create(CCtexture2D *pTexture,const CCRect& rect);bool initWithTexture(CCtexture2D *pTexture);bool initWithTexture(CCtexture2D *pTexture,const CCRect& rect);
The pTexture parameter of the CCTexture2D type is a texture object. You can use the addImage method of the CCTextureCache class to load the image file as a texture and return it. The rect function is similar to the previous one.
C. Create a CCSpriteFrame using the following method:

static CCSprite* create(CCSpriteFrame *pSpriteFrame);bool initWithSpriteFrame(CCSpriteFrame *pSpriteFrame);
The pSpriteFrame parameter is a texture frame, which stores a CCTexture2D application and a CCRect to represent a part of the texture.

Ii. texture-related attributes

CCTexture2D * Texture: gets or sets the Texture used by the sprite. After you use this method to set the Texture, the sprite will display a complete Texture.

CCRect TextureRect: gets or sets the texture display part. This CCRect uses texture coordinates, that is, the upper left corner is the origin.

CCSpriteBatchNode * BatchNode: gets or sets the batch node to which the genie belongs.

3. texture-related methods

Void setDisplayFrame (CCSpriteFrame * pNewFrame): sets the texture frame in the display. pNewFrame is a new texture frame, which represents the texture or texture display part, which can be different from the old frame.

CCSpriteFrame * displayFrame: Get the texture frame being displayed.

Bool isFrameDisplayed (CCSpriteFrame * pFrame): returns a value indicating whether the pFrame is a texture frame being displayed.

4. Color-related attributes

CcColor3 Color: gets or sets the Color that is superimposed on the Genie. ccColor3 consists of three Color components (red, green, and blue). The default value is pure white, indicating that the Color of the genie is not changed, if it is set to another value, it will change.

GLubyte Opacity: gets or sets the Opacity of the Genie. GLubyte is a built-in OpenGL type, indicating an unsigned 8-digit integer with a value ranging from 0 ~ 255

Bool OpacityModifyRGB: This attribute can be modified to obtain or set whether the texture data used by the sprite has been premultiplied by the Alpha channel. If the image display of the Aloha Channel is incorrect.

5. CCNode and Coordinate System
Cocos2d-x uses the scene, layer, Genie hierarchy to organize game elements, this hierarchy also corresponds to the rendering level of the game, so the game elements can be organized into a tree structure, become a rendering tree. Cocos2d-x abstracts every game element on the rendering tree into a node, that is, CCNode. All game elements inherit from CCNode and have all the features of CCNode.

CCNode defines a common feature for drawing objects, including location, scaling, visibility, and rotation angle.
(1) Coordinate System in Cocos2d-x: There are two coordinate systems

A. Drawing Coordinate System: use the same coordinate system as OpenGL, the lower left corner is the origin, the right is the positive direction of the X axis, the upward is the positive direction of the Y axis, in the Cococs2d-x, all plotting operations use the drawing coordinate system, such as Position and AnchorPoint.

B. texture coordinate system: Use the upper left corner as the origin, the right side as the positive direction of the X axis, and the downward side as the positive direction of the Y axis, for example, the TextureRect attribute of CCSprite.

(2) drawing properties:

A. CCSize ContentSize: obtains or sets the content size of this node for graphic conversion.

B. CCPoint AnchorPoint and CCPoint Position: AnchorPoint are used to set an AnchorPoint to control the node Position and transformation more accurately. The AnchorPoint's two parameters x and y are real numbers between 0 and 1, indicating the position of the AnchorPoint relative to the node's length and width. Position is used to set the node Position. Because Position refers to the coordinate value of the anchor in the parent node, the Position displayed on the node is usually related to the anchor.

C. folat Rotation: gets or sets the Rotation Angle of a node. The Node rotates clockwise for a certain amount based on its own anchor. The unit is angle, and the Rotation angle can be arbitrary.

D. float Scale (float ScaleX and float ScaleY): Scale is used to obtain or set the scaling ratio of the node and the node scales with the anchor as the center. Scale represents the overall scaling ratio, while ScaleX and ScaleY represent the zooming ratio between X and Y. By default, all three are 1.

E. bool Visible: gets or sets the node visibility. When Visible is true, the node is displayed. Its accessors are special:

Bool isVisible (); // similar to get accessors

Void setVisible (bool visible); // similar to set accessors

F. float SkewX and float SkewY: obtain or set the oblique angle. Nodes take the anchor as the center, and perform deformation at a certain angle in the parallel X axis or Y axis. SkewX is the clockwise deformation of the parallel X axis, and SkewY is the clockwise deformation of the parallel Y axis, in the unit of angle. The default value is 0.

G and int tags: obtain or set the label of a node, quickly locate the desired node from all nodes of the node, and locate the subnode. The tags cannot be the same. Related methods include getChildByTag and removeChildByTag.

H. void UserData: obtains or sets additional information related to nodes. Use the void * attribute to save any data.

Other attributes: CCArray * Children, CCNode * Parent, CCCamera * Camera, CCGridBase * Grid, CCGLProgram * ShaderProgram, CCActionManager * ActionManager, CCScheduler * Scheduler, etc.

6. node organization:

Organization node-related methods
Method Description
AddChild (CCNode * child) ADD child to the current node
RemoveFromParentAndCleanup (bool cleanup) Remove the current node from its parent node. If cleanup is true, run the clean method.
RemoveChild (CCNode * child, bool cleanup) Remove the child node from the current node. If cleanup is true, the child's clean method is called.
RemoveChildByTag (int tag, bool cleanup) Remove a tag-based node from the current node
RemoveAllChildWithCleanup (bool cleanup) Remove all child nodes of the current node
GetChildByTag (int tag) Returns the tag-based node of the current node.
Cleanup Stop all actions and timers on this node
7. timer event: the timer is a tool that continuously triggers game time at a certain interval, and the timer is a tool for game dynamic change.

(1) update Timer: this mechanism is the update method of the CCNode refresh event. This method is triggered once before each frame is drawn. By default, the update event is not enabled for CCNode. You need to call the scheduleUpdate method and reload update to execute your own code. Stop the timer using the unscheduwhitptage method.

(2) schedule timer: The schedule method provided by CCNode. It can call a function at a certain interval. The interval must be greater than two frames.

8. Other events: Cocos2d-x also provides us with some other events related to process control.

(1) onEnter: This method is called when the scenario of this node is about to be presented.

(2) onEnterTransitionDidFinish (): This method is called after the entry action in the scenario where the node is located ends. If no entry action is performed in the scenario, this method is called immediately after onEnter ().

(3) onExit (): This method is called when the node is about to exit.

(4) onExitTransitionDidStart (): This method is called after the scene action of the node ends. If the scene does not show an action, this method is followed by onExit () and then called.

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.