新手刚接触游戏编程,cocos2d-x做一个简单的横版战斗游戏Demo卡在普通攻击判断上。
花了一些时间和别人讨论理清逻辑写了这个代码。应该还有很多优化的地方。
发出来与大家分享下。
没理清逻辑的时候,真写不出来
void ContButton::touchBegan() { isTouch=true;//按钮按下 this->schedule(schedule_selector(ContButton::updatelongprogress),1);//蓄力时间判断 //做连击判断 if(touchCounts==0) onSingleCLick(); if(touchCounts==1&&m_pHero->getAttackAction()->isDone()) onDoubleClick(); if(touchCounts==2&&m_pHero->getAttackActionB()->isDone()) onThreeClick(); if(touchCounts==3&&m_pHero->getAttackActionC()->isDone()) touchCounts=0; } void ContButton::touchEnded() { isTouch=false; pressTimes=0; this->unschedule(schedule_selector(ContButton::updatelongprogress)); //如果刚完成长按事件 则把按下次数清零 长按状态置空 直接返回 不继续执行 if (m_longProgress ) { touchCounts=0; m_longProgress=false; return; } this->scheduleOnce(schedule_selector(ContButton::updateAttackDelay),0.8); } void ContButton::onSingleCLick() { CCLOG("signle");//1连击 this->unschedule(schedule_selector(ContButton::updateAttackDelay)); m_pHero->runAttackAction(); touchCounts++; } void ContButton::onDoubleClick() { CCLOG("double"); //2连击 this->unschedule(schedule_selector(ContButton::updateAttackDelay)); m_pHero->runAttackActionB(); touchCounts++; } void ContButton::onThreeClick() { CCLOG("three"); //3连击 this->unschedule(schedule_selector(ContButton::updateAttackDelay)); m_pHero->runAttackActionC(); touchCounts++; } void ContButton::onLongPressed() { CCLOG("preassed");//长按 } void ContButton::updateAttackDelay(float ft) { touchCounts=0; } void ContButton::updatelongprogress(float ft) { if (isTouch) { pressTimes++; if (pressTimes >= 2) { m_longProgress=true; onLongPressed(); } } else { pressTimes=0; } }原文链接:/cocos2dx/344710.html