Cocos2d-x Game Development

Source: Internet
Author: User

Original, reprinted please indicate the source: http://blog.csdn.net/dawn_moon/article/details/21245881

Okay, it's finally about to run. Frame Animation is required for running the game. What is Frame Animation? Do not explain it.


Introduction to a small graph packaging tool TexturePacker, this is a very powerful tool, there is also a physical editor PhysicsEditor, also good, address: http://www.codeandweb.com. We just received free licences from the tool author last week. Thank you. If you are interested, you can apply for it.


An efficient way to load the genie is to use CCSpriteFrameCache and CCSpriteBatchNode together. Let's talk about this usage first.

Perform the following steps:

1. Load the packaged big image and the generated plist file to CCSpriteFrameCache. If the two file names have the same suffix, load the plist file directly.

2. Generate a CCSpriteBatchNode object batch_node in the big chart and add it to the current scenario addChild (batch_node ).

3. Use CCSprite: spriteWithSpriteFrame or CCSprite: spriteWithSpriteFrameName to create the genie.

4. Add the wizard generated in the previous step to bath_node through addChild.

Detailed code will be provided later


Create Runner. cpp

////  Runner.h//  Parkour////  Created by lerry on 14-3-14.////#ifndef __Parkour__Runner__#define __Parkour__Runner__#include "cocos2d.h"#include "Box2D.h"#include "resources.h"enum RunnerState{    running,    jumping,    crouch};class Runner : public cocos2d::CCNode{    cocos2d::CCSprite* mRunner;        cocos2d::CCSize mRunningSize;        cocos2d::CCAction* mRunningAction;        cocos2d::CCSpriteBatchNode* mBatchNode;        b2World* mWorld;        RunnerState mState;    private:    void initAction();        void initBody();        void initShape();    public:    bool init(b2World* world, cocos2d::CCSpriteBatchNode* batchNode);            virtual void update(float dt);        static Runner* create(b2World* world, cocos2d::CCSpriteBatchNode* batchNode);    };#endif /* defined(__Parkour__Runner__) */

Inherit from CCnode. because there will be a physical world in the future, the create and init functions are rewritten. These two functions remain consistent with the engine, and the created objects are automatically released. This section does not join the implementation of the physical world, but is just a running character.


//// Runner. cpp // Parkour // Created by lerry on 14-3-14. //// # include "Runner. h "USING_NS_CC; Runner * Runner: create (b2World * world, CCSpriteBatchNode * batchNode) {Runner * runner = new Runner (); if (runner & runner-> init (world, batchNode) {runner-> autorelease (); return runner;} else {delete runner; runner = NULL; return NULL ;}} bool Runner: init (b2World * world, CCSpriteBatchNode * batchNode) {if (! CCNode: init () {return false;} // physical world of box2d this-> mWorld = world; this-> mBatchNode = batchNode; initAction (); initBody (); initShape (); mRunner = CCSprite: runner (runner0); // obtain animation CCAnimation * animation = CCAnimationCache: sharedAnimationCache ()-> animationByName ("running") by name "); mRunner-> setPosition (ccp (85, 50); mRunner-> runAction (CCRepeatForever: create (CCAnimate: Re Ate (animation); mBatchNode-> addChild (mRunner); return true;} void Runner: update (float dt) {} void Runner: initAction () {CCArray * animateFrames = CCArray: create (); char str [50] = {0}; for (int I = 0; I! = 8; ++ I) {sprintf (str, "runner+d.png", I); animateFrames-> addObject (CCSpriteFrameCache: extract ()-> spriteFrameByName (str ));} CCAnimation * animation = CCAnimation: animation (animateFrames, 0.1); animation-> animation (true); // The Name Of animationcache: sharedAnimationCache ()-> addAnimation (animation, "running");} void Runner: initBody () {} void Runner: initShape (){}

MWorld and mBatchNode are transmitted by PlayScene. InitAction is implemented here. The implementation of animation is slightly different from that of JS.

Due to naming issues, I renamed the resource file again.

/// Resources. h // Parkour // Created by lerry on 14-3-13. // Copyright (c) 2014 Goonear Co ., ltd. all rights reserved. // # ifndef Parkour_resources_h # define parse const char backmusic [] = "background.pdf"; static const char jummpmusic [] = "jump.mp3"; static const char crouchmusic [] = "crouch.mp3 .mp3 "; static const char spritesheet [] = "parkour. plist "; static const char spritePacker [] =" parkour.png "; static const char runner0 [] =" runner0.png "; # endif

Now you need to modify the init function of PlayerScene, add the character, and change it to the following:

Bool PlayScene: init () {if (! CCLayer: init () {return false;} initPhysics (); CCSpriteFrameCache: extract ()-> extract (spritesheet); CCSpriteBatchNode * spriteBatch = CCSpriteBatchNode :: create (spritePacker); this-> addChild (spriteBatch); mRunner = Runner: create (mWorld, spriteBatch); this-> addChild (mRunner ); // call the update function scheduleUpdate (); return true ;}


Okay, it's running.


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.