.net 英雄角色在单机游戏中跳跃不起作用

zlwx9yxi  于 2023-07-01  发布在  .NET
关注(0)|答案(1)|浏览(111)

我在用单场游戏制作一个游戏我试着让角色跳起来,然后让重力把它拉下来,过了一会儿,目前任何动作都只需要“优先级”,这意味着你可以左右移动,永远不会摔倒我该怎么解决这个问题
相关代码:
Game1.cs

using GameDevelopement_Game.enemies;
using GameDevelopement_Game.enums;
using GameDevelopement_Game.Input;
using GameDevelopement_Game.interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SharpDX.Direct2D1.Effects;
using SharpDX.Direct3D9;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using Color = Microsoft.Xna.Framework.Color;

namespace GameDevelopement_Game
{
    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;
        private Texture2D background;
        private Texture2D startScherm;
        private Texture2D gameOver;
        private Texture2D finished;
        private GameState.CurrentGameState gameState = GameState.CurrentGameState.main_menu;
        //hero
        Hero Hero;
        //andere gameobjects
        public List<IGameObject> GameObjectsList = new List<IGameObject>();
        public List<Floor> floorListlvl1 = new List<Floor>();
        public List<Floor> floorListlvl2 = new List<Floor>();

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            _graphics.PreferredBackBufferWidth = 1920;
            _graphics.PreferredBackBufferHeight = 1080;
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            base.Initialize();            
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            InitializeGameObjects();
            background = Content.Load<Texture2D>("background");
            startScherm = Content.Load<Texture2D>("startscherm");
            gameOver = Content.Load<Texture2D>("game_over");
            finished = Content.Load<Texture2D>("success");
        }

        private void InitializeGameObjects()
        {
            #region all levels
            //bottom of screen floor
            Floor bottomFloor = new Floor(Content.Load<Texture2D>("Floor_Texture"), new Vector2(0, 1040), 1920, 10, 3);
            GameObjectsList.Add(bottomFloor);
            floorListlvl1.Add(bottomFloor);
            floorListlvl2.Add(bottomFloor);
            #endregion
            #region hero
            Hero = new Hero(Content.Load<Texture2D>("Fox_Sprite_Sheet_Running_4x"),Content.Load<Texture2D>("Fox_Sprite_Sheet_Running_4x_reversed"), Content.Load<Texture2D>("Fox_Sprite_Sheet_Standing_Still_4x"), Content.Load<Texture2D>("Fox_Sprite_Sheet_Stinding_Still_4x_Reversed"), Content.Load<Texture2D>("Red_full"), Content.Load<Texture2D>("Yellow_full") ,new Vector2(0, 0), new KeyboardReader(1, true, false), GameObjectsList,gameState, 3); //lvl 3 ==> in beide lvls
            #endregion
            #region lvl1
            //floors
            Floor floor1 = new Floor(Content.Load<Texture2D>("Floor_Texture"), new Vector2(0, 250), 1680, 100, 1);
            Floor floor2 = new Floor(Content.Load<Texture2D>("Floor_Texture"), new Vector2(240, 500), 1680, 100, 1);
            Floor floor3 = new Floor(Content.Load<Texture2D>("Floor_Texture"), new Vector2(0, 750), 1680, 100, 1);
            GameObjectsList.Add(floor1);
            GameObjectsList.Add(floor2);
            GameObjectsList.Add(floor3);
            floorListlvl1.Add(floor1);
            floorListlvl1.Add(floor2);
            floorListlvl1.Add(floor3);
            floorListlvl1.Add(floor3);
            //enemies
            GameObjectsList.Add(new Enemy1(Content.Load<Texture2D>("enemy_1"), Content.Load<Texture2D>("enemy_1_reversed"), new Vector2(900, 187), 1));
            GameObjectsList.Add(new Enemy3(Content.Load<Texture2D>("enemy_3"), Content.Load<Texture2D>("enemy_3_reversed"), new Vector2(400, 430), 1));
            GameObjectsList.Add(new Enemy3(Content.Load<Texture2D>("enemy_3"), Content.Load<Texture2D>("enemy_3_reversed"), new Vector2(1500, 430), 1));
            GameObjectsList.Add(new Enemy1(Content.Load<Texture2D>("enemy_1"), Content.Load<Texture2D>("enemy_1_reversed"), new Vector2(200, 687), 1));
            GameObjectsList.Add(new Enemy3(Content.Load<Texture2D>("enemy_3"), Content.Load<Texture2D>("enemy_3_reversed"), new Vector2(1300, 680), 1));
            GameObjectsList.Add(new Enemy2(Content.Load<Texture2D>("enemy_2"), Content.Load<Texture2D>("enemy_2_reversed"), new Vector2(800, 900), 1, floorListlvl1));
            GameObjectsList.Add(new Enemy2(Content.Load<Texture2D>("enemy_2"), Content.Load<Texture2D>("enemy_2_reversed"), new Vector2(1600, 900), 1, floorListlvl1));
            //endgate
            GameObjectsList.Add(new Gate(Content.Load<Texture2D>("gate"), new Vector2(0, 956), 1));            
            /*//test om snel van lvl 1 naar lvl 2 te gaan
            GameObjectsList.Add(new Gate(Content.Load<Texture2D>("gate"), new Vector2(100, 150), 1));*/
            //coins
            GameObjectsList.Add(new Coin(Content.Load<Texture2D>("coin"), new Vector2(200, 220), 1));
            GameObjectsList.Add(new Coin(Content.Load<Texture2D>("coin"), new Vector2(1800, 220), 1));
            GameObjectsList.Add(new Coin(Content.Load<Texture2D>("coin"), new Vector2(800, 470), 1));
            GameObjectsList.Add(new Coin(Content.Load<Texture2D>("coin"), new Vector2(300, 470), 1));
            GameObjectsList.Add(new Coin(Content.Load<Texture2D>("coin"), new Vector2(1000, 720), 1));
            GameObjectsList.Add(new Coin(Content.Load<Texture2D>("coin"), new Vector2(1500, 1010), 1));
            #endregion
            #region lvl2
            //floors
            Floor floor4 = new Floor(Content.Load<Texture2D>("Floor_Texture"), new Vector2(0, 250), 840, 100, 2);
            Floor floor5 = new Floor(Content.Load<Texture2D>("Floor_Texture"), new Vector2(1080, 250), 840, 100, 2);
            Floor floor6 = new Floor(Content.Load<Texture2D>("Floor_Texture"), new Vector2(240, 500), 1440, 100, 2);
            Floor floor7 = new Floor(Content.Load<Texture2D>("Floor_Texture"), new Vector2(0, 750), 840, 100, 2);
            Floor floor8 = new Floor(Content.Load<Texture2D>("Floor_Texture"), new Vector2(1080, 750), 840, 100, 2);
            GameObjectsList.Add(floor4);
            GameObjectsList.Add(floor5);
            GameObjectsList.Add(floor6);
            GameObjectsList.Add(floor7);
            GameObjectsList.Add(floor8);
            floorListlvl2.Add(floor4);
            floorListlvl2.Add(floor5);
            floorListlvl2.Add(floor6);
            floorListlvl2.Add(floor7);
            floorListlvl2.Add(floor8);
            //enemies
            GameObjectsList.Add(new Enemy1(Content.Load<Texture2D>("enemy_1"), Content.Load<Texture2D>("enemy_1_reversed"), new Vector2(400, 187), 2));
            GameObjectsList.Add(new Enemy1(Content.Load<Texture2D>("enemy_1"), Content.Load<Texture2D>("enemy_1_reversed"), new Vector2(1200, 187), 2));
            GameObjectsList.Add(new Enemy3(Content.Load<Texture2D>("enemy_3"), Content.Load<Texture2D>("enemy_3_reversed"), new Vector2(600, 430), 2));
            GameObjectsList.Add(new Enemy3(Content.Load<Texture2D>("enemy_3"), Content.Load<Texture2D>("enemy_3_reversed"), new Vector2(1300, 430), 2));
            GameObjectsList.Add(new Enemy1(Content.Load<Texture2D>("enemy_1"), Content.Load<Texture2D>("enemy_1_reversed"), new Vector2(200, 687), 2));
            GameObjectsList.Add(new Enemy3(Content.Load<Texture2D>("enemy_3"), Content.Load<Texture2D>("enemy_3_reversed"), new Vector2(1350, 680), 2));
            GameObjectsList.Add(new Enemy2(Content.Load<Texture2D>("enemy_2"), Content.Load<Texture2D>("enemy_2_reversed"), new Vector2(200, 900), 2, floorListlvl2));
            GameObjectsList.Add(new Enemy2(Content.Load<Texture2D>("enemy_2"), Content.Load<Texture2D>("enemy_2_reversed"), new Vector2(800, 900), 2, floorListlvl2));
            GameObjectsList.Add(new Enemy2(Content.Load<Texture2D>("enemy_2"), Content.Load<Texture2D>("enemy_2_reversed"), new Vector2(1600, 900), 2, floorListlvl2));
            //endgate
            GameObjectsList.Add(new Gate(Content.Load<Texture2D>("gate"), new Vector2(1920-52, 956), 2));
            //coins
            GameObjectsList.Add(new Coin(Content.Load<Texture2D>("coin"), new Vector2(200, 220), 2));
            GameObjectsList.Add(new Coin(Content.Load<Texture2D>("coin"), new Vector2(1800, 220), 2));
            GameObjectsList.Add(new Coin(Content.Load<Texture2D>("coin"), new Vector2(800, 470), 2));
            GameObjectsList.Add(new Coin(Content.Load<Texture2D>("coin"), new Vector2(300, 470), 2));
            GameObjectsList.Add(new Coin(Content.Load<Texture2D>("coin"), new Vector2(1700, 720), 2));
            GameObjectsList.Add(new Coin(Content.Load<Texture2D>("coin"), new Vector2(100, 1010), 2));
            #endregion
        }

        protected override void Update(GameTime gameTime)
        {
            //to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            //in main menu
            if (gameState == GameState.CurrentGameState.main_menu && Keyboard.GetState().IsKeyDown(Keys.Enter))
            {
                gameState = GameState.CurrentGameState.level_1;
                Hero._GameState = GameState.CurrentGameState.level_1;
            }
            //in lvl 1
            if (gameState == GameState.CurrentGameState.level_1)
            {
                Hero.Update(gameTime);               
                foreach (var obj in GameObjectsList)
                {
                    if(obj.lvl == 1 || obj.lvl == 3)
                    {
                        if (obj.isFloor == false)
                        {
                            obj.Update(gameTime);
                        }
                    }                    
                }
            }
            //exiting lvl 1
            if(Hero.coinCount >= 6 && gameState == GameState.CurrentGameState.level_1)
            {
                Hero.levelCompleted = true;
            }
            if(Hero.levelCompleted == true && gameState == GameState.CurrentGameState.level_1)
            {
                Hero.isdDead = false;
                Hero.levelCompleted = false;
                Hero.coinCount = 0;
                Hero.Health = 10;
                Hero.Positie = new Vector2(0, 0);
                Hero._GameState = GameState.CurrentGameState.level_2;
                gameState = GameState.CurrentGameState.level_2;
            }
            //in lvl 2
            if (gameState == GameState.CurrentGameState.level_2)
            {
                Hero.Update(gameTime);
                foreach (var obj in GameObjectsList)
                {
                    if (obj.lvl == 2 || obj.lvl == 3)
                    {
                        if (obj.isFloor == false)
                        {
                            obj.Update(gameTime);
                        }
                    }
                }
            }
            //exiting lvl 2
            if (Hero.coinCount >= 6 && gameState == GameState.CurrentGameState.level_2)
            {
                Hero.levelCompleted = true;
            }
            if (Hero.levelCompleted == true && gameState == GameState.CurrentGameState.level_2)
            {
                Hero.isdDead = false;
                Hero.levelCompleted = false;
                Hero.Positie = new Vector2(500, 0);
                Hero._GameState = GameState.CurrentGameState.finished;
                gameState = GameState.CurrentGameState.finished;
            }
            //death
            if (Hero.isdDead == true && gameState == GameState.CurrentGameState.level_1 || Hero.isdDead && gameState == GameState.CurrentGameState.level_2)
            {
                gameState = GameState.CurrentGameState.gameover;
            }
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            _spriteBatch.Begin();

            if(gameState == GameState.CurrentGameState.main_menu)
            {
                _spriteBatch.Draw(startScherm, new Vector2(0, 0), Color.White);
            }
            if(gameState == GameState.CurrentGameState.level_1)
            {
                _spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
                Hero.Draw(_spriteBatch);
                foreach (var obj in GameObjectsList)
                {
                    if(obj.lvl == 1 || obj.lvl == 3)
                    {
                        obj.Draw(_spriteBatch);
                    }                    
                }
            }
            if(gameState == GameState.CurrentGameState.level_2)
            {
                _spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
                Hero.Draw(_spriteBatch);
                foreach (var obj in GameObjectsList)
                {
                    if(obj.lvl == 2 || obj.lvl == 3)
                    {
                        obj.Draw(_spriteBatch);
                    }                    
                }
            }
            if(gameState == GameState.CurrentGameState.gameover)
            {
                _spriteBatch.Draw(gameOver, new Vector2(0, 0), Color.White);
            }
            if(gameState == GameState.CurrentGameState.finished)
            {
                _spriteBatch.Draw(finished, new Vector2(0, 0), Color.White);
            }

            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

hero.cs:

using GameDevelopement_Game.Animation;
using GameDevelopement_Game.enums;
using GameDevelopement_Game.Input;
using GameDevelopement_Game.interfaces;
using GameDevelopement_Game.Movement;
using GameDevelopement_Game.rendering;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SharpDX.Direct2D1;
using SharpDX.Direct3D9;
using SharpDX.MediaFoundation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices.ActiveDirectory;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Color = Microsoft.Xna.Framework.Color;
using Rectangle = Microsoft.Xna.Framework.Rectangle;
using SpriteBatch = Microsoft.Xna.Framework.Graphics.SpriteBatch;

namespace GameDevelopement_Game
{
    public class Hero:IGameObject
    {
        //identifiers
        public bool isEnemy { get; } = false;
        public bool isFloor { get; } = false;
        public bool isdDead { get; set; } = false;
        public bool isGate { get; } = false;
        public bool isCoin { get; } = false;
        public int lvl { get; set; }
        public bool levelCompleted { get; set; }
        public int damage { get; } = 0;
        //input
        private IInputReader inputreader;

        //animaties
        Texture2D heroTextureRunning;
        Texture2D heroTextureRunningReversed;
        Texture2D heroTextureStandingStill;
        Texture2D heroTextureStandingStillReversed;
        Texture2D healthBar;
        Texture2D coinBar;
        private Rectangle standingStillSourceRectangle = new Rectangle(0, 0, 84, 64);
        Animatie animatie;

        //hero movement
        private Vector2 snelheid;
        public Vector2 Snelheid
        {
            get { return snelheid; }
            set { snelheid = value; }
        }

        private Vector2 versnelling;
        public Vector2 Versnelling
        {
            get { return versnelling; }
            set { versnelling = value; }
        }
        private Vector2 positie;
        public Vector2 Positie
        {
            get { return positie; }
            set { positie = value; }
        }
        public void ChangePosX(float x)
        {
            positie.X = x;
        }
        public void ChangePosY(float y)
        {
            positie.Y = y;
        }
        private Vector2 direction;
        public Vector2 Direction
        {
            get { return direction; }
            set { direction = value; }   
        }
        public void ChangeDirectionX(float x)
        {
            direction.X = x;
        }
        public void ChangeDirectionY(float y)
        {
            direction.Y = y;
        }
        private MovementManager heroMovementManager;
        public bool isStandingStill;

        //health
        private int health;
        public int Health
        {
            get { return health; }
            set { health = value; }
        }
        private Vector2 healthbarPos = new Vector2(0, 0);
        private Rectangle healthbarSize;
        public int invincibilityTimer;

        //coins
        public int coinCount { get; set; }
        private Vector2 coinbarPos = new Vector2(1000, 0);
        private Rectangle coinbarSize;

        //collision
        private List<IGameObject> otherObjList = new List<IGameObject>();

        public Rectangle CollisionRectangle
        {
            get
            {
                return new Rectangle((int)Positie.X, (int)Positie.Y, 84, 64);
            }
        }
        private GameState.CurrentGameState gameState;
        public GameState.CurrentGameState _GameState
        {
            get
            {
                return gameState;
            }
            set
            {
                gameState = value;
            }
        }
        public Hero(Texture2D textureRunning,Texture2D textureRunningReversed,Texture2D textureStandingStill,Texture2D textureStandingStillReversed , Texture2D healthbar, Texture2D coins, Vector2 Positie, IInputReader Inputreader, List<IGameObject> otherObj,GameState.CurrentGameState g, int _lvl)
        {
            heroTextureRunning = textureRunning;
            heroTextureRunningReversed = textureRunningReversed;
            this.heroTextureStandingStill = textureStandingStill;
            this.heroTextureStandingStillReversed = textureStandingStillReversed;
            this.healthBar = healthbar;
            this.coinBar = coins;
            inputreader = Inputreader;
            #region animation
            animatie = new Animatie(15);
            animatie.AddAnimationFrame(new AnimationFrame(new Rectangle(0, 0, 84, 64)));
            animatie.AddAnimationFrame(new AnimationFrame(new Rectangle(128, 0, 84, 64)));
            animatie.AddAnimationFrame(new AnimationFrame(new Rectangle(256, 0, 84, 64)));
            animatie.AddAnimationFrame(new AnimationFrame(new Rectangle(384, 0, 84, 64)));
            animatie.AddAnimationFrame(new AnimationFrame(new Rectangle(512, 0, 84, 64)));
            animatie.AddAnimationFrame(new AnimationFrame(new Rectangle(640, 0, 84, 64)));
            animatie.AddAnimationFrame(new AnimationFrame(new Rectangle(768, 0, 84, 64)));
            animatie.AddAnimationFrame(new AnimationFrame(new Rectangle(896, 0, 84, 64)));
            #endregion
            positie = Positie;
            snelheid = new Vector2(10, 10);
            versnelling = new Vector2(0.1f, 0.1f);
            otherObjList = otherObj;
            heroMovementManager = new MovementManager(inputreader);
            health = 10;
            healthbarSize = new Rectangle(0, 0, health * 100, 20);
            lvl = _lvl;
            levelCompleted = false;
            gameState = g;
            coinCount = 0;
            coinbarSize = new Rectangle(0, 0, coinCount * 100, 20);
        }

        public void Update(GameTime gametime)
        {
            heroMovementManager.Move(this, 84, 64, otherObjList);
            animatie.Update(gametime);
        }
        
        public void Draw(SpriteBatch _spriteBatch)
        {
            RenderHero.DrawHero(_spriteBatch, inputreader, heroTextureRunning, heroTextureRunningReversed, heroTextureStandingStill, heroTextureStandingStillReversed, healthBar, coinBar, positie, animatie, invincibilityTimer, health, healthbarPos, coinCount, coinbarPos);
        }
    }
}

IInputReader.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;

namespace GameDevelopement_Game.Input
{
    public interface IInputReader
    {
        int lookDirection { get; set; }
        bool isStandingStill { get; set; }
        public bool triesToJump { get; set; }
        Vector2 ReadInput();

    }
}

KeyBoardReader.cs:

using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;

namespace GameDevelopement_Game.Input
{
    public class KeyboardReader:IInputReader
    {
        public int lookDirection { get; set; }

        public bool isStandingStill { get; set; }

        public bool triesToJump { get; set; }
        private int jumpTimer = 0;

        public KeyboardReader(int LookDirection, bool isStandingStill, bool Jump)
        {
            this.lookDirection = LookDirection;
            this.isStandingStill = isStandingStill;
            triesToJump = Jump;
        }
        public Vector2 ReadInput()
        {
            jumpTimer++;
            KeyboardState state = Keyboard.GetState();
            Vector2 direction = Vector2.Zero;
            if (state.IsKeyUp(Keys.Left) && state.IsKeyUp(Keys.Right) && state.IsKeyUp(Keys.Space))
            {
                isStandingStill = true;
            }
            if (state.IsKeyDown(Keys.Left))
            {
                isStandingStill = false;
                direction.X -= 1;
                this.lookDirection = -1;
                //
                return direction;
            }
            if (state.IsKeyDown(Keys.Right))
            {
                isStandingStill = false;
                direction.X += 1;
                this.lookDirection = 1;
                //
                return direction;
            }
            if (state.IsKeyDown(Keys.Up))
            {
                if(jumpTimer > 60)
                {
                    jumpTimer = 0;
                }
                if(jumpTimer < 30)
                {
                    direction.Y -= 1;
                    return direction;
                }
            }
            if (/*state.IsKeyDown(Keys.Down)*/true)
            {
                direction.Y += 1;
                //
                return direction;
            }
            return direction;
        }

    }
}

MovementManager.cs:

using GameDevelopement_Game.Input;
using GameDevelopement_Game.interfaces;
using Microsoft.Xna.Framework;
using SharpDX.Direct2D1.Effects;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameDevelopement_Game.Movement
{
    public class MovementManager
    {
        private IInputReader inputReader;
        private Vector2 toekomstigePositie;
        private int jumpTimer;
        private int invincibilityTimer;
        public MovementManager(IInputReader ireader)
        {
            inputReader = ireader;
            jumpTimer = 0;
            invincibilityTimer = 50;
        }
        public void Move(Hero hero, int heroWidth, int heroHeight, List<IGameObject> objects)
        {
            hero.Direction = inputReader.ReadInput();
            hero.Direction *= hero.Snelheid;
            toekomstigePositie = hero.Positie + hero.Direction;
            hero.invincibilityTimer = invincibilityTimer;

            if (inputReader.triesToJump)
            {
                //Jump(hero.Positie.Y, 20);
            }

            #region collision detection
            bool hashit = false;
            Rectangle heroCollisionRectangle;
            heroCollisionRectangle = new Rectangle((int)toekomstigePositie.X, (int)toekomstigePositie.Y, heroWidth, heroHeight - 4);
            //check for X value
            bool collidesX = false;
            foreach (IGameObject obj in objects)
            {
                if (obj.lvl == (int)hero._GameState && obj.isdDead == false)
                {
                    if (heroCollisionRectangle.Intersects(obj.CollisionRectangle))
                    {
                        if (obj.isEnemy == true && invincibilityTimer > 50)
                        {
                            hashit = true;
                            hero.Health -= obj.damage;
                            invincibilityTimer = 0;
                        }
                        if (obj.isGate == true)
                        {
                            hero.levelCompleted = true;
                        }
                        if (obj.isCoin == true)
                        {
                            obj.isdDead = true;
                            hero.coinCount++;
                        }
                        else if (!obj.isEnemy)
                        {
                            collidesX = true;

                        }
                    }
                }
            }
            if (!collidesX) { hero.ChangePosX(toekomstigePositie.X); }

            //check for Y value
            bool collidesY = false;
            foreach (IGameObject obj in objects)
            {
                if (obj.lvl == (int)hero._GameState && obj.isdDead == false)
                {
                    if (heroCollisionRectangle.Intersects(obj.CollisionRectangle))
                    {
                        if (obj.isEnemy == true && invincibilityTimer > 50)
                        {
                            if (hashit == false)
                            {
                                hero.Health -= obj.damage;
                                invincibilityTimer = 0;
                            }
                        }
                        if (obj.isGate == true)
                        {
                            hero.levelCompleted = true;
                        }
                        if (obj.isCoin == true)
                        {
                            obj.isdDead = true;
                            hero.coinCount++;
                        }
                        else if (!obj.isEnemy)
                        {
                            collidesY = true;
                        }
                    }
                }
            }
            if (!collidesX) { hero.ChangePosY(toekomstigePositie.Y); }
            hashit = false;
            invincibilityTimer++;
            if (invincibilityTimer == 2000000000)
            {
                invincibilityTimer = 100;
            }


            //check for screen border
            if (hero.Positie.X < 0)
            {
                hero.ChangePosX(0);
            }
            if (hero.Positie.Y < 0)
            {
                hero.ChangePosY(0);
            }
            if (hero.Positie.X > 1920 - 84)
            {
                hero.ChangePosX(1920 - 84);
            }
            if (hero.Positie.Y > 1080 - 96)
            {
                hero.ChangePosY(1080 - 96);
            }
            #endregion
            if (hero.Health <= 0)
            {
                hero.isdDead = true;
            }
        }
        /*
        private bool Jump(float charPosY, float jumpHeight)
        {
            //Temporary vars
            float charStartPosY = charPosY;
            float jumpStartHeight = jumpHeight;
            bool goDown = false;

            //Check if character is not going down, let the man jump!
            while (!goDown)
            {
                for (float i = 0; i <= jumpStartHeight; i++)
                {
                    charPosY -= jumpHeight;
                    jumpHeight--;
                    if (i >= jumpStartHeight)
                    {
                        //If the man finished jumping, set goDown true
                        goDown = true;
                        jumpHeight = jumpStartHeight;
                    }
                }
            }

            return goDown;
        }*/
    }
    
}
abithluo

abithluo1#

问题是您在keyboardreader.csReadInput()中的每个if语句中使用了return direction;
return意味着它将结束函数并跳过函数的剩余部分,如果定义了,则返回一个值。
看到你返回一个Vector2,我认为它可以返回更多的方向值。因此,尝试删除ReadInput()中的所有return direction;,除了最后一个。然后再试一次。

相关问题