• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
wwkaven
帮助1

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_d.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、待续

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhiaeike
系列文章
更多 icon
同类精品
更多 icon
继续加载