Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 寻找布尔选择的逻辑、算法或设计模式_Algorithm_Design Patterns - Fatal编程技术网

Algorithm 寻找布尔选择的逻辑、算法或设计模式

Algorithm 寻找布尔选择的逻辑、算法或设计模式,algorithm,design-patterns,Algorithm,Design Patterns,我遇到的情况是,如果满足一个条件,则必须禁用其他条件,即使它们在下一阶段变为真 基本上我有一个方法,每次移动精灵时都会触发。我想将移动限制为单轴,因此当它开始在x轴上移动时,它只能在x轴上移动,直到拖动结束 我正在寻找一种优雅的方法来实现这一点,而不是一直观察4个变量的状态 例如,在伪代码中,我需要完成以下操作(对于x轴): 以下是我目前拥有的: void GameController::handleTouchMoved(CCTouch* touch) { // right

我遇到的情况是,如果满足一个条件,则必须禁用其他条件,即使它们在下一阶段变为真

基本上我有一个方法,每次移动精灵时都会触发。我想将移动限制为单轴,因此当它开始在x轴上移动时,它只能在x轴上移动,直到拖动结束

我正在寻找一种优雅的方法来实现这一点,而不是一直观察4个变量的状态

例如,在伪代码中,我需要完成以下操作(对于x轴):

以下是我目前拥有的:

void GameController::handleTouchMoved(CCTouch* touch)
{   
    // right
    if(dragX > dragPrevX)
    {
        pSelectedCurrent->setPosition(ccp(pSelectedCurrent->getPositionX()+movmentSpeed,pSelectedCurrent->getPositionY()));
    }
    // left
    else if(dragX < dragPrevX)
    {
        pSelectedCurrent->setPosition(ccp(pSelectedCurrent->getPositionX()-movmentSpeed,pSelectedCurrent->getPositionY()));
    }
    // up
    else if(dragY > dragPrevY)
    {
        pSelectedCurrent->setPosition(ccp(pSelectedCurrent->getPositionX(),pSelectedCurrent->getPositionY()+movmentSpeed));
    }
    // down 
    else if(dragY < dragPrevY)
    {
        pSelectedCurrent->setPosition(ccp(pSelectedCurrent->getPositionX(),pSelectedCurrent->getPositionY()-movmentSpeed));
    }   
}
void GameController::handleTouchMoved(CCTouch*touch)
{   
//对
如果(dragX>dragPrevX)
{
pSelectedCurrent->setPosition(ccp(pSelectedCurrent->getPositionX()+移动速度,pSelectedCurrent->getPositionY());
}
//左
否则如果(dragXsetPosition(ccp(pSelectedCurrent->getPositionX()-movementspeed,pSelectedCurrent->getPositionY());
}
//向上
else if(dragY>dragPrevY)
{
pSelectedCurrent->setPosition(ccp(pSelectedCurrent->getPositionX(),pSelectedCurrent->getPositionY()+移动速度));
}
//向下
否则如果(拖动<拖动速度)
{
pSelectedCurrent->setPosition(ccp(pSelectedCurrent->getPositionX(),pSelectedCurrent->getPositionY()-移动速度));
}   
}
您可以这样做:

void GameController::handleTouchMoved(CCTouch* touch)
{   
    // right and left
    if(dragX != dragPrevX)
    {
        pSelectedCurrent->setPosition(ccp(pSelectedCurrent->getPositionX()+(dragX-dragPrevX)/(abs(dragX-dragprevX)*movmentSpeed,pSelectedCurrent->getPositionY()));
    }
// up and down
    else if(dragY != dragPrevY)
    {
       pSelectedCurrent->setPosition(ccp(pSelectedCurrent->getPositionX(),pSelectedCurrent->getPositionY()+(dragY-dragPrevY)/(abs(dragY-dragprevY)*movmentSpeed));
    }

}
int moveX,moveY;
...
//第一步
moveX=0;
moveY=0;
如果精灵开始沿X轴移动
moveX=1;
其他的
moveY=1;
...
//一般移动处理
p选择电流->设置位置(cpp(
pSelectedCurrent->getPositionX()+符号(dragX-dragPrevX)*移动速度*moveX,
pSelectedCurrent->getPositionY()+符号(dragY-dragPrevY)*移动速度*moveY
));
//符号(x)=-1,如果x<0,
//0,如果x=0,
//1,如果x>0

你可以封装那些moveUp()moveDown()等等,但无论如何你都无法避免检查..这很好,但我需要检测它是右还是左,是下还是上,以将其传递到右动画函数这只适用于正向移动,也有反向移动
void GameController::handleTouchMoved(CCTouch* touch)
{   
    // right and left
    if(dragX != dragPrevX)
    {
        pSelectedCurrent->setPosition(ccp(pSelectedCurrent->getPositionX()+(dragX-dragPrevX)/(abs(dragX-dragprevX)*movmentSpeed,pSelectedCurrent->getPositionY()));
    }
// up and down
    else if(dragY != dragPrevY)
    {
       pSelectedCurrent->setPosition(ccp(pSelectedCurrent->getPositionX(),pSelectedCurrent->getPositionY()+(dragY-dragPrevY)/(abs(dragY-dragprevY)*movmentSpeed));
    }

}
int moveX, moveY;
...

// First move
moveX = 0;
moveY = 0;
if sprite starts to move along the X axis
     moveX = 1;
else
     moveY = 1;

...

// General move handling
pSelectedCurrent->setPosition(cpp(
    pSelectedCurrent->getPositionX()+sign(dragX - dragPrevX)*movmentSpeed*moveX,
    pSelectedCurrent->getPositionY()+sign(dragY - dragPrevY)*movmentSpeed*moveY
    ));

// sign(x) = -1, if x < 0, 
//            0, if x = 0,
//            1, if x > 0