CCSprite in Cocos2d-X

Source: Internet
Author: User
Tags emscripten

CCSprite in Cocos2d-X
Zookeeper

In cocos2d-x, the genie CCSprite is the main object of game development and processing, such as the plane in the sky, the tanks on the ground, the task of player control and so on are all genie. Even a white cloud float randomly in the sky and a bird flying over are also genie. In fact, the genie is an image that can change constantly, including position change, rotation, amplification, and motion.

It can be seen that the genie is a very important component in the game and can be seen everywhere. In the cocos2d-x, to use the image display, basically will use the genie class, CCSprite class in the cocos2d-x class structure such


To observe the CCSprite function and enable the CCSprite. h file, the CCSprite function and implementation method are described through code in the CCSprite. h file.

# Ifndef _ SPITE_NODE_CCSPRITE_H __# define _ SPITE_NODE_CCSPRITE_H __# include "base_nodes/CCNode. h "# include" CCProtocols. h "# include" textures/CCTextureAtlas. h "# include" ccTypes. h "# include" cocoa/CCDictionary. h "# include
 
  
# Ifdef EMSCRIPTEN # include "base_nodes/CCGLBufferedNode. h "# endif // EMSCRIPTENNS_CC_BEGINclass CCSpriteBatchNode; class CCSpriteFrame; class CCAnimation; class CCRect; class CCPoint; class CCSize; class CCTexture2D; struct transformValues _; # define CCSpriteIndexNotInitialized 0 xffffffff // CCSprite invalid index on the CCSpriteBatchNodeclass CC_DLL CCSprite: public CCNodeRGBA, public CCTextureProtocol # ifd Ef EMSCRIPTEN, public CCGLBufferedNode # endif // EMSCRIPTEN {public: // create an empty genie static CCSprite * create (); // create the genie static CCSprite * create (const char * pszFileName) through the file; // create the genie static CCSprite * create (const char * pszFileName, const CCRect & rect); // create the sprite static CCSprite * createWithTexture (CCTexture2D * pTexture) through textures and rectangles; // create the sprite static CCSprite * createWithTexture (CCTexture2D * pTexture, const CC Rect & rect); // create the genie static CCSprite * createWithSpriteFrame (CCSpriteFrame * pSpriteFrame) through the sprite frame ); // create the sprite static CCSprite * createWithSpriteFrameName (const char * pszSpriteFrameName) through the sprframe and rectangle; // const char * pszSpriteFrameName (void); // destructor virtual ~ CCSprite (void); // initialize the instance virtual bool init (void); // initialize the texture virtual bool initWithTexture (CCTexture2D * pTexture ); // initialize the texture and rectangle virtual bool initWithTexture (CCTexture2D * pTexture, const CCRect & rect); // initialize the texture, rectangle, and torsion virtual bool initWithTexture (CCTexture2D * pTexture, const CCRect & rect, bool rotated); // initialize the virtual bool initWithSpriteFrame (CCSpriteFrame * pSpriteFrame); // initialize the virtual bool frame (const char * pszSpriteFrameName ); // initialize the virtual bool initWithFile (const char * pszFilename); // initialize the file and the rectangular virtual bool initWithFile (const char * pszFilename, const CCRect & rect ); // set the texture virtual void setTexture (CCTexture2D * texture); // obtain the texture virtual CCTexture2D * getTexture (void); // set the inline void setBlendFunc (ccBlendFunc blendFunc) {m_sBlendFunc = blendFunc;} // obtain the inline ccBlendFunc getBlendFunc (void) {return m_sBlendFunc;} // set the sprite ratio to virtual void setScaleX (float fScaleX ); virtual void setScaleY (float fScaleY); // sets the sprite's coordinate virtual void setPosition (const CCPoint & pos); // rotate the sprite. The angle is in the Unit and the positive value is clockwise, the negative number is virtual void setRotation (float fRotation); virtual void setRotationX (float fRotationX); virtual void setRotationY (float fRotationY); // the Sprite is distorted, and the single vector is almost rotated, however, virtual void setSkewX (float sx); virtual void setSkewY (float sy); // destroy virtual void removeChild (CCNode * pChild, bool bCleanup ); // Delete the current child node from the parent node. If Cleanup is true, all actions of the current node are deleted. virtual void removeAllChildrenWithCleanup (bool bCleanup ); // reset zOrder virtual void reorderChild (CCNode * pChild, int zOrder) of the genie; // Add virtual void addChild (CCNode * pChild) of the genie; virtual void addChild (CCNode * pChild, int zOrder); virtual void addChild (CCNode * pChild, int zOrder, int tag); // rearrange all genie virtual void sortAllChildren (); // sets the sprite ratio virtual void setScale (float fScale); // sets VertexZ (VertexZ and Zorder all represent the rendering order) virtual void setVertexZ (float fVertexZ ); // sets the coordinate virtual void setAnchorPoint (const CCPoint & anchor) of the stroke. // determines whether to ignore the virtual void ignoreAnchorPointForPosition (bool value ); // set whether the Sprite is visible virtual void setVisible (bool bVisible); // draw virtual void draw (void); // set the color virtual void setColor (const ccColor3B & color3 ); // update the display color virtual void updateDisplayedColor (const ccColor3B & parentColor); // set the transparency virtual void setOpacity (GLubyte opacity ); // modify the transparency of the color virtual void setOpacityModifyRGB (bool modify); virtual bool isOpacityModifyRGB (void); // update the transparency virtual void updateDisplayedOpacity (GLubyte parentOpacity ); // update the changed virtual void updateTransform (void); // obtain the virtual CCSpriteBatchNode * getBatchNode (void); // set the virtual void setBatchNode (CCSpriteBatchNode * worker) of the batch node ); // set virtual void setTextureRect (const CCRect & rect) in the rectangle where the texture is located; // set virtual void setTextureRect (const CCRect & rect, bool rotated, const CCSize & untrimmedSize); // sets the vertex rectangle virtual void setVertexRect (const CCRect & rect); // sets the display sprite Frame virtual void setDisplayFrame (CCSpriteFrame * pNewFrame ); // determine whether a virtual bool isFrameDisplayed (CCSpriteFrame * pFrame) has been created; // return the currently displayed virtual CCSpriteFrame * displayFrame (void ); // set the sprite frame name and index virtual void setDisplayFrameWithAnimationName (const char * animationName, int frameIndex); // whether the returned sprite needs to update inline virtual bool isDirty (void) {return m_bDirty;} // sets whether the genie needs to update inline virtual void setDirty (bool bDirty) {m_bDirty = bDirty ;} // returns the Tex coordinate, vertex coordinate, and color inline ccV3F_C4B_T2F_Quad getQuad (void) {return m_sQuad;} // specifies whether to rotate the inline bool isTextureRectRotated (void) {return m_bRectRotated ;} // obtain the texture index inline unsigned int getAtlasIndex (void) {return m_uAtlasIndex;} // set the texture index inline void setAtlasIndex (unsigned int uAtlasIndex) {m_uAtlasIndex = uAtlasIndex ;} // obtain the genie coordinate inline const CCRect & getTextureRect (void) {return m_obRect;} // obtain the genie reference inline CCTextureAtlas * getTextureAtlas (void) {return m_pobTextureAtlas ;} // set the Wizard to reference inline void setTextureAtlas (CCTextureAtlas * pobTextureAtlas) {cursor = cursor;} // The inline const CCPoint & getOffsetPosition (void) {return m_obOffsetPosition ;} // returns whether the sprite quickly flipped bool isFlipX (void); // sets the sprite to flip void setFlipX (bool bFlipX); // determines whether the sprite flipped bool isFlipY (void ); // set the sprite to flip void setFlipY (bool bFlipY); protected: void updateColor (void); virtual void setTextureCoords (CCRect rect); virtual void updateBlendFunc (void ); virtual void Merge (void); virtual void Merge (bool bValue); CCTextureAtlas * m_pobTextureAtlas; // specify texture atlas (weak reference) unsigned int m_uAtlasIndex; // Absolute (real) index on the SpriteSheet CCSpriteBatchNode * m_pobBatchNode; // Used batch node (weak reference) bool m_bDirty; // Whether the sprite needs to be updated bool m_bRecursiveDirty; /// Whether all of the sprite's children needs to be updated bool m_bHasChildren; // Whether the sprite contains children bool m_bShouldBeHidden; /// shocould not be drawn because one of the ancestors is not visible CCAffineTransform m_transformToBatch; // Data used when the sprite is self-rendered // ccBlendFunc m_sBlendFunc; /// It's required for your inheritance CCTexture2D * m_pobTexture; // CCTexture2D object that is used to render the sprite /// Shared data /// texture CCRect m_obRect; /// Retangle of CCTexture2D bool m_bRectRotated; // Whether the texture is rotated // Offset Position (used by Zwoptex) CCPoint m_obOffsetPosition; CCPoint cursor; // vertex coords, texture coords and color info ccV3F_C4B_T2F_Quad m_sQuad; bool m_bOpacityModifyRGB; // whether to Opacity // whether to flip bool m_bFlipX along the X axis; // whether to flip bool m_bFlipY along the Y axis }; NS_CC_END # endif // _ SPITE_NODE_CCSPRITE_H __
 


Before using Sprite, you must create a Sprite. To create a Sprite, you can view my blog.

Create genie in Cocos2d-X: http://blog.csdn.net/u010105970/article/details/39617335


After creating the genie, you can use the genie.

Play the genie: http://blog.csdn.net/u010105970/article/details/39638009


When you need to deal with a large number of genie, you need to use the batch processing genie to improve programming efficiency. For the batch processing genie method, refer to my blog

Batch Processing genie in Cocos2d-X: http://blog.csdn.net/u010105970/article/details/39899399


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.