我不知道为什么,但我的跳转方法不起作用。我的角色只能跳到一个物体上。另一个障碍对选手没有任何影响。我真的没有看到我的错误
foreach (RectangleShape obstacles in MoveAtObjectList)
{
MainPlayer.Move(deltaTime, obstacles);
}
foreach (RectangleShape obstacles in JumpAtObjectList)
{
MainPlayer.Jump(deltaTime, obstacles);
break;
}
Draw();
Window.Display();
}
}
}
}
这是我的主类的一部分,在那里我在foreach循环中调用jump方法。在Payer类中,我试图编写跳转函数。
using SFML.Graphics;
using SFML.System;
using SFML.Window;
namespace Pong3final
{
class Player : RectangleShape
{
float CollisionDistance = 0.5f;
float PlayerSpeed = 50;
float JumpSpeed = 100;
float FallSpeed = 100;
float JumpTimer;
bool JumpBool = false;
bool Fall = true;
public void Jump(float deltaTime, RectangleShape collisionObject)
{
if (Keyboard.IsKeyPressed(Keyboard.Key.Space) && !Fall && CheckCollision(this, collisionObject))
{
JumpBool = true;
}
//Player is jumping
if (JumpBool)
{
JumpTimer += deltaTime;
this.Position -= new Vector2f(0, JumpSpeed) * deltaTime;
if (JumpTimer > 0.5f)
{
JumpBool = false;
Fall = true;
JumpTimer = 0;
}
}
//Player is falling when he doesnt touch anything and he isnt jumping
else if (!CheckCollision(this, collisionObject))
{
Fall = true;
}
//Player is falling
if (!JumpBool && !CheckCollision(this, collisionObject) && Fall)
{
this.Position += new Vector2f(0, FallSpeed) * deltaTime;
if (CheckCollision(this, collisionObject)) //Player stops falling because of a collision with an object
{
Fall = false;
}
}
}
public void Move(float deltaTime, RectangleShape collisionObject)
{
{
Vector2f MovePlayerPosition = Position;
if ((Keyboard.IsKeyPressed(Keyboard.Key.Left) || Keyboard.IsKeyPressed(Keyboard.Key.A)))
{
MovePlayerPosition -= new Vector2f(PlayerSpeed, 0) * deltaTime;
FloatRect collisionOverlap;
if (CheckCollision(this, collisionObject, out collisionOverlap))
{
MovePlayerPosition = MovePlayerPosition + new Vector2f(collisionOverlap.Width + CollisionDistance, 0);
}
}
if (Keyboard.IsKeyPressed(Keyboard.Key.Right) || Keyboard.IsKeyPressed(Keyboard.Key.D))
{
MovePlayerPosition += new Vector2f(PlayerSpeed, 0) * deltaTime;
FloatRect collisionOverlap;
if (CheckCollision(this, collisionObject, out collisionOverlap))
{
MovePlayerPosition = MovePlayerPosition - new Vector2f(collisionOverlap.Width + CollisionDistance, 0);
}
}
Position = MovePlayerPosition;
}
}
public static bool CheckCollision(RectangleShape obj1, RectangleShape obj2, out FloatRect overlap)
{
FloatRect obj1Collider = obj1.GetGlobalBounds();
FloatRect obj2Collider = obj2.GetGlobalBounds();
//Nur die rechte, linke und obere Seite kollidiert
if (obj1Collider.Intersects(obj2Collider, out overlap))
{
return true;
}
overlap = new FloatRect();
return false;
}
public static bool CheckCollision(RectangleShape obj1, RectangleShape obj2)
{
FloatRect obj1Collider = obj1.GetGlobalBounds();
FloatRect obj2Collider = obj2.GetGlobalBounds();
return obj1Collider.Intersects(obj2Collider);
}
}
}
看起来你在第一次迭代后就突破了循环,结果只执行了第一次迭代:
foreach (RectangleShape obstacles in JumpAtObjectList)
{
MainPlayer.Jump(deltaTime, obstacles);
break;
}
从循环中删除break语句:
foreach (RectangleShape obstacles in JumpAtObjectList)
{
MainPlayer.Jump(deltaTime, obstacles);
}