加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x 3.3 动作游戏连续普通攻击判断-改进

发布时间:2020-12-14 20:11:47 所属栏目:百科 来源:网络整理
导读:接上一篇Cocos2d-x 3.3 动作游戏连续普通攻击判断 改进一下代码,实现了顺序播放动画效果,但是无法做到在播放动画途中按下攻击键,实现"预读"下一个动作效果 void ContButton::update(float dt){if(this-isTouch) {if(touchflag){touchflag=false;if(touchCoun

接上一篇Cocos2d-x 3.3 动作游戏连续普通攻击判断

改进一下代码,实现了顺序播放动画效果,但是无法做到在播放动画途中按下攻击键,实现"预读"下一个动作效果

void ContButton::update(float dt)
{
	if(this->isTouch) 
	{
		if(touchflag)
		{	
			touchflag=false;
			if(touchCounts==0)touchcounts=1;
			if(touchCounts==1)touchcounts=2;
			if(touchCounts==2)touchcounts=0;
		}

	}
	if(m_pHero->getNomalAttackA()->isDone()||m_pHero->getNomalAttackB()->isDone()||m_pHero->getNomalAttackC()->isDone())
	{touchflag=true;CCLOG("--------->%d",touchCounts);}
	if(touchcounts!=touchCounts)
	{
		if(touchcounts==1)onSingleCLick();
		if(touchcounts==2)onDoubleClick();
		if(touchcounts==0)onThreeClick();
		touchCounts=touchcounts;
	}
}

改进2

void ContButton::update(float dt)
{
	if(this->isTouch) 
	{
		if(touchflag)
		{	
			touchflag=false;
			if(touchCounts==0)touchcounts=1;
			if(touchCounts==1)touchcounts=2;
			if(touchCounts==2)touchcounts=0;
			this->unschedule(schedule_selector(ContButton::updateAttackDelay));
			CCLOG("--------->%d",touchCounts);
		}
		isTouch=false;
	}
	if(m_pHero->getNomalAttackA()->isDone()||m_pHero->getNomalAttackB()->isDone()||m_pHero->getNomalAttackC()->isDone())
	{
		touchflag=true;
	}
	if(touchcounts!=touchCounts)
	{
		if(touchcounts==1)onSingleCLick();
		if(touchcounts==2)onDoubleClick();
		if(touchcounts==0)onThreeClick();
		touchCounts=touchcounts;
		this->scheduleOnce(schedule_selector(ContButton::updateAttackDelay),0.8f);
	}
}
再改,加入蓄力系统,完全实现效果,但是纯过程编程.希望继续优化
void ContButton::update(float dt)
{
	if(touchcounts!=touchCounts)
	{
		if(touchflag)
		{
			if(touchcounts==1)onSingleCLick();
			touchflag=false;
		}
		if(m_pHero->getNomalAttackA()->isDone())
		{
			if(touchcounts==2)onDoubleClick();
		}
		if(m_pHero->getNomalAttackB()->isDone())
		{
			if(touchcounts==0)onThreeClick();
		}
		if(m_pHero->getNomalAttackC()->isDone())
		{	
			touchflag=true;
		}
	}
}
void ContButton::touchBegan()
{
	isTouch=true;//按钮按下
	this->schedule(schedule_selector(ContButton::updatelongprogress),1);//蓄力时间判断
	this->unschedule(schedule_selector(ContButton::updateAttackDelay));//取消连击数变0延时
	//做连击判断
	if(touchCounts==0)touchcounts=1;
	if(touchCounts==1)touchcounts=2;
	if(touchCounts==2)touchcounts=0;
	CCLOG("--------->%d",touchCounts);
}
void ContButton::touchEnded()
{
	isTouch=false;
	pressTimes=0;
	this->unschedule(schedule_selector(ContButton::updatelongprogress));
	//如果刚完成长按事件 则把按下次数清零 长按状态置空 直接返回 不继续执行
	if (m_longProgress ) 
	{
		touchCounts=0;
		m_longProgress=false;
		onLongPressed();
		return;
	}
	else
	{
		//判断是否为蓄力状态,是的话切换默认站立状态.蓄力失败
	}

}
void ContButton::onSingleCLick()
{
	CCLOG("signle");//1连击
	m_pHero->runNomalAttackA();
	touchCounts=touchcounts;
	this->scheduleOnce(schedule_selector(ContButton::updateAttackDelay),0.8f);
	//touchCounts++;
}      
void ContButton::onDoubleClick()
{
	CCLOG("double"); //2连击
	m_pHero->runNomalAttackB();
	touchCounts=touchcounts;
	this->scheduleOnce(schedule_selector(ContButton::updateAttackDelay),0.5f);
}      
void ContButton::onThreeClick()
{
	CCLOG("three"); //3连击
	m_pHero->runNomalAttackC();
	touchCounts=touchcounts;

}       
void ContButton::updateAttackDelay(float ft)
{
	touchCounts=0; 
	touchcounts=0;
	touchflag=true;
}
void ContButton::updatelongprogress(float ft)
{
	if (isTouch) 
	{
		pressTimes++;
		if(pressTimes>0.5)
		{
			//放个蓄力动画什么的
			//状态切换为蓄力状态
			//连击延时计数最高也是0.5,所以不会冲突

			if (pressTimes >= 2) 
			{
				m_longProgress=true;
			}
		}
	}
	else
	{
		pressTimes=0;
	}
}
void ContButton::onLongPressed()
{
	CCLOG("preassed");//长按
	//使用蓄力招数
}     

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读