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 ...
随机推荐
- SAL-9 获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date='9999-01-01'
题目描述 获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date='9999-01-01'CREATE TABLE `dept_man ...
- PE文件 01 导入表
0x01 导入表结构 数据目录表中的第二个成员标记了导入表的RVA和Size大小,由此可以定位到导入表: typedef struct _IMAGE_DATA_DIRECTORY { DWORD ...
- 【模板】AC自动机(简单版)
我:“woc...AC自动机?” 我:“可以自动AC???” 然鹅... 大佬:“傻...” 我:“(⊙_⊙)?” 大佬:“缺...” 我:“......” (大佬...卒 | 逃...) emm.. ...
- Log4j 日志记录
关于Log4j的简单示例 <!--手动配置log4j.properties文件内容:-->1 #level:debug/info/warn/error log4j.rootLogger=O ...
- 20165326 java实验二
2017-2018-2 20165326实验二<Java面向对象程序设计>实验报告 课程:Java程序设计 班级:1653班 姓名:陈卓 学号:20165326 成绩:指导教师:娄嘉鹏 实 ...
- php优秀框架codeigniter学习系列——异常和错误处理机制
这篇介绍下CI框架的异常和错误处理机制. 在入口文件index.php中,根据设置的环境参数设置error_reporting的范围,和是否显示错误. 在CI初始化程序CodeIgniter.php中 ...
- IE6以下版本对元素width和height的处理与CSS标准的兼容性问题
1.CSS使用width和height定义元素框的内容,Windows平台的IE6以下版本浏览器并未按照标准处理而是使用width和height来定义可见元素框的尺寸,IE/win使用width来描述 ...
- 压缩后的数据 要经过 base64_encode 后才能在网络上传送
function ob_gzip($content) // $content 就是要压缩的页面内容{ if(!headers_sent() && // 如果页面头部信息还没有输出 ex ...
- php 安装过程 第二次探索
由于第一次安装过程写的比较乱,再做整理 1.phpstudy 才是正确姿势. http://phpstudy.php.cn/ 官网下载. phpstrom 中配置 php为 phpstudy目录下 ...
- foreach遍历数组、数组的转置与方阵的迹
public class Copy1 { public static void main(String[] args) { array1(); //如果不初始化元素,默认为0 int [][] a = ...