cocos2dx学习之贪吃蛇移动解决方案 Life is but a span.
移动时需要后方队员追赶前面的队员,保持简单的算法。但存在的问题是队长移动太快时出现错位现象。所以设计出几套解决方案;
tempHero = _hero->_afterHero;
while (tempHero != NULL) {//当前非空
if (tempHero->_myAnimType == tempHero->_preHero->_myAnimType) {//当前和前面的动作一致
switch (tempHero->_myAnimType) {
...
}
tempHero->setPosition(ccpAdd(tempHero->getPosition(), Point(speedX,speedY)));
}else{//当前和前面动作不同
switch (tempHero->_myAnimType) {
case animType_null:
break;
case animType_right:
{
if (tempHero->getPositionX() < tempHero->_preHero->getPositionX()) {//未超过
tempHero->setPosition(ccpAdd(tempHero->getPosition(),Point(_herospeed,0)));
}else {//超过
if (tempHero->_preHero->_myAnimType == animType_up) {
tempHero->setPosition(tempHero->_preHero->getPositionX(),tempHero->_preHero->getPositionY() - 64);
}else {
tempHero->setPosition(tempHero->_preHero->getPositionX(),tempHero->_preHero->getPositionY() + 64);
}
tempHero->playAnim(tempHero->_preHero->_myAnimType);
}
}
break;
case animType_left:
{
...
}
case animType_down:
{
...
}
case animType_up:
{
...
}
}
}
tempHero = tempHero->_afterHero;
}
blog comments powered by Disqus