0

シンプルなマッチ 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軸で最小でも検出がある場合、指が離されるまでX軸に移動するオプションを使用せずに、移動をY軸にのみロックする必要があります。Xについても同様です。

4

0 に答える 0