【问题标题】:C# Sfml i dont see the mistake?C# Sfml 我没有看到错误?
【发布时间】:2020-06-19 13:32:45
【问题描述】:

我不知道为什么,但是我的跳转方法不起作用。我的角色只能跳到一个物体上。其他障碍对玩家没有任何影响。我真的没有看到我的错误

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

【问题讨论】:

    标签: c# windows 2d sfml game-development


    【解决方案1】:

    看起来您在第一次迭代后打破了循环,这导致只执行第一次迭代:

    foreach (RectangleShape obstacles in JumpAtObjectList)
    {
        MainPlayer.Jump(deltaTime, obstacles);
        break;
    }
    

    从循环中删除 break 语句:

     foreach (RectangleShape obstacles in JumpAtObjectList)
     {
         MainPlayer.Jump(deltaTime, obstacles);
     }
    

    【讨论】:

    • 我删除了它,但现在我启动程序时播放器总是消失。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 2017-01-14
    • 2014-05-19
    相关资源
    最近更新 更多