学习cocos2d-x之路(4)--实现Hero在各个方向移动
来源:互联网 发布:ugui源码调试 编辑:程序博客网 时间:2024/06/05 17:04
为了实现在4个方向Hero的运动,创建枚举变量
typedef enum{
keyDown=0,
keyLeft=1,
keyUp=2,
keyRight=3,
}HeroDirection;
注意: 此处枚举变量的设置要按照英雄图片中的方向设置,需要定义在类声明中。
将createHeroSprite(),进行修改,声明为 cocos2d::CCAnimation* createHeroAnimation(HeroDirection direction);
声明两个成员变量:
cocos2d::CCAnimation ** walkAnimations;
cocos2d::CCSprite * heroSprite;
声明移动的回调函数:
void menuMoveCallback(CCObject * pSender);
注意: 默认创建的工程只有.cpp中 使用了 using namespace cocos2d. 如果要在类声明中使用cocos2d类,必须加 cocos2d:: 或者 添加using namespace cocos2d,否则会报一堆莫名其妙的错误,我被这个错误痛苦折磨了一个小时
函数体修改为:
cocos2d::CCAnimation* HelloWorld::createHeroAnimation(HeroDirection direction)
{
CCTexture2D *heroTex=CCTextureCache::sharedTextureCache()->addImage("hero.png");
CCSpriteFrame * frame0,*frame1,*frame2,*frame3;
frame0=CCSpriteFrame::createWithTexture(heroTex,CCRectMake(32*0,direction*32,32,32)); //通过方向控制纹理图的帧数
frame1=CCSpriteFrame::createWithTexture(heroTex,CCRectMake(32*1,direction*32,32,32));
frame2=CCSpriteFrame::createWithTexture(heroTex,CCRectMake(32*2,direction*32,32,32));
frame3=CCSpriteFrame::createWithTexture(heroTex,CCRectMake(32*3,direction*32,32,32));
CCArray * animFrames= CCArray::create();
animFrames->addObject(frame0);
animFrames->addObject(frame1);
animFrames->addObject(frame2);
animFrames->addObject(frame3);
CCAnimation * animation=CCAnimation::createWithSpriteFrames(animFrames,0.2);
animFrames->release();
// heroSprite->setPosition(ccp(32))
return animation;
}
在HelloWorld::init()中创建精灵:
walkAnimations=new CCAnimation *[4]; //创建四个方向的动画。在这里创建的动画依然不能在控制移动的回调函数中使用
walkAnimations[keyDown]=createHeroAnimation(keyDown); //暂时不清除原因
walkAnimations[keyLeft]=createHeroAnimation(keyLeft);
walkAnimations[keyRight]=createHeroAnimation(keyRight);
walkAnimations[keyUp]=createHeroAnimation(keyUp);
CCAnimationFrame * panimationFrame=
dynamic_cast<CCAnimationFrame*>(walkAnimations[keyDown]->getFrames()->objectAtIndex(0)); //获得第一帧
CCSpriteFrame * pspriteFrame=panimationFrame->getSpriteFrame(); //转化为SpriteFrame
heroSprite=CCSprite::createWithSpriteFrame(pspriteFrame); //使用第一帧创建精灵
heroSprite->setPosition(ccp(32,32));
addChild(heroSprite);
创建四个方向按钮
CCMenuItem *down=CCMenuItemFont::create("down", this, menu_selector(HelloWorld::menuMoveCallback)); //移动的回调函数
CCMenuItem *left=CCMenuItemFont::create("left", this, menu_selector(HelloWorld::menuMoveCallback));
CCMenuItem *right=CCMenuItemFont::create("right", this, menu_selector(HelloWorld::menuMoveCallback));
CCMenuItem *up=CCMenuItemFont::create("up", this, menu_selector(HelloWorld::menuMoveCallback));
down->setTag(keyDown);
left->setTag(keyLeft);
right->setTag(keyRight);
up->setTag(keyUp);
CCMenu *menu=CCMenu::create(down,left,right,up,NULL);
menu->alignItemsHorizontallyWithPadding(50);
addChild(menu);
编写移动的回调函数:
void HelloWorld::menuMoveCallback(CCObject *pSender){
CCNode *node=(CCNode *) pSender;
HeroDirection targetDirection=(HeroDirection) node->getTag();
CCAnimate * animate=CCAnimate::create(createHeroAnimation(targetDirection));
CCPoint moveByPosition;
//根据方向计算移动的距离
switch (targetDirection) {
case keyDown:
moveByPosition=ccp(0,-32);
break;
case keyLeft:
moveByPosition=ccp(-32,0);
break;
case keyRight:
moveByPosition=ccp(32,0);
break;
case keyUp:
moveByPosition=ccp(0,32);
break;
default:
break;
}
//CCMoveBy实现咋指定时间内移动指定距离
//CCSpanwn实现多个动作同时发生
CCMoveBy *moveBy=CCMoveBy::create(0.28f,moveByPosition);
CCSpawn * spawn=CCSpawn::create(animate,moveBy,NULL);
CCAction *action=CCSequence::create(spawn,NULL);
//创建不断重复的动画,并让heroSprite播放
heroSprite->runAction(action);
}
- 学习cocos2d-x之路(4)--实现Hero在各个方向移动
- cocos2d-x学习之触摸移动
- 学习cocos2d-x之路(5)--实现屏幕滚动
- 学习cocos2d-x 之路 (1)--了解cocos2d-x
- Cocos2d-X手势之简单实现(方向、模糊圆形处理)
- cocos2D-x 学习之路(一)
- cocos2D-x 学习之路(二)
- cocos2D-x 学习之路(三)
- [cocos2d-x学习之路]在mac上配置cocos2d-x开发环境
- cocos2d-x API 学习 ---- 各个gui插件
- cocos2d-x遥感控制角色移动简单实现之JoyStick
- quick-cocos2d-x(lua) 拖动精灵使其在屏幕移动的效果实现
- Cocos2d-x 随运动方向转变图片方向实现方式 (改版)
- Cocos2d-x 随运动方向转变图片方向实现方式
- cocos2d-x学习日志(15) --公告栏的实现(文字左右移动)
- [cocos2d-x学习之路]Cocos2d-x下载与安装
- cocos2d-x 学习日志(4)之cocos2d-x集成sharesdk
- cocos2D-X源码分析之从cocos2D-X学习OpenGL(4)---混合
- ios学习笔记(三)之内存管理
- mysql各存储引擎之间的区别
- 通过SQL语句或数据库,生成数据库模型PDM方法详解
- 14周项目2:抽象基类shape
- 第十四周—2.Shape类及其派生类中的纯虚函数
- 学习cocos2d-x之路(4)--实现Hero在各个方向移动
- frambuffer 节点注册过程
- 系统调用和快速系统调用
- TuneUp Utilities
- Java 基础源码 switch语句判断指定月份属于一年中的哪个季度
- 析构函数
- Javascript 两数相除保留两位小数点
- APP被苹果App Store拒绝的79个原因(未完待续)
- dispatch_once函数创建单例