Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XNA 4.0左右移动播放器不起作用,为什么?_C#_Xna - Fatal编程技术网

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

C# XNA 4.0左右移动播放器不起作用,为什么?,c#,xna,C#,Xna,我跟随YouTube上OYOU91的教程,制作了一个小的基于瓷砖的跳跃跑步游戏。 然而,每次我试图左右移动球员时,他就是不动。他跳跃时只向左或向右移动。在那之后,我完全重做了所有的事情,就像他(OYOU91)做的那样,当球员站在地上时,他仍然不会动。 以下是静态TileCollider类: using Microsoft.Xna.Framework; using System; using System.Collections.Generic; using System.Linq; using

我跟随YouTube上OYOU91的教程,制作了一个小的基于瓷砖的跳跃跑步游戏。 然而,每次我试图左右移动球员时,他就是不动。他跳跃时只向左或向右移动。在那之后,我完全重做了所有的事情,就像他(OYOU91)做的那样,当球员站在地上时,他仍然不会动。 以下是静态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;
            } 
        }




    }
}
使用Microsoft.Xna.Framework;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间XJump
{
公共静态类TileCollider
{
公共静态bool TouchesTop(矩形rect1、矩形rect2)
{
如果((rect1.Bottom>=rect2.Top-1)&&(rect1.Bottom=rect2.Left+rect2.Width/5)&(rect1.Left=rect2.Left+(rect2.Width/5))&&
(rect1.Left=rect2.Bottom-(rect2.Width/4))&&
(rect1.Bottom=rect2.Left)&&
(rect1.Left=rect2.Bottom-(rect2.Width/4))&&

(rect1.Bottom尝试添加位置向量2变量,这样可以跟踪玩家的位置,而速度变量将跟踪玩家在给定方向上移动的速度,例如VelocityY=玩家Y位置的变化速度是多少


希望这有帮助…GL

小提示:与其做
如果(x)返回true;否则返回false;
,不如做
返回x;
@Daniel Wanner,如果这回答了你的问题,你应该这样标记。首先,这将帮助其他与你有同样问题的人。
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;
        }

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