XNA 4.0 左右移动播放器不起作用,为什么

本文关键字:不起作用 为什么 播放器 移动 左右 XNA | 更新日期: 2023-09-27 18:30:31

我按照YouTube上的Oyyou91教程,制作了一个基于瓷砖的小跳跃和运行游戏。然而,每次我试图向左或向右移动玩家时,他就是不动。他只在跳跃时向左或向右移动。在那之后,我完全重做了所有事情,并按照他(Oyyou91)的方式做了,当玩家站在地上时仍然不会移动。下面是静态的 TileCollider 类:

using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XJump
{
    public static class TileCollider
    {
        public static bool TouchesTop(Rectangle rect1, Rectangle rect2)
        {
            if ((rect1.Bottom >= rect2.Top - 1) && (rect1.Bottom <= rect2.Top + (rect2.Height / 2))&&
                (rect1.Right >= rect2.Left + rect2.Width / 5)&&(rect1.Left <= rect2.Right - rect2.Width / 5))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool TouchesBottom(Rectangle rect1, Rectangle rect2)
        {
            if ((rect1.Top <= rect2.Bottom +(rect2.Height / 5))&&
                (rect1.Top >= rect2.Bottom -1)&&
                (rect1.Right >= rect2.Left + (rect2.Width / 5))&&
                (rect1.Left <= rect2.Right - (rect2.Width / 2)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool TouchesLeft(Rectangle rect1, Rectangle rect2)
        {
            if ((rect1.Right <= rect2.Right)&&
                (rect1.Right >= rect2.Left -5)&&
                (rect1.Top >= rect2.Bottom - (rect2.Width / 4))&&
                (rect1.Bottom <= rect2.Top + (rect2.Width / 4)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool TouchesRight(Rectangle rect1, Rectangle rect2)
        {
            if ((rect1.Left >= rect2.Left)&&
                (rect1.Left <= rect2.Right + 5)&&
                (rect1.Top >= rect2.Bottom - (rect2.Width / 4))&&
                (rect1.Bottom <= rect2.Top + (rect2.Width / 4)))
            {
                return true;
            }
            else
            {
                return false;
            } 
        }


    }
}

这是我的播放器类:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XJump.Map;
using XJump.Tiles;
namespace XJump.Entities
{
    class Player
    {
        private Texture2D texture;
        private Rectangle rectangle;
        private float VelocityY;
        private float VelocityX;
        private bool HasJumped = false;
        public void Draw(SpriteBatch sb)
        {
            sb.Draw(texture, rectangle, Color.White);
        }
        private void Update()
        {
            VelocityX = 0;
            if (VelocityY < 10)
            {
                VelocityY += 1;
            }
            KeyboardState ks = Keyboard.GetState();
            MouseState ms = Mouse.GetState();
            Collision();
            Input(ks, ms);

            rectangle.X += (int)VelocityX;
            rectangle.Y += (int)VelocityY;

        }
        private void Input(KeyboardState ks, MouseState ms)
        {
            if (ks.IsKeyDown(Keys.A))
            {
                VelocityX = -10;
            }
            if (ks.IsKeyDown(Keys.D))
            {
                VelocityX = 10;
            }
            if(ks.IsKeyDown(Keys.W))
            {
                if(!HasJumped)
                {
                    VelocityY -= 15f;
                    rectangle.Y -= 5;
                    HasJumped = true;
                }
            }
            else
            {
                VelocityX = 0;
            }
            Rectangle msr = new Rectangle(ms.X, ms.Y, 1, 1);
            if(msr.Intersects(this.rectangle)&&ms.LeftButton == ButtonState.Pressed)
            {
                VelocityY = 0f;
                VelocityX = 0f;
                rectangle.X = msr.X;
                rectangle.Y = msr.Y;
            }
            }

        private void Collision()
        {
            foreach (CollisionTile tile in TileMap.TilesRear)
            {
                if(TileCollider.TouchesTop(this.rectangle, tile.rect))
                {
                    VelocityY = 0;
                    HasJumped = false;
                }
                if (TileCollider.TouchesBottom(this.rectangle, tile.rect))
                {
                    VelocityY += 5;
                }
                if (TileCollider.TouchesLeft(this.rectangle, tile.rect))
                {
                    VelocityX = 0;
                }
                if (TileCollider.TouchesRight(this.rectangle, tile.rect))
                {
                    VelocityX = 0;
                }

            }

        }
        public Player(int PosX, int PosY)
        {
            texture = GlobalContainer.TileSet[3];
            rectangle = new Rectangle(PosX, PosY, texture.Width, texture.Height);
            TileMap.DrawingEntities += this.Draw;
            TileMap.Updating += this.Update;
        }
    }
}

正如我已经说过的,玩家只在空中左右移动(不触摸任何瓷砖)。我仍然无法弄清楚问题所在。请帮助我。

XNA 4.0 左右移动播放器不起作用,为什么

if(ks.IsKeyDown(Keys.W))
 {
    if(!HasJumped)
    {
        VelocityY -= 15f;
        rectangle.Y -= 5;
        HasJumped = true;
    }
  }
  else
  {
     VelocityX = 0;  <---- Dont do this
  }

您将VelocityX重置回 0

你可能想要(跳跃时不要向左或向右移动)

 if(ks.IsKeyDown(Keys.W))
 {
    if(!HasJumped)
    {
        VelocityY -= 15f;
        rectangle.Y -= 5;
        HasJumped = true;
    }
     else
    {
        VelocityX = 0; 
    }
}

尝试添加一个 Position vector2 变量,这样就会跟踪玩家的位置,而速度变量会跟踪玩家在给定方向上移动的速度,例如 VelocityY = 玩家的 Y 位置以什么速度变化......

希望这有帮助...GL