[Cocos2D-X] a glimpse of the path (1) make a dynamic genie

Source: Internet
Author: User

Principle: Cocos2D has a director controlling the entire game process. The director adds the scene to the screen, and there are various actors in the scene. Show an image to see the Cocos2D game process: AppDelegate. cpp [cpp] bool AppDelegate: Pull () {// initialize the Director CCDirector * pDirector = CCDirector: sharedDirector (); pDirector-> setOpenGLView (CCEGLView: Export dopenglview ()); // enable FPS pDirector-> setDisplayStats (true); // set the default FPS value is 1.0/60 if you don't call this pDirector-> setAnimationInterval (1.0/60 ); // create a scenario CCScene * pScene = GameScence: scene (); // run the scenario pDirector-> runWithScene (pScene); return true ;} the above Code adds a scenario GameScence. The specific implementation is as follows: GameScence. h [cpp] # include "cocos2d. h "# include" Box2D/Box2D. h "class GameScence: public cocos2d: CCLayer {public: bool init (); // You must override scene () static cocos2d: CCScene * scene (); // equivalent to the create function, which is to override the create FUNCTION CREATE_FUNC (GameScence);}; GameScence in CCLayer. cpp [cpp] # include "GameScene. h "using namespace cocos2d; CCScene * GameScence: scene () {CCScene * scene = NULL; do {scene = CCScene: create (); GameScence * gameScene = GameScence :: create (); scene-> addChild (gameScene);} while (0); return scene;}; bool GameScence: init () {bool bRet = false; do {// create an genie CCSprite * pSprite = CCSprite: create ("bg.png") from the image; // obtain the screen size CCSize size = CCDirector: shareddire () -> getWinSize (); // set the position of the Sprite in the scene. The coordinates are from the lower left corner of the page (0, 0 pSprite-> setPosition (ccp (size. width/2, size. height/2); // Add the genie to the scene. this-> addChild (pSprite, 0);} while (0); bRet = true; return bRet ;}; if there are comments in the code, we will not repeat them. The following is: how can we display a dynamic Genie? The modified init () [cpp] bool GameScene is shown below :: init () {bool bRet = false; do {CCSprite * pMap = CCSprite: create ("bg.png"); CCSize size = CCDirector: sharedDirector ()-> getWinSize (); pMap-> setPosition (ccp (size. width/2, size. height/2); this-> addChild (pMap, 0); CCSprite * sprite; CCArray * pSpriteArray = CCArray: createWithCapacity (4); for (int I = 0; I <4; I ++) {// capture frame CCSpriteFrame * pFrame = CCSpriteFrame: create ("role.png", CCRectMake (I *, 62 )); pSpriteArray-> addObject (pFrame); // Add the sprite to the scenario. The default value is the first frame image if (I = 0) {sprite = CCSprite: createWithSpriteFrame (pFrame ); sprite-> setPosition (ccp (200,100); addChild (sprite) ;}// create an animation from the array CCAnimation * splitAnimation = CCAnimation: createWithSpriteFrames (pSpriteArray, 0.1f ); sprite-> runAction (CCRepeatForever: create (CCAnimate: create (splitAnimation) ;}while (0); bRet = true; return bRet ;}; to make dynamic images, You need to map them directly:

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.