0

我正在构建一个简单的三消游戏。我有一个问题,有时当我向下移动光标时,它也会向左或向右移动一点。
我尝试根据阻力的初始移动位置将移动限制为 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也是一样。

4

0 回答 0