Sign in to follow this  
DarkZlayer

Managing Many Sprites

Recommended Posts

Well, For a few days now I have been trying to think of a way inwhich I can manage many sprites of the same image. I have a book that shows a way to use tilesets (which definately helps make code easier to read\type) and sometimes I find myself using the same sprite but since using the tileset causes them to have the same name it moves\changes both of them. Now an easy but tedious way to go about it would be to obviously create many of that object but with different names. To me that seems like it is way too much work and there has to be an easier way. The type of thing I am trying to do is a game in which I will have to control many of the same type of sprite and it wouldn't be good if they all moved at the same time. I really don't have any code I can\should post because I am completely clueless on an effective way to do this so I am hoping you guys can help with me a good way. ~Thanks

Share this post


Link to post
Share on other sites
You've already realised that you can have the same image being used by different sprites. If the image is the same, then what's different?


  • Position - either on the screen, or in the game world
  • Size - width and height
  • Current animation and current frame within that animation


That's three to start you off; there may be others (transparency, for example). Once you've figured it all out, the list you're looking at is the data that defines an individual sprite. You can have as many copies of that data as you like, without having multiple copies of the actual image. More to the point, you need to have multiple copies of that data, because if two sprites share the same data then they'll appear to the player to be the same sprite. In order to draw two seperate units you need to have two seperate chunks of data.

In C or C++, you could describe this data using a structure:


struct Sprite
{
int X, Y; // position
int Width, Height; // size
int AnimationID;
int FrameNumber;
};

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It is in fact pretty easy to solve...

Just split the "resource" from the "entity". What i mean is: the tileset/image is just a resource, the entity is representing something/someone in the world.

So, simply create 2 classes: 1 derived from resource named Image/Tileset/wahtever, then create a entity class..

The entity class is managing the position, orientity, state... its managing the actual thing in the world.

The resource class is managing the actual loading/reading of the data.

That what, u call call on the entity, which in turn will setup transformations matrix (or simple offsetting) than the drawing method will simply ask the resource to draw itself to the target.

Doing thins on a list of entity will surely allow them to be "seperate" entity.

There's alot of ways of solving this, i think this one is one of the simpliest.

Good Luck,
Jonathan

Share this post


Link to post
Share on other sites
Thanks that helps a lot. Now if you wouldn't mind answering one other question it would be appreicated. With that information I will now be able to do it all but there is one problem.

Say I wanted to create two new units while the game is running. The only possible way I can think of is to do this (if using the same structure as superpig) is something like: (I am using C# by the way but I understand what you mean)

Sprite unit = new Sprite(int x, int y, int height, int width, etc)

Now say I wanted to create two new sprites I would end up having two things with the same name therefore the same thing. How would I be able to create two thing with predictable names so I can use them while the game is running without having to put in all of that stuff before it is running. Yes, I realize this is most likely something simple to overcome but I never learned how to and I cannot think of keywords to type in on Google to search it so I had to ask here.

Share this post


Link to post
Share on other sites
Oh, you mean how to store them?

What you want is something called a "container," that can store many objects. A simple array is a kind of container, but you would probably be better off with one of the containers in System.Collections.Generic - something like List<>. List<> is an object that you can add and remove individual objects to whenever you want.

Then, when it comes to rendering your sprites, you'd use a "foreach" loop to process all the entries in the list and render them all.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this