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
- //
- // 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__) */
继承CCnode,由于后面会有一个物理世界,所以重写了create函数和init函数。这两个函数还是保持与引擎一致,创建的对象也是自己主动释放对象。本节没有增加物理世界的实现,仅仅是实现一个跑动的人物。
- //
- // 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()
- {
- }
mWorld和mBatchNode会由PlayScene传进来。
这里主要实现了initAction。动画的实现与JS版的稍稍有点差别。
由于命名问题。我将资源文件又又一次命名了一下
- //
- // 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
如今须要改动PlayerScene的init函数,增加人物,变成以下这样:
- 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;
- }
好了,跑起来了
cocos2d-x游戏开发 跑酷(三) 人物跑动的更多相关文章
- iOS cocos2d 2游戏开发实战(第3版)书评
2013是游戏爆发的一年,手游用户也是飞速暴增.虽然自己不做游戏,但也是时刻了解手机应用开发的新动向.看到CSDN的"写书评得技术图书赢下载分"活动,就申请了一本<iOS c ...
- cocos2d-x游戏开发 跑酷(两) 物理世界
原创.转载请注明出处:http://blog.csdn.net/dawn_moon/article/details/21240343 泰然的跑酷用的chipmunk物理引擎.我没有细致学过这个东西. ...
- HTML5 Canvas游戏开发(三)lufylegend开源库件(上)
lufylegend可以解决HTML5开发游戏中会遇到的一些问题: 1.各种浏览器对于JavaScript和HTML的解析是不一致的. 2.手机浏览器和PC浏览器的区别. 3.JavaScript并非 ...
- cocos2d-x游戏开发 跑酷(四) 关联与物理世界
原创.转载注明出处http://blog.csdn.net/dawn_moon/article/details/21451077 前面一节尽管实现了一个跑动的人物,可是他只不过一个精灵在运行一个跑动的 ...
- cocos2d-x游戏开发 跑酷(八) 对象管理 碰撞检測
对象管理类的原理是这种: ObjectManager类是一个单例类,全局仅仅有一个对象实例存在.初始化的时候创建两个数组CCArray来保存金币和岩石.为什么要保存,由于在地图重载的时候.要销毁看不见 ...
- cocos2d-x游戏开发 跑酷(九) 源代码下载及小结
这个东西零零碎碎写了一个礼拜吧. 事实上也没多少东西在里面.文章后附下载地址 博客地址:http://blog.csdn.net/dawn_moon 由于我没用过chipmunk,并且它是面向过程的东 ...
- three.js尝试(二)模拟游戏开发:3D人物在地图上行走
本次尝试,模拟了一个小人物在场景中行走,使用简单模型建立了森林,图片纹理模拟草地,加载3D模型呈现人物,使用按键asdw模拟人物的行走,行走和站立时人物的切换等. 主要用到点:3D模型的加载,模型的动 ...
- (转载)如何学好iphone游戏开发
转自:http://www.cnblogs.com/zilongshanren/archive/2011/09/19/2181558.html 自从发布<如何学习iphone游戏开发>到 ...
- 整理了一下浅墨大神的Visual C++/DirectX 9.0c的游戏开发手记
还是非常棒的博客,只是没有一个文件夹.所以自己做了一个山寨文件夹在这里.便于随时查找. 前面31期从略. [Visual C++]游戏开发笔记三十二 浅墨DirectX提高班之中的一个 DirectX ...
随机推荐
- MyEclipse和Eclipse
Eclipse 分成3个子项目: ·平台Platform ·开发工具箱-Java Development Toolkit(JDT) ·外挂开发环境-Plug-in Development Enviro ...
- Java语法基础学习DaySeven
---恢复内容开始--- 一.包装类——Wrapper 1.定义:针对八种基本数据类型定义相应的引用类型——包装类(封装类) boolean——Boolean byte——Byte ...
- L255 Learning to say no brings a thrill of freedom
I am not sure who came up with that thing about never saying yes to something in the distant future ...
- 使用Chrome调试工具抢阿里云免费套餐
活动地址如下: https://free.aliyun.com/ntms/free/experience/getTrial.html 首先打开地址,需要登录,登陆后看到如下页面: 选择个人免费套餐,这 ...
- 2017ICPC北京赛区网络赛 Minimum(数学+线段树)
描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two types of queries: 1. ...
- Oracle账户管理
登录:conn 用户名/口令创建用户create user 用户名 identified by 口令修改密码password 用户名删除用户drop user 用户名如果要删除的用户已经创建表,那么再 ...
- 后门技术(HOOK篇)之DT_RPATH
0x01 GNU ld.so动态库搜索路径 参考材料:https://en.wikipedia.org/wiki/Rpath 下面介绍GNU ld.so加载动态库的先后顺序: LD_PRELOAD环境 ...
- Quorum算法
分布式系统中,一般保存多个数据副本,明显可以提高系统可靠性.并且存储这些数据副本的节点,不仅做容灾用,也可以提供服务,作负载均衡. 这里就涉及到一个数据一致性的问题,也就是各副本间要进行同步,来保持最 ...
- SLES12SP2使用总结
1. 设置hostname hostnamectl set-hostname hostname***
- shell脚本-预定义常量
$0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上 ...