[disc] 6 ways to kill an enemy or: how to code a bullet
hi folks,
for the game I'm currently working on, i needed some bullets to fly around
and do some damage.
but how to code a bullet?
easy, you might say, though it is easy, but is it the best way?
while thinking about it i came across some different methods to do the same thing: letting the bullet fly into a certain direction and either hit a sprite, a wall or just "fade away".
i'm quite used to software abstraction, so i tried to narrow down the behaviors of the different bullet-types to the basic needs in order to have only one class for all bullets. (player / enemy 1 / enemy 2 / ...)
method a)
- create an MC in the library, set the linkage AND an AS2 class.
- the class would be an extension of the MovieClip class, so i could override the onEnterFrame event and init the class with an object to set the start values (read about it on LiveDocs: http://livedocs.macromedia.com/flash...=00001336.html)
- pro:
-- very straight forward method
-- every needed method can be contained in the Bullet class
-- could overwrite onEnterFrame to make it move and a simple hittest to check for collisions
-- attach and go ...
-- no loops needed to move bullets
- contra:
-- do you really want to use one onEnterFrame for each bullet?
-- need to keep track of new bullets depth
-- need to keep track of bullets in game
-- each bullet must be aware of it's surroundings "game world"
method b)
- same as a, but using an ASBroadcaster instead of the oEFs
- create an "onMove" event in the bullet class that gets fired by the ASBroadcaster
- pro:
-- same as (a)
-- still straight forward
-- still no loops needed
- contra:
-- same as (a)
-- the need to remove unused bullets from the ASBroadcaster or keep track of "used" bullets to reuse them
method c)
- create a bullet manager class that handle ALL bullets in the game
- pro:
-- no need to worry about bullets in the game
-- the manager would keep track of depths /counts and so on
-- could use either a loop to move bullets or use the ASBroadcaster
-- all bullets would be simple objects with a reference to the MC and the needed values
-- since the bullets would be stored in an array they could be added and removed without problems
-- reusable for future project
- contra:
-- quite a lot more work to write the manager, since it needs to be more
flexible than a single bullet
-- is it needed?
-- extra parameters for different bullet types (or extra code)
method d)
- a mix of a/b and c
method f)
- keep track of the bullet inside the player/enemy object
- pro
-- very straight forward
-- easy to keep track of "bullets per object"
-- easy to combine the movement method with the object's "action" event
- contra
-- not very "clean" separation of the game's objects (player, enemies)
-- extra bullet handling code per object needed (eg. different enemies)
i came across some more, but i thought this might be enough for a serious discussion.
so what do you guys use/think?
nGFX
ps: i have already found my method of choice and i will tell you somewhere later in this thread (if it starts at all), because i don't want to "preselect" an idea for this discussion.