我正在建立一个简单的比赛3游戏。我有一个问题,有时当我向下移动光标时,它也会向左或向右移动一点
我尝试将移动限制在X或Y上,这取决于拖动的初始移动的位置,但我想我在这里遗漏了一些东西。
检测初始运动的最佳方法是什么,并将其严格限制在该运动中:
这就是我所拥有的:
bool Gem::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
CCPoint touchPoint = touch->getLocation();
if (m_state != kPaddleStateUngrabbed)
{
return false;
}
if ( !containsTouchLocation(touch) )
{
return false;
}
m_state = kPaddleStateGrabbed;
TouchBeganPossitionY = touch->getLocation().y;
TouchBeganPossitionX = touch->getLocation().x;
std::string idd = getGemId();
return true;
}
void Gem::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
CCAssert(m_state == kPaddleStateGrabbed, "Gem - Unexpected state!");
bMoveY = false;
bMoveX = false;
m_state = kPaddleStateUngrabbed;
bMoveYContinue = true;
bMoveXContinue = true;
nextGem = NULL;
isNextGemSelected = false;
}
void Gem::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
CCPoint touchPoint = touch->getLocation();
int colNum = -1;
int rowNum = -1;
float thisSpritePositionX = -1;
float thisSpritePositionInPlaceX = -1;
float thisSpritePositionY = -1;
float thisSpritePositionInPlaceY = -1;
colNum = getColNum();
rowNum = getRowNum();
thisSpritePositionX = this->mySprite->getPositionX();
thisSpritePositionInPlaceX = this->getGemPos().x;
thisSpritePositionY = this->mySprite->getPositionY();
thisSpritePositionInPlaceY= this->getGemPos().y;
if((touchPoint.y != TouchBeganPossitionY) && !bMoveX && bMoveYContinue)
{
//going down
if((touchPoint.y < TouchBeganPossitionY) )
{
CCLOG("move down");
if(!isNextGemSelected)
{
nextGem = getNextGemByRowAndCol(getRowNum()-1,getColNum());
isNextGemSelected = true;
}
if(nextGem != NULL)
{
/*
the touch move will be for few pixels , then the MoveBy action will take place
*/
float thisSpritePositionInPlaceYEstimate = (thisSpritePositionInPlaceY-positionPaddingExtend);
if(thisSpritePositionY < thisSpritePositionInPlaceYEstimate)
{
bMoveY = true;
mySprite->setPosition( ccp(mySprite->getPositionX(),touchPoint.y) );
}
else
{
bMoveYContinue = false;
isNextGemSelected = false;
setPositionForGemOnDetect(nextGem,kMoveDown);
}
}
}//End if((touchPoint.y < TouchBeganPossitionY) )
else if(touchPoint.y > TouchBeganPossitionY)
{
CCLOG("move up");
if(!isNextGemSelected)
{
nextGem = getNextGemByRowAndCol(getRowNum()+1,getColNum());
isNextGemSelected = true;
}
if(nextGem != NULL)
{
/*
the touch move will be for few pixels , then the MoveBy action will take place
*/
float thisSpritePositionInPlaceYEstimate = (thisSpritePositionInPlaceY+positionPaddingExtend);
if(thisSpritePositionY > thisSpritePositionInPlaceYEstimate)
{
bMoveY = true;
mySprite->setPosition( ccp(mySprite->getPositionX(),touchPoint.y) );
}
else
{
isNextGemSelected = false;
bMoveYContinue = false;
setPositionForGemOnDetect(nextGem,kMoveUp);
}
}
}
} //End if((touchPoint.y != TouchBeganPossitionY) && !bMoveX && bMoveYContinue)
else if((touchPoint.x != TouchBeganPossitionX) && !bMoveY && bMoveXContinue)
{
//Moving Right
if((touchPoint.x > TouchBeganPossitionX) )
{
CCLOG("move right");
if(!isNextGemSelected)
{
nextGem = getNextGemByRowAndCol(rowNum,colNum+1);
isNextGemSelected = true;
}
if(nextGem != NULL)
{
/*
the touch move will be for few pixels , then the MoveBy action will take place
*/
float thisSpritePositionInPlaceXEstimate = (thisSpritePositionInPlaceX+positionPaddingExtend);
if(thisSpritePositionX < thisSpritePositionInPlaceXEstimate)
{
//bMoveXContinue = false;
bMoveX = true;
mySprite->setPosition( ccp(touchPoint.x,mySprite->getPositionY()) );
}
else
{
bMoveXContinue = false;
isNextGemSelected = false;
setPositionForGemOnDetect(nextGem,kMoveRight);
}
}
}//End if((touchPoint.x > TouchBeganPossitionX) )
else if(touchPoint.x < TouchBeganPossitionX)
{
CCLOG("move left");
if(!isNextGemSelected)
{
nextGem = getNextGemByRowAndCol(rowNum,colNum-1);
isNextGemSelected = true;
}
if(nextGem != NULL)
{
float thisSpritePositionInPlaceXEstimate = (thisSpritePositionInPlaceX-positionPaddingExtend);
if(thisSpritePositionX>thisSpritePositionInPlaceXEstimate)
{
bMoveX = true;
mySprite->setPosition( ccp(touchPoint.x,mySprite->getPositionY()) );
}
else
{
bMoveXContinue = false;
isNextGemSelected = false;
setPositionForGemOnDetect(nextGem,kMoveLeft);
}
}
}
}//End else if((touchPoint.x != TouchBeganPossitionX) && !bMoveY && bMoveXContinue)
}
简而言之:
我想要的是非常简单,当检测到Y轴中的最小值时,我希望机芯仅锁定到Y轴,没有选项移动到X轴,直到释放手指。 X一样。