Xna programming problem with drawing sprites

Started by
2 comments, last by writtensouls 16 years, 10 months ago
Hello, My problem is this... I want to draw one character leaving images of all the locations he been and i want to draw another character that moves around without leaving images in the spots he was. If i use the graphicsdevice clear method it does it without images but then it doesnt let me draw another sprite without being cleared. How can I do this? Thanks for looking Anthony out
Advertisement
So, obviously you'll want to redraw the past positions of the character, and for that you'll have to remember the previous positions of the character.

I suggest keeping a queue of positions for some finite amount, say the last 15 positions or so. Each frame, draw the positions from oldest to newest. When you move the character, push his position on the queue, if its beyond 15 positions, delete the oldest one.

You can keep more or fewer than 15, but you'll have to pick some number, you can't just have infinite previous positions for obvious memory reasons.


If you *really* need no limit, then using a Dirty-rectangle scheme, where the one character never cleans up after himself, might be more what you're looking for.

throw table_exception("(? ???)? ? ???");

I'd agree that you should limit how many past positions you want to draw. That said, here's how you could do some sort of infinite (relatively) drawing:

class Game1 : Microsoft.Xna.Framework.Game{   List<Vector2> oldPositions = new List<Vector2>();  //all sorts of your stuff in here   public void Update(GameTime gameTime)   {      //update all your stuff here like the position and whatnot      oldPositions.Add(mySprite.Position); //or however you store the position   }   public void Draw(GameTime gameTime)   {      for (int i = 0; i < oldPositions.Count; i++)         spriteBatch.Draw(yourTexture, oldPositions); //obviously making this drawn like your other sprite   }}


That will store them until you run out of memory and crash or until you're just drawing too much on the screen. It should draw them correctly as things are added to the end of the list and this would start with the first thing that was added.

If you wanted to limit the number of them saved, you could add the following couple of lines right after the oldPositions.Add() call:

if (oldPositions.Count > 15) // or whatever number you want
oldPositions.RemoveAt(0);

Note that this isn't the most efficient way to keep the number down, but it's a decent enough start for a basic implementation. If you were going to do a limited size list, it'd probably be better to just use an array of Vector2s and do the allocation of the maximum number you want to store. But give this a try or look up stuff on using the array as a "challenge."
thanks i came up with that idea too before reading but when i tried to use list of vectors as the position it said invalid data types or something. I probably did something wrong though. Thanks

This topic is closed to new replies.

Advertisement