-3

I followed the Tutorials of Oyyou91 on YouTube, to make a little tile based Jump and Run Game. However, everytime i tried to move the player left or right, he just wont move. He only moves left or Right, when hes Jumping. After that, i completly redid everything and did it the same way he (Oyyou91) did, and the player still wont move when hes standing on the ground. Here is the static TileCollider class:

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




    }
}

and here is my Player class:

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

    }
}

As i already Said, the player only moves left and Right when in air(not touching any Tile). I still cant figure out the problem. Please help me.

4

2 回答 2

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; 
    }
}
于 2013-09-06T18:21:47.660 回答
0

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

希望这会有所帮助... GL

于 2013-09-09T21:54:12.893 回答