Public Group

# Xna programming problem with drawing sprites

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

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
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."

##### Share on other sites
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

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
12

• 9
• 21
• 13
• 9
• 17
• ### Forum Statistics

• Total Topics
632608
• Total Posts
3007391

×