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

上一篇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");//长按
	//使用蓄力招数
}     

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...