cocos2d-x游戏开发 跑酷(三) 人物跑动

原创,转载请注明出处: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

  1. //  
  2. //  Runner.h  
  3. //  Parkour  
  4. //  
  5. //  Created by lerry on 14-3-14.  
  6. //  
  7. //  
  8.   
  9. #ifndef __Parkour__Runner__  
  10. #define __Parkour__Runner__  
  11.   
  12. #include "cocos2d.h"  
  13. #include "Box2D.h"  
  14. #include "resources.h"  
  15.   
  16. enum RunnerState{  
  17.     running,  
  18.     jumping,  
  19.     crouch  
  20. };  
  21.   
  22. class Runner : public cocos2d::CCNode  
  23. {  
  24.     cocos2d::CCSprite* mRunner;  
  25.       
  26.     cocos2d::CCSize mRunningSize;  
  27.       
  28.     cocos2d::CCAction* mRunningAction;  
  29.       
  30.     cocos2d::CCSpriteBatchNode* mBatchNode;  
  31.       
  32.     b2World* mWorld;  
  33.       
  34.     RunnerState mState;  
  35.       
  36. private:  
  37.     void initAction();  
  38.       
  39.     void initBody();  
  40.       
  41.     void initShape();  
  42.       
  43. public:  
  44.     bool init(b2World* world, cocos2d::CCSpriteBatchNode* batchNode);  
  45.       
  46.       
  47.     virtual void update(float dt);  
  48.       
  49.     static Runner* create(b2World* world, cocos2d::CCSpriteBatchNode* batchNode);  
  50.       
  51. };  
  52.   
  53. #endif /* defined(__Parkour__Runner__) */  

继承CCnode,因为后面会有一个物理世界,所以重写了create函数和init函数,这两个函数还是保持与引擎一致,创建的对象也是自动释放对象。本节没有加入物理世界的实现,只是实现一个跑动的人物。


  1. //  
  2. //  Runner.cpp  
  3. //  Parkour  
  4. //  
  5. //  Created by lerry on 14-3-14.  
  6. //  
  7. //  
  8.   
  9. #include "Runner.h"  
  10.   
  11.   
  12. USING_NS_CC;  
  13.   
  14. Runner* Runner::create(b2World *world, CCSpriteBatchNode* batchNode)  
  15. {  
  16.     Runner* runner = new Runner();  
  17.     if (runner && runner->init(world, batchNode)) {  
  18.         runner->autorelease();  
  19.         return runner;  
  20.     }else  
  21.     {  
  22.         delete runner;  
  23.         runner = NULL;  
  24.         return NULL;  
  25.     }  
  26. }  
  27.   
  28. bool Runner::init(b2World *world, CCSpriteBatchNode* batchNode)  
  29. {  
  30.     if (!CCNode::init()) {  
  31.         return false;  
  32.     }  
  33.     // box2d的物理世界  
  34.     this->mWorld = world;  
  35.     this->mBatchNode = batchNode;  
  36.       
  37.     initAction();  
  38.     initBody();  
  39.     initShape();  
  40.       
  41.     mRunner = CCSprite::createWithSpriteFrameName(runner0);  
  42.       
  43.     // 通过名字获取animation  
  44.     CCAnimation* animation = CCAnimationCache::sharedAnimationCache()->animationByName("running");  
  45.     mRunner->setPosition(ccp(85, 50));  
  46.     mRunner->runAction(CCRepeatForever::create(CCAnimate::create(animation)));  
  47.     mBatchNode->addChild(mRunner);  
  48.       
  49.       
  50.     return true;  
  51. }  
  52.   
  53. void Runner::update(float dt)  
  54. {  
  55.       
  56. }  
  57.   
  58.   
  59. void Runner::initAction()  
  60. {  
  61.     CCArray* animateFrames = CCArray::create();  
  62.     char str[50] = {0};  
  63.     for (int i = 0; i != 8; ++i) {  
  64.         sprintf(str, "runner%d.png", i);  
  65.         animateFrames->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str));  
  66.     }  
  67.     CCAnimation* animation = CCAnimation::createWithSpriteFrames(animateFrames, 0.1);  
  68.     animation->setRestoreOriginalFrame(true);  
  69.     // animation 命名  
  70.     CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "running");  
  71.       
  72. }  
  73.   
  74. void Runner::initBody()  
  75. {  
  76.       
  77. }  
  78.   
  79. void Runner::initShape()  
  80. {  
  81.       
  82. }  

mWorld和mBatchNode会由PlayScene传进来。这里主要实现了initAction。动画的实现与JS版的稍稍有点区别。

由于命名问题,我将资源文件又重新命名了一下

  1. //  
  2. //  resources.h  
  3. //  Parkour  
  4. //  
  5. //  Created by lerry on 14-3-13.  
  6. //  Copyright (c) 2014年 Goonear Co.,Ltd. All rights reserved.  
  7. //  
  8.   
  9. #ifndef Parkour_resources_h  
  10. #define Parkour_resources_h  
  11.   
  12. static const char backmusic[] = "background.mp3";  
  13.   
  14. static const char jummpmusic[] = "jump.mp3";  
  15.   
  16. static const char crouchmusic[] = "crouch.mp3";  
  17.   
  18. static const char spritesheet[] = "parkour.plist";  
  19.   
  20. static const char spritePacker[] = "parkour.png";  
  21.   
  22. static const char runner0[] = "runner0.png";  
  23.   
  24. #endif  

现在需要修改PlayerScene的init函数,加入人物,变成下面这样:

  1. bool PlayScene::init()  
  2. {  
  3.     if(!CCLayer::init()){  
  4.         return false;  
  5.     }  
  6.       
  7.     initPhysics();  
  8.       
  9.       
  10.     CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(spritesheet);  
  11.     CCSpriteBatchNode* spriteBatch = CCSpriteBatchNode::create(spritePacker);  
  12.     this->addChild(spriteBatch);  
  13.       
  14.     mRunner = Runner::create(mWorld, spriteBatch);  
  15.       
  16.     this->addChild(mRunner);  
  17.       
  18.     // 调用update函数  
  19.     scheduleUpdate();  
  20.       
  21.     return true;  
  22. }  


好了,跑起来了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值