Översikt
Detta exempel visar hur man animerar och översätter från logiska till visuella koordinater
Kod
Box.cs
class Box
{
float m_speedX = 1.0f;
internal void Update(Microsoft.Xna.Framework.GameTime gameTime)
{
float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
m_x += elapsedTime * m_speedX;
if (m_x > 1.0f)
{
m_x -= 1.0f;
}
}
public float m_x = 0.0f;
public float m_y = 0.5f;
}
Game1.cs
...
BoxView m_boxView;
Box m_box = new Box();
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
m_box.Update(gameTime);
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
m_boxView.drawBox(m_box);
base.Draw(gameTime);
}
...
BoxView.cs
...
class BoxView
{
private SpriteBatch m_spriteBatch;
private Texture2D m_boxTexture;
private int m_windowWidth;
private int m_windowHeight;
public BoxView(GraphicsDevice graphicsDevice, ContentManager content)
{
m_windowWidth = graphicsDevice.Viewport.Width;
m_windowHeight = graphicsDevice.Viewport.Height;
m_spriteBatch = new SpriteBatch(graphicsDevice);
m_boxTexture = content.Load<Texture2D>("bubbles");
}
internal void drawBox(Box box)
{
int vx = (int)(box.m_x * m_windowWidth);
int vy = (int)(box.m_y * m_windowHeight);
Rectangle destrect = new Rectangle(vx-15, vy-15, 30, 30);
m_spriteBatch.Begin();
m_spriteBatch.Draw(m_boxTexture, destrect, Color.White);
m_spriteBatch.End();
}
}
