Cocos2d-x格斗小游戏(五) Attack 出拳动作

来源:互联网 发布:迅龙数据恢复软件免费 编辑:程序博客网 时间:2024/04/27 21:23

1、出拳即五种状态中的Attack,大致的流程是:

(1)加载出拳动作所需的图片,并创建一个出拳动作;

(2)响应GameBasicLayer层的触摸事件,ccTouchesBegan中调用Attack函数

(3)实现Attack函数。


2、首先,在SpritePlayer类中添加一个函数loadAttackAnimation,并在init函数中调用,用于实现流程(1):

bool SpritePlayer::loadAttackAnimation(){bool bRct = false;do {CCArray *pAttackFrames = NULL;pAttackFrames = CCArray::createWithCapacity(3);CC_BREAK_IF(! pAttackFrames);for (int i = 0; i < 3; i++){CCSpriteFrame *pSpriteFrame = NULL;pSpriteFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::createWithFormat("hero_attack_00_%02d.png", i)->getCString());pAttackFrames->addObject(pSpriteFrame); // 别忘了添加到数组中CC_BREAK_IF(! pSpriteFrame);}CCAnimation *pAttackAnimation = NULL;pAttackAnimation = CCAnimation::createWithSpriteFrames(pAttackFrames, 1.0 / 24.0);CC_BREAK_IF(! pAttackAnimation);//this->setAttackAction(CCRepeatForever::create(CCAnimate::create(pAttackAnimation))); // 注意:不是重复执行了this->setAttackAction(CCSequence::create(CCAnimate::create(pAttackAnimation), CCCallFunc::create(this, callfunc_selector(SpritePlayer::Normal)), NULL));  // 出拳动作执行一次后,变为Normal动作bRct = true;} while (0);return bRct;}


3、其次,在GameBasicLayer类中,将本层的触摸设为允许(init函数中),并响应函数ccTouchesBegan

// 开启触摸this->setTouchEnabled(true);

void GameBasicLayer::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){_spritePlayer->Attack();}

4、在SpriteActions类中实现Attack函数:

void SpriteActions::Attack(){if (_spriteState == STATE_NORMAL || _spriteState == STATE_WALK || _spriteState == STATE_ATTACK){this->stopAllActions();this->runAction(_attackAction);_spriteState = STATE_ATTACK;}}


5、这样,当用于点击窗口非虚拟方向键的区域时,就会触发ccTouchesBegan事件,然后调用Attack方法,而Attack方法即是停止当前动作(Normal、Walk或Attack),执行一次出拳动作。

    注意:当玩家精灵处于Hurt(受伤)或Dead(死亡)状态时无法出拳。


6、运行效果如下:



7、待续


0 0