学习cocos2d-x之路(4)--实现Hero在各个方向移动
2013-06-04 22:31
567 查看
为了实现在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);
}
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学习日志(15) --公告栏的实现(文字左右移动)
- cocos2d-x 主角向任意方向使其同速移动的实现方法
- 学习cocos2d-x之路(5)--实现屏幕滚动
- JQuery移动页面开发之屏幕方向改变与滚屏的实现
- Cocos2d-x学习之实现类似android的菜单栏tabbar效果
- cocos2d-x-3.0学习笔记之如何实现地图的卷动
- 移动app接口编程技术-学习实现之PHP进阶 数组
- (跨平台)cocos2d-x C++ or Object-C(前端)调用C# webservices(后台),实现交叉编译到Android/IOS/WinPhone等移动终端设备
- Cocos2d-x学习(十一):用cocos2d-x实现MoonWarriors(游戏场景切换和主菜单实现)
- 《移动终端用户交互工程》研究生课小结一:如何在计算机方向开设一门女生积极学习的课
- 原创]unity3D学习【功能实现】之一:坐标的转换以及物体的移动
- Cocos2d-x学习(七):cocos2d-x中ScrollView的简单实现
- 【Unity快速实现小功能】动画系统学习小技巧篇(二)——摄像机跟随角色移动
- Cocos2d-x学习(十二):用cocos2d-x实现MoonWarriors(无限背景滚动的简单实现)
- cocos2d-x实例学习(6)之CCRotateTo和CCRotateBy实现变形
- cocos2d-x学习笔记(三)让精灵按照自己设定的运动轨迹行动(曲线移动)。(以椭圆轨迹为例)。
- 学习COCOS2D-x 精灵动画 实现一个《热血传奇》开门效果
- DBUtils学习之——使用ResultSetHandler接口的各个实现类实现数据库的增删改查
- cocos2d-x 2.X demo学习笔记 6 ----ClickAndMoveTest 触摸事件和移动
- cocos2d-x-3.2塔防游戏开发1:背景,地图,obj,产生移动NPC的实现