Sorry if this has been asked before, but I can't seem to find it here. I'm trying to make an asteroids clone in XNA. I took the Game State Management project and am making new classes for things like the player and placing them in the GameplayScreen class. For some reason my ship isn't being draw correctly.
GameplayScreen's Draw method:
public override void Draw(GameTime gameTime)
{
ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
Color.CornflowerBlue, 0, 0);
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
spriteBatch.Begin();
//draw fps
if(showFPS)
spriteBatch.DrawString(framerateFont, "FPS: " + frameRate, new Vector2(15, 15), Color.White);
player.Draw(spriteBatch);
spriteBatch.End();
// If the game is transitioning on or off, fade it out to black.
if (TransitionPosition > 0 || pauseAlpha > 0)
{
float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);
ScreenManager.FadeBackBufferToBlack(alpha);
}
}
And my ship's drawing code:
public PlayerShip(Game game)
{
this.game = game;
Viewport view = game.GraphicsDevice.Viewport;
center = new Vector2(view.Width / 2, view.Height /2);
sourceRectangle = new Rectangle();
Reset(center);
}
public void Reset(Vector2 position)
{
this.position = position;
}
public void LoadContent()
{
ship = game.Content.Load<Texture2D>("ship");
sourceRectangle = new Rectangle(0, 0, ship.Width,
ship.Height);
origin = new Vector2(ship.Width / 2, ship.Height);
}
public void Update(GameTime gameTime)
{
HandleInput();
}
//handles all keyboard input by user. rotates ship based on current rotation.
private void HandleInput()
{
input = Keyboard.GetState();
float x = position.X;
float y = position.Y;
Matrix m = Matrix.CreateRotationZ(rotation);
if (input.IsKeyDown(Keys.Right))
rotation += turnFactor;
else if (input.IsKeyDown(Keys.Left))
rotation -= turnFactor;
else if (input.IsKeyDown(Keys.Up))
{
x += m.M12 * directionFactor;
y -= m.M11 * directionFactor;
}
else if (input.IsKeyDown(Keys.Down))
{
x -= m.M12 * directionFactor;
y += m.M11 * directionFactor;
}
position.X = x;
position.Y = y;
Console.WriteLine("Y: {0} ", y);
Console.WriteLine("X: {0} ", x);
}
public void Draw(SpriteBatch sb)
{
Vector2 origin = new Vector2(ship.Width / 2, ship.Height);
sb.Draw(ship, ship.Bounds, null, Color.White, rotation, origin, SpriteEffects.None, 0f);
}
I hooked the project up to a console window and it seems that the X and Y positions are updating correctly code wise, but not visually. It seems to update where it is rotationally, but going up or down doesn't draw correctly.
Image of what it's doing http://imgur.com/4cKBk