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

时间:2013-09-06 18:19:00

标签: c# xna

我在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;
            } 
        }




    }
}

这是我的Player类:

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

    }
}

正如我已经说过的那样,玩家只能在空中向左和向右移动(不接触任何平铺)。我仍然无法弄清楚这个问题。请帮帮我。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

尝试添加一个位置矢量2变量,这样就可以跟踪玩家的位置,而速度变量将跟踪玩家在给定方向上移动的速度,例如: VelocityY =玩家Y位置改变的速度......

希望这有助于...... GL

相关问题