我做了一个类,这是一个字符,在更新方法中,我使用尽可能多的物理,使它的工作现实和编辑的价值观,以使运动感觉良好。
我最近注意到,当我向左和向右移动时,它以不同的速度移动,并且不知道这是真的多久了。在开始的时候,我声明所有的大写名为变量常量,并使用相同的常量来表示运动,所以我不知道是什么导致了这一点。
我已尽力找出问题所在,并加以解决,但我什么也没有。
在我设定常量的开头:
const float GRAVITY = 1f;
const float AIR_SPEED_COEFFICIENT = 0.4f;
const float TERMINAL_AIR_HORIZONTAL_VELOCITY = 10f;
const float AIR_RESISTANCE = 0.97f;
const float FRICTION = 0.64f;更新功能:
public void Update(Rectangle floor, GameWindow Window)
{
jumpFrameCounter++;
if (Keyboard.GetState().IsKeyDown(Keys.Up) || Keyboard.GetState().IsKeyDown(Keys.W))
{
if (jumpFrameCounter > 11 && jumpsUsed < amountOfJumps)
{
velocity.Y = -GRAVITY * 16 * jumpHeight;
jumpsUsed++;
jumpFrameCounter = 0;
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X -= RUN_SPEED / 4;
}
else
{
velocity.X -= airSpeed * AIR_SPEED_COEFFICIENT;
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X += RUN_SPEED / 4;
}
else
{
velocity.X += airSpeed * AIR_SPEED_COEFFICIENT;
}
}
if (velocity.X > 0.00001f || velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A)))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X *= FRICTION;
}
else
{
velocity.X *= AIR_RESISTANCE;
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Down) || Keyboard.GetState().IsKeyDown(Keys.S))
{
fastFall = true;
}
velocity.Y += GRAVITY * unfloatyness;
if (fastFall)
{
velocity.Y += 6 * GRAVITY;
}
if (hitbox.Bottom >= floor.Top)
{
if (velocity.X > RUN_SPEED * groundSpeed)
{
velocity.X = RUN_SPEED * groundSpeed;
}
else if (velocity.X < -RUN_SPEED * groundSpeed)
{
velocity.X = -RUN_SPEED * groundSpeed;
}
}
else
{
if (velocity.X > TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed)
{
velocity.X = TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed;
}
else if (velocity.X < -TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed)
{
velocity.X = -TERMINAL_AIR_HORIZONTAL_VELOCITY * airSpeed;
}
}
position += velocity;
hitbox = new Rectangle((int)position.X, (int)position.Y, fighterTexture.Width, fighterTexture.Height);
if (hitbox.Bottom > floor.Top && velocity.Y > 0)
{
position.Y = floor.Top - fighterTexture.Height;
jumpsUsed = 0;
fastFall = false;
velocity.Y = -1f;
}
if (hitbox.Left < 0)
{
position.X = 0;
}
else if (hitbox.Right > Window.ClientBounds.Width)
{
position.X = Window.ClientBounds.Width - fighterTexture.Width;
}
if (hitbox.Top < 0)
{
position.Y = 0;
}
else if (hitbox.Bottom > Window.ClientBounds.Height)
{
position.Y = Window.ClientBounds.Bottom - fighterTexture.Height;
}
}发布于 2020-03-12 02:03:58
这句话很让人困惑。Because AND&& has higher precedence than OR||
if (velocity.X > 0.00001f || velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A)))声明就会变成。
if (velocity.X > 0.00001f
||
(velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) )
|| Keyboard.GetState().IsKeyDown(Keys.D)
|| Keyboard.GetState().IsKeyDown(Keys.Left)
|| Keyboard.GetState().IsKeyDown(Keys.A)))我猜你真的想:
if ( ( velocity.X > 0.00001f || (velocity.X < -0.00001f )
&&
(
! (Keyboard.GetState().IsKeyDown(Keys.Right)
|| Keyboard.GetState().IsKeyDown(Keys.D)
|| Keyboard.GetState().IsKeyDown(Keys.Left)
|| Keyboard.GetState().IsKeyDown(Keys.A)))
)但是你没有留下任何评论,我不知道怎么解释。这条线看起来像“如果x的速度接近于零,而Left、D或A被按下或Right没有按下”,则将X设为较大的值。
但这句话与你所描述的相冲突。
我最近注意到,当我向左和向右移动时,它以不同的速度移动,并且不知道这是真的多久了。
你希望左和右做同样的事情,但不是在代码中。
我的建议是“使用括号表示可读性”和“如果有超过3个条件需要签入一行时使用bool”。
发布于 2020-03-12 09:27:55
解决方案是路易·戈提供的,
事实证明,它就像将if语句更改为2个嵌套语句一样简单,并使计算机感到困惑。
旧法典:
if (velocity.X > 0.00001f || velocity.X < -0.00001f && !(Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A)))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X *= FRICTION;
}
else
{
velocity.X *= AIR_RESISTANCE;
}
}经修订的守则:
if (velocity.X > 0.00001f || velocity.X < -0.00001f)
{
if (!(keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A)))
{
if (hitbox.Bottom >= floor.Top)
{
velocity.X *= FRICTION;
}
else
{
velocity.X *= AIR_RESISTANCE;
}
}
}https://stackoverflow.com/questions/60645306
复制相似问题