gpt4 book ai didi

algorithm - 寻找 bool 选择的算法或设计模式

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:01:50 26 4
gpt4 key购买 nike

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

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

我正在寻找某种优雅的方式来执行此操作,而不是一直观察 4 个变量状态。

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

if sprite starts to move along the X axis:
set state == moveRight
if sprite moves more then 1 pixel on the Y axis and moveRight == true:
do nothing

这是我目前拥有的:

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

最佳答案

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

关于algorithm - 寻找 bool 选择的算法或设计模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18483112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com