So i'm trying to do a chasing sprite from a standstill to moving when I get close enough. Right now I just want the animation to work.
When I compile my code everything loads just fine, but the "enemy-sprite" is standing still and the animation is just flipping out.
What have I missed to do?
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace AnimatedSprites
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class SpriteManager : Microsoft.Xna.Framework.DrawableGameComponent
{
// SpriteBatch for drawing
SpriteBatch spriteBatch;
// A sprite for the player and a list of automated sprites
UserControlledSprite player;
List<Sprite> spriteList = new List<Sprite>();
public SpriteManager(Game game)
: base(game)
{
// TODO: Construct any child components here
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(Game.GraphicsDevice);
player = new UserControlledSprite(
Game.Content.Load<Texture2D>(@"Images/threerings"),
new Vector2(200, 200), new Point(108, 108), 10, new Point(0, 0),
new Point(10, 0), new Vector2(6, 6));
base.LoadContent();
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// Update player
player.Update(gameTime, Game.Window.ClientBounds);
SpawnEnemy();
// Update all sprites
for (int i = 0; i < spriteList.Count; ++i)
{
Sprite s = spriteList;
s.Update(gameTime, Game.Window.ClientBounds);
// Check for collisions
if (s.collisionRect.Intersects(player.collisionRect))
{
// Play collision sound
if (s.collisionCueName != null)
((Game1)Game).PlayCue(s.collisionCueName);
// Remove collided sprite from the game
spriteList.RemoveAt(i);
--i;
}
}
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
// Draw the player
player.Draw(gameTime, spriteBatch);
// Draw all sprites
foreach (Sprite s in spriteList)
s.Draw(gameTime, spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
private void SpawnEnemy()
{
Vector2 speed = new Vector2(1.0f);
Vector2 position = new Vector2(300, 300 );
// Default frame size
Point frameSize = new Point(75, 75);
// Create the sprite
spriteList.Add(
new ChasingSprite(Game.Content.Load<Texture2D>(@"images\skullball"),
position, new Point(192, 192), 10, new Point(0, 0),
new Point(4, 2), speed, "skullcollision", this));
}
// Return current position of the player sprite
public Vector2 GetPlayerPosition()
{
return player.GetPosition;
}
}
}