multiple enemies
hey,
I've bee thinking for a while about how to handle a variable amount of objects (in my case enemies) in a game. I refuse to google it just to make sure I can find a solution by myself.
so this is what I came up with:
I make a class "enemy". In this class I put a pointer to an other enemy, lets call it enemy.pointer.
So when I have to add an enemy, I use new and put it in the empty location my last enemy.pointer is pointing to.
For removing an enemy:
enemy1.pointer points to enemy2 and enemy2.pointer points to enemy3
to remove enemy2 i make enemy1.pointer point to enemy 3 and use delete on enemy 2.
if (this==crap)
{don't tell me a better way.}
else
{any further hints are welcome}
thanks, TH
Quote:Original post by twoaterisnIt's not crap :-) But depending on what language you're using, there are probably better ways. As you requested I won't be specific, but I will say that you're reinventing a wheel here (specifically a very common data structure). In general, implementing this particular structure from scratch can be somewhat error-prone, but it can be a good exercise as well.
hey,
I've bee thinking for a while about how to handle a variable amount of objects (in my case enemies) in a game. I refuse to google it just to make sure I can find a solution by myself.
so this is what I came up with:
I make a class "enemy". In this class I put a pointer to an other enemy, lets call it enemy.pointer.
So when I have to add an enemy, I use new and put it in the empty location my last enemy.pointer is pointing to.
For removing an enemy:
enemy1.pointer points to enemy2 and enemy2.pointer points to enemy3
to remove enemy2 i make enemy1.pointer point to enemy 3 and use delete on enemy 2.
if (this==crap)
{don't tell me a better way.}
else
{any further hints are welcome}
Quote:Original post by twoaterisn
hey,
I've bee thinking for a while about how to handle a variable amount of objects (in my case enemies) in a game. I refuse to google it just to make sure I can find a solution by myself.
so this is what I came up with:
I make a class "enemy". In this class I put a pointer to an other enemy, lets call it enemy.pointer.
So when I have to add an enemy, I use new and put it in the empty location my last enemy.pointer is pointing to.
For removing an enemy:
enemy1.pointer points to enemy2 and enemy2.pointer points to enemy3
to remove enemy2 i make enemy1.pointer point to enemy 3 and use delete on enemy 2.
if (this==crap)
{don't tell me a better way.}
else
{any further hints are welcome}
thanks, TH
It's called a linked list if you want to research this further. It is also implemented in the STL as std::list if you are using C++.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement