gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Controlling Multipal Sprites...

#8773 - Snoobab - Sat Jul 19, 2003 1:50 pm

Hi. I'm having a bit of trouble with a game I'm working on.

At the moment my main space ship can shoot missiles, the problem is it can only shoot one at a time. i.e. only one missile can be on screen at once. If I push the fire button twice quickly the same missile is re-spawned at its original launch possition. What I would really like is more than one missiles to be on screen at once. How would I do this ? How would I keep track of the different missiles ? Would a struct containing all the missile properties(position, velicity, id?) be best ? Any ideas ?

Thanks,

- Ed.

#8777 - tepples - Sat Jul 19, 2003 4:36 pm

Yes, make a struct containing missile properties. Then make an array of Missile. Then give each Missile a state variable: dead, live, exploding. When the player presses the fire button, look for the first dead Missile, but don't overflow the buffer.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#8781 - Cyberman - Sat Jul 19, 2003 6:23 pm

If you are using C++ You might wish to create a linked list of Missiles.

Code:

class Missile
{
 ...
}

class MissileList
{
 MissileList(Missile *);
 ~MissileList();
 Missile * MyMissile
 MissileList *Next;
 Missile &operator[](int Index);
 int Add(Missile *);
 void Delete(int);
 int Count();
}

class SpaceShip
{
...
 private:
  MissileList *Fired;
}


You fire a missile by doing

Code:

FiredMissile = new Missile(/* whatever parameters you need to make a missile*/);
Fired->Add(FiredMissile);

You update the missile status on the display by doing.
Code:

// start from the oldest to the newest missile
for (Index = Fired->Count() - 1; Index >=0; Index--)
{
 // check if the missile died someone
 if ((*Fired)[Index].Update())
 {
  // the missile is dead remove it
  Fired->Delete(Index);
 }
}

The Add() function inserts the new missile at the begining of the Missile list. Then Update returns true if the missile exploded drifted off into space or whatever (it's dead in other words) the missile is then deleted and removed from the list.

Update handles any animation change in orientation change in position or exploding needed for the missile as well.

Of course the code inside the objects will be more complicated but this should keep things at the high level simple which in my experience makes the whole thing simpler :) (well more often it does LOL).

Cyb