一种来回摇摆价值的方法

Ste*_*lla 2 c# xna

我正在做一个小游戏,我有一些我想要升高和降低的物体.对象移动到Y的最大值 - >对象移动到Y的最小值 - >重复.我对如何做到这一点有一个大概的想法,我会把它放在一个计时器/我的更新方法中.

if(Y >= max)
{
    direction = "down";
}
if(y =< min)
{
    direction = "up";
}

if(direction == "up") Y -= speed;
if(direction == "down") Y += speed;
Run Code Online (Sandbox Code Playgroud)

(也可以使用bool of course,但为了隐瞒)

但感觉我只是在重新发明轮子,是否有任何机会内置方法/数学函数自动执行此操作?例如.SomeFunction(min,max,increment).我正在使用XNA框架,因此内置的功能也是受欢迎的.

Mat*_*son 7

忘了单独的direction旗帜.

只需使用负速度"up"来简化代码:

if ((Y >= max) || (Y <= min)) // Hit an edge?
   speed = -speed;            // Reverse direction.

Y += speed;
Run Code Online (Sandbox Code Playgroud)