好的,所以我正在制作一个 2d 游戏,我也是新的病态复制/粘贴我的整个 Game.cs 脚本除了 Program.cs 之外,我的项目中没有其他分类
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
bool hasJumped = true;
Vector2 velocity;
Texture2D player;
Texture2D ground1;
Vector2 playerPosition = new Vector2(30, 30);
Vector2 ground1p1 = new Vector2(0,430);
Vector2 ground1p2 = new Vector2(200,430);
Vector2 ground1p3 = new Vector2(0, 310);
Vector2 ground1p4 = new Vector2(200, 310);
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
player = Content.Load<Texture2D>("Player");
ground1 = Content.Load<Texture2D>("Ground1");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
KeyboardState newState = Keyboard.GetState();
if ((playerPosition.Y > 0) && (playerPosition.Y < graphics.GraphicsDevice.Viewport.Height-player.Height))
{
playerPosition += velocity;
}
if (Keyboard.GetState().IsKeyDown(Keys.Right)&&playerPosition.X <=(graphics.GraphicsDevice.Viewport.Width-player.Width))
{
velocity.X = 3f;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Left) && (playerPosition.X >= 0))
{
velocity.X = -3f;
}
else velocity.X = 0f;
if (Keyboard.GetState().IsKeyDown(Keys.Up)&& hasJumped==false)
{
playerPosition.Y -= 10f;
velocity.Y = -4f;
hasJumped = true;
}
if (hasJumped==true)
{
velocity.Y += 0.10f;
}
if (DetectPlayerAndGround1Collision2(playerPosition,ground1p1,player,ground1) == true)
{
hasJumped = false;
velocity.Y = 0f;
}
if (DetectPlayerAndGround1Collision2(playerPosition, ground1p2, player, ground1) == true)
{
hasJumped = false;
velocity.Y = 0f;
}
if (hasJumped == false)
{
velocity.Y = 0f;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(ground1,ground1p1 , Color.White);
spriteBatch.Draw(ground1, ground1p2, Color.White);
spriteBatch.Draw(ground1, ground1p4, Color.White);
spriteBatch.Draw(player, playerPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
public Boolean DetectPlayerAndGround1Collision2(Vector2 playerPositionM,Vector2 groundPositionM,Texture2D playerM,Texture2D groundM)
{
if (playerPositionM.Y <= groundPositionM.Y + groundM.Height && playerPositionM.Y > groundPositionM.Y)
{
if (playerPositionM.X < groundPositionM.X && (playerPositionM.X + playerM.Width < groundPositionM.X + groundM.Width && playerPositionM.X + playerM.Width > groundPositionM.X)) { return true; }
else if (playerPositionM.X + playerM.Width > groundPositionM.X + groundM.Width && (playerPositionM.X < groundPositionM.X + groundM.Width && playerPositionM.X > groundPositionM.X)) { return true; }
else if ((playerPositionM.X > groundPositionM.X && playerPositionM.X < groundPositionM.X + groundM.Width) && (playerPositionM.X + playerM.Width < groundPositionM.X + groundM.Width && playerPositionM.X + playerM.Width > groundPositionM.X)) { return true; }
}
}
}
有人能告诉我为什么当我在它的边缘时我从我的“ground1”精灵中掉下来吗?我完全不知道我的剧本有多么错误我最后的希望是问一个比我更熟练的人,或者我可能是盲人,我看不到什么愚蠢的谢谢