cocos2d-x游戏开发 跑酷(三) 人物跑动
2014-05-02 17:45
295 查看
原创,转载请注明出处:/article/1495321.html
好吧,终于要跑起来了。要实现跑酷需要用到帧动画,什么是帧动画,不解释行么。
介绍一个将小图打包的工具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;
}
好了,跑起来了
相关文章推荐
- 如何解决xcode里开发cocos2dx修改lua脚本后不刷新的问题
- Cocos2d-x 2.0 按键加速处理深入分析
- 全面剖析Cocos2d游戏触摸机制 (下)
- 全面剖析Cocos2d游戏触摸机制 (上)
- cocos2d-x之怪物系统
- cocos2d-x plist文件各个属性的含义
- Cocos2d-android游戏引擎-介绍
- cocos2d-x 中XML解析与数据存储
- cocos2d-x 3.0正式版创建工程笔记
- cocos2d-x 中的pvr.ccz压缩图片的使用方法
- 如何优化Cocos2d-X游戏的内存
- Cocos2d-x Render-NewCulling
- QUICK_COCOS2DX创建新项目
- cocos2dx 3.0正式版 在mac上新建项目
- cocos3.0使用cocostudio动画帧结合地图对象键值创建精灵动画
- Cocos2d-x3.0 RenderTexture(三)
- cocos2d-x3.0 RenderTexture(二)
- cocos2d-x 两个场景间进行传参数
- cocos2dx CCControlSlider
- cocos2dx CCControlSlider