Create a Flash game like Pixel Purge step 3 – Fire rate and spread

As promised, here I am with the 3rd part of this tutorial, showing you how to manage fire rate and multiple bullets. Moreover, I made some changes to bullet_mc class following the suggestions Harack left in the comments of step 2.

To manage fire rate, we need to set up a timer each time the player fires a bullet. Then, while such timer is active, the player won’t be able to fire even if he keeps pressing left mouse button. To deal with fire spread, we’ll simply fire bullets at different angles.

These are the new variables used in this script:

private var fire_delay:uint=250;: represents the delay, in milliseconds, between a bullet and another

private var delay:Timer=new Timer(fire_delay);: this is the timer which will handle the fire delay. If you want to have more information about AS3 Timer class, refer to Understanding AS3 Timer Class.

private var delay_completed:Boolean=true;: a boolean variable used to inform us the delay between a bullet and the next one to be fired is over. If it’s true, you can fire again.

private var bullets_per_time:uint=3;: this variable will tell the script how many bullets will be fired at once.

private var bullets_angle=10*0.0174532925;: and this is the difference angle between a bullet and another, in radians. 0.0174532925 is what converts degrees to radians.

Now the main class changes this way:

The interesting part is the one which manages the firing: at line 58 you can see the player can fire if firing is true (mouse button is pressed) and delay_completed is true too (at least fire_delay milliseconds passed since the last bullet has been fired).

Then delay_completed is set to false (line 60) and a listener is created to count fire_delay milliseconds (lines 61 and 62).

on_delay_over function at lines 72-76 sets delay_completed to true again and removes the listener.

At lines 63-68 firing x and y speeds are calculated before passing them to bullet_mc class, according to Harack’s suggestions, and the spread angle is determined in a way similar to the rays of light you encountered in Create a survival horror game in Flash – AS3 version. And there’s nothing more to say, since the rest of the script is just a collection of old concepts you can find here and there in the blog.

And this is bullet_mc class. As you can see, no more trigonometry in it.

This is the result:

Move the ship with arrow keys, aim and fire with the mouse.

Download the source code. Next step, the enemy!!

  • shawn

    great sample~

  • Thank you for sharing, I had been following the previous post, so I was waiting for this one to try the game! I love this code stuffsssss.

    Great post

  • Dear Emanuele, thank you for this article and interesting lesson. Could you please say, what plugin do you use to publish AS code in article? Thank you in advance.

  • emanuele I’m disappointed! adding an event listener to each bullet? they should be stored in a Vector and updated through a loop – what if you want to pause the game or remove them all? time to teach your readers something new ;)

  • Dan

    Great tutorial as always :). Can’t wait for the next one.

  • Heya, I’m Porter, programmer of Pixel Purge. Neat to see you’re doing a post on how to create one of my games, definitely a compliment. While I do feel you can’t possibly create something as polished as Pixel Purge quickly, you can definitely get the fundamentals down, which is what you’re supplying a nice tutorial for. I too address concern with the bullets though, that’s a lot of event listeners, Vector is the way to go. I’m eager to see how well your final version can handle a full screen of enemies, explosions and bullets. We spent a lot of time rewriting our framework to handle such loads, so I find the optimizing you’ll have to do very interesting. Keep up the tutorial, looking forward to seeing the finished product :) See if you can beat me to the completion of Pixel Purge 2 :D

  • BobTheCoolGuy

    Nice to hear from the creator of the game. I would be interested in seeing it done with a vector also, because I don’t really know what they are in the Actionscript sense. It would be as someone said something helpful and new to learn.

  • Grazie Emanuele,

    I tuoi tutorial sono veramente esaustivi, mi stanno aiutando ad essere produttivo nella produzione di giochi in tempi record nonostante non usi flash! Sto anche leggendo con piacere il tuo libro! Aspetto con ansia il prossimo post di questa serie.

  • Arreador

    Hi, emanuele, are you planning to finish this tutorial? I really wait the enemys part, i`m working in a game similar to this, any help with the enemys waves??

  • theblob

    Great series. I’m really hoping to see a fourth part soon :) – Thanks alot emanuele

  • anouvis

    Hi, emanuele, great series i’m really hoping that you planning to finish this tutorial. I really wait the enemys part!!!