I just finished one of MJW's sh'm'up tutorials and by the end I had multiple lines that were duplicates of each other. For example:
While this used to be okay - I just wanted working code! - Now, after these tutorials I'm spoiled. Some would say a better (though very beginner!) programmer. I feel like I need to compact it and clean it up as much as possible.
So I created a public function called removeClip (first it was removeEnemy, then I figured I could use it for the bullets too, and maybe other things later!) and passed it three variables:
Then I just went back through AvoiderGame.as and replaced all those lines that removed the bullet / enemy with:
removeClip( enemy, i, army );
removeClip( bullet, j, bullets );
Michael, thanks for teaching us how to code.
EDIT: MJW posted a comment on trimming down my code even more that I'd like to share! (Especially in case you don't read the comments...)
He wrote: "I'm not sure if you know this, but all arrays have a method, .indexOf( item ), that returns the index of the item specified (or -1 if the item is not in the array).
So you can write "array.splice( array.indexOf( movieClip ), 1 )", for example. This way you don't have to pass the index of the item through to the function each time!"
I've used his suggestion, and changed my removeClip function to this:
I guess if you wanted, you could also make a check to make sure it was part of that array before trying to splice, like this:
Anyways, it might not seem like a lot at first. You're only slimming it down by one small variable. But that's one less variable for every bullet that leaves the screen, or hits an enemy, plus every enemy that gets hit, or leaves the screen, and in a much bigger game it would really start adding up.
A little more information about it here from Senocular