Jump to content
  • Advertisement
Sign in to follow this  
AndersEkdahl

Chasing sprite - not working

This topic is 2497 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello.
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;
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
I recon this is source code from "Learn XNA 3.0", is that right?

Let me see if I understand your problem: So what you have for now is when you touch a sprite, it dies upon collision, and you are saying that for now you want the sprite animation to work, without moving, and it doesn't work? Is this correct?

Share this post


Link to post
Share on other sites
It's from learning XNA 4.0, but it's the same code ^^

You are correct, the sprite is there. But it seems to draw sprites over and over again. I can see the sprite moving but like I said, looks like it is a couple of sprites on top of eachother.

Share this post


Link to post
Share on other sites
It looks like you have layers of sprites as you move around on the screen?

If you're doing all of the drawing directly in your SpriteManager class then be sure to clear the buffer in your Draw() function such as

public override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue); //this is missing from your source code so the last buffer's contents are still being drawn.
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);
}

Share this post


Link to post
Share on other sites
I see.

Taking another look you have your SpawnEnemy() call inside your Update() function so you're going to spawn a new enemy every update tick which should explain the 'layering' you're describing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!