原创,转载请注明出处:http://blog.csdn.net/dawn_moon/article/details/21245881
好吧,终于要跑起来了。要实现跑酷需要用到帧动画,什么是帧动画,不解释行么。
介绍一个将小图打包的工具TexturePacker,这是一个很强大的工具,另外还有一个物理编辑器PhysicsEditor,也不错,地址:http://www.codeandweb.com。刚好上周收到工具作者给的free licences,感谢一下。有兴趣的同学可以去申请一下。
加载精灵的一个比较高效的方式是用CCSpriteFrameCache和CCSpriteBatchNode配合使用。先讲一下这个用法。
以下几个步骤:
1.将打包好的大图和生成的plist文件加载到CCSpriteFrameCache,如果这两个文件名除后缀不同其它完全一样的话,直接加载plist文件就可以了。
2.用大图生成一个CCSpriteBatchNode对象batch_node,并将其加入到当前场景addChild(batch_node)。
3.通过CCSprite::spriteWithSpriteFrame或CCSprite::spriteWithSpriteFrameName创建精灵。
4.将上一步生成的精灵通过addChild加入到bath_node即可。
后面会有详细代码
创建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__) */
// // 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; } // box2d的物理世界 this->mWorld = world; this->mBatchNode = batchNode; initAction(); initBody(); initShape(); mRunner = CCSprite::createWithSpriteFrameName(runner0); // 通过名字获取animation CCAnimation* animation = CCAnimationCache::sharedAnimationCache()->animationByName("running"); mRunner->setPosition(ccp(85, 50)); mRunner->runAction(CCRepeatForever::create(CCAnimate::create(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::sharedSpriteFrameCache()->spriteFrameByName(str)); } CCAnimation* animation = CCAnimation::createWithSpriteFrames(animateFrames, 0.1); animation->setRestoreOriginalFrame(true); // animation 命名 CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "running"); } void Runner::initBody() { } void Runner::initShape() { }
由于命名问题,我将资源文件又重新命名了一下
// // resources.h // Parkour // // Created by lerry on 14-3-13. // Copyright (c) 2014年 Goonear Co.,Ltd. All rights reserved. // #ifndef Parkour_resources_h #define Parkour_resources_h static const char backmusic[] = "background.mp3"; static const char jummpmusic[] = "jump.mp3"; static const char crouchmusic[] = "crouch.mp3"; static const char spritesheet[] = "parkour.plist"; static const char spritePacker[] = "parkour.png"; static const char runner0[] = "runner0.png"; #endif
bool PlayScene::init() { if(!CCLayer::init()){ return false; } initPhysics(); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(spritesheet); CCSpriteBatchNode* spriteBatch = CCSpriteBatchNode::create(spritePacker); this->addChild(spriteBatch); mRunner = Runner::create(mWorld, spriteBatch); this->addChild(mRunner); // 调用update函数 scheduleUpdate(); return true; }
好了,跑起来了