接上一篇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");//长按 //使用蓄力招数 }原文链接:https://www.f2er.com/cocos2dx/344699.html