cocos2d-x游戏开发 跑酷(三) 人物跑动
2014-05-22 14:59
267 查看
原创,转载请注明出处: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
[cpp] view
plaincopyprint?
//
// 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函数,这两个函数还是保持与引擎一致,创建的对象也是自动释放对象。本节没有加入物理世界的实现,只是实现一个跑动的人物。
[cpp] view
plaincopyprint?
//
// 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版的稍稍有点区别。
由于命名问题,我将资源文件又重新命名了一下
[cpp] view
plaincopyprint?
//
// 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函数,加入人物,变成下面这样:
[cpp] view
plaincopyprint?
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;
}
好了,跑起来了
好吧,终于要跑起来了。要实现跑酷需要用到帧动画,什么是帧动画,不解释行么。
介绍一个将小图打包的工具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
[cpp] view
plaincopyprint?
//
// 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函数,这两个函数还是保持与引擎一致,创建的对象也是自动释放对象。本节没有加入物理世界的实现,只是实现一个跑动的人物。
[cpp] view
plaincopyprint?
//
// 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版的稍稍有点区别。
由于命名问题,我将资源文件又重新命名了一下
[cpp] view
plaincopyprint?
//
// 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函数,加入人物,变成下面这样:
[cpp] view
plaincopyprint?
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游戏开发 跑酷(三) 人物跑动
- cocos2d-x游戏开发 跑酷(三) 人物跑动
- 【cocos2d-x IOS游戏开发-城市跑酷1】跑酷游戏介绍
- 【cocos2d-x IOS游戏开发-城市跑酷9】没有各种坑还叫游戏吗
- cocos2d-x游戏开发 跑酷(六) 地图循环
- 【cocos2d-x IOS游戏开发-城市跑酷12】男一号上场,大家鼓掌!
- 【cocos2d-x IOS游戏开发-城市跑酷16】碰撞检测:撞墙 or 从房子上掉下来
- 【cocos2d-x IOS游戏开发-城市跑酷7】设计烟囱与烟的效果
- cocos2d-x游戏开发 跑酷(一)开始界面
- cocos2d-x游戏开发 跑酷(一)开始界面
- cocos2d-x游戏开发 跑酷(五) 跳起和下蹲动作 手势识别
- 【cocos2d-x IOS游戏开发-城市跑酷18】统计结果,主角复活Try Again!!
- 【cocos2d-x IOS游戏开发-城市跑酷4】设置“道具”:云、路灯、竞技团队
- cocos2d-x游戏开发 跑酷(八) 对象管理 碰撞检测
- cocos2d-x游戏开发 跑酷(二) 物理世界
- cocos2d-x游戏开发 跑酷(四) 关联物理世界
- 【cocos2d-x IOS游戏开发-城市跑酷14】重写触屏[Touch]事件处理机制,响应玩家操作
- 【cocos2d-x IOS游戏开发-城市跑酷8】实现街区房子的摆放
- 【cocos2d-x IOS游戏开发-城市跑酷3】读取资源,设置背景层
- 【cocos2d-x IOS游戏开发-城市跑酷15】完善积分板【CCLabelBMFont】