How to limit movement of CCSprit in direct straight Y,X lines

Hello all , its abit algurithem question and also cocos2d-x question
i building simple match 3 game , now inside the GEM CCSprit when used touch it and move it to the X axis i want it to move only in direct straight X line (right and left )
and when the user touch and move it in the Y axis want it to move only in direct straight Y line (up and down)
this is how i use it in the GEM class :

void Gem::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
    CCPoint touchPoint = touch->getLocation();    
    CCAssert(m_state == kPaddleStateGrabbed, "Gem - Unexpected state!");    
    CCLOG("Gem %s ccTouchMoved! x:%f ,y:%f",getImageName().c_str(),touchPoint.x,touchPoint.y);

    if(touchPoint.x >0)
    {
        setPosition( ccp(touchPoint.x,getPositionY()) );
    }
    else if(touchPoint.y >0)
    {
        setPosition( ccp(getPositionX(),touchPoint.y) );
    }
}

but in this code it moves only in the X axis
thanks

Seems to me like your code is never reaching the else condition, since you’re hardly ever going to get a ccTouchMoved callback where the x didn’t move at all.

If it were me, I’d set up three states, one for not moving, one for moving vertically and one for moving horizontally. When the gem is grabbed in the “not moving” state, when the touch is moved, I’d check if it moved more horizontally or vertically, and activate one of the other two states accordingly. Depending on which state is active, I’d move just the x or just the y. This way it would make it a bit easier to validate stuff like how far the user can move one gem, etc.