为什么我的精灵不动
Why does my sprite not move
我第一次尝试使用 类 制作一个简单的 Pong 游戏,
但是在严格按照一些教程进行操作之后,我的代码无法正常工作。即使我确实拥有完全相同的一切。
任何人都可以帮我看看吗?
当我按下 left/right 时,Child Class Ball Update() 应该让球在屏幕上移动。没有错误,但球不会移动。
Class代码:
public class Sprite
{
public Texture2D Texture;
public Rectangle Hitbox;
public KeyboardState KBS = Keyboard.GetState();
public virtual void Update()
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Hitbox, Color.White);
}
}
public class Ball : Sprite
{
public Ball(Texture2D newTexture, Rectangle newHitbox)
{
Texture = newTexture;
Hitbox = newHitbox;
}
public override void Update()
{
if(KBS.IsKeyDown(Keys.Right))
{
Hitbox.X += 3;
}
if (KBS.IsKeyDown(Keys.Left))
{
Hitbox.X -= 3;
}
}
}
主要代码:
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Gameworld
Ball Ball;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Ball = new Ball(Content.Load<Texture2D>("Ball"), new Rectangle(0, 0, 24, 24));
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
Ball.Update();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
Ball.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
因此,如果有人能帮助我,我将不胜感激!
您必须在每个更新周期更新键盘状态。
将其添加到您的 Sprite class,并从 Game1 调用它。
public void Update(GameTime gameTime)
{
KBS = Keyboard.GetState();
}
我第一次尝试使用 类 制作一个简单的 Pong 游戏, 但是在严格按照一些教程进行操作之后,我的代码无法正常工作。即使我确实拥有完全相同的一切。 任何人都可以帮我看看吗? 当我按下 left/right 时,Child Class Ball Update() 应该让球在屏幕上移动。没有错误,但球不会移动。
Class代码:
public class Sprite
{
public Texture2D Texture;
public Rectangle Hitbox;
public KeyboardState KBS = Keyboard.GetState();
public virtual void Update()
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Hitbox, Color.White);
}
}
public class Ball : Sprite
{
public Ball(Texture2D newTexture, Rectangle newHitbox)
{
Texture = newTexture;
Hitbox = newHitbox;
}
public override void Update()
{
if(KBS.IsKeyDown(Keys.Right))
{
Hitbox.X += 3;
}
if (KBS.IsKeyDown(Keys.Left))
{
Hitbox.X -= 3;
}
}
}
主要代码:
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Gameworld
Ball Ball;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Ball = new Ball(Content.Load<Texture2D>("Ball"), new Rectangle(0, 0, 24, 24));
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
Ball.Update();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
Ball.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
因此,如果有人能帮助我,我将不胜感激!
您必须在每个更新周期更新键盘状态。
将其添加到您的 Sprite class,并从 Game1 调用它。
public void Update(GameTime gameTime)
{
KBS = Keyboard.GetState();
}