How to restrict the movement of the touch only to X or Y?

Hey
im building simple match 3 game i have problem that sometimes when i move the courser down it goes also a bit to the left or the right
i try to restrict the movement to the X or the Y depending on where the initial movement of the drag goes , but i guess i miss here something .
what is the best way to detect the initial movement and strict it to that movement only

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)
}

and still i can’t catch it to be perfect Y or X movment

Why you don’t use calculated thisSpritePositionX and thisSpritePositionY instead of new method call in

mySprite->setPosition( ccp(mySprite->getPositionX(),touchPoint.y) );

and

mySprite->setPosition( ccp(touchPoint.x,mySprite->getPositionY()) );

Thanks for the replay , but what do you mean ?
what i want is very simple , when there is detection even the smallest in Y axis , i want that the movement will be locked only to the Y axis with no option to move to the X axis until finger released
the same for the X

and the answer is :
based on this thread no way only the boolean logic way
http://www.cocos2d-iphone.org/forums/topic/moving-straight-along-x-and-y-axis/

working great by the way

1 Like