您的位置:首页 > 移动开发 > Cocos引擎

学习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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐