Create a Flash game like Pixel Purge step 2 – Firing bullets

In step 1 I showed you how to move your spaceship, now it’s time to fire some bullets.

First, we need to more objects: the crosshair, called crosshair_mc, and the bullet itself, called bullet_mc.

Firing is very easy, as all you need to do is keeping the mouse button pressed. In the original game there is also a fire ratio and a different amount of bullets fired at the same time, but we’ll see these features later, when we’ll manage powerups.

At the moment, main class changes this way:

firing variable will decide whether I am firing or not and at lines 51-56 will generate the bullets.

bullet_mc constructor works with three arguments: x position, y position and the angle of shooting, calculated with trigonometry.

bullet_mc class also uses trigonometry to make the bullet fly:

There’s nothing you haven’t already seen in the blog, just combined in a different way.

This is the result:

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

Download the source code.

  • Me

    Pretty cool so far, thanks!

  • If your giving each bullet an on enter frame function its best to minimise calculations within that function.

    Cos and sin each function with 100+ bullets may experience lag for some systems.

    remove this from on enter frame:
    x-=speed*Math.cos(shooting_angle);
    y-=speed*Math.sin(shooting_angle);

    add the datafields xSpeed and ySpeed.
    Do the calculation and declaration within the constructor and just call x -= xSpeed; y -= ySpeed;

    Having multiple enter frame loops does also effect performance within the long run especially if they all have calculations such as sin,cos,tan,atan even hitTestPoint. I think you are better off just using one in the parent that has a function call to handleBullets() which could loop through your bullets vector or array (vectors are faster) and call the moveBullet() function on it.

    -Harack

  • Pingback: Swf.hu 2.0 – flash és webfejlesztés » Heti linkajánló – 2010/40()

  • Greg

    nice post I will use this in a flash game one day
    i also have a request for a tutorial which uses the flash addon FZip which can access ZIP files im not sure what this can be used for and how to use it so maybe you can try it out

  • plasmacannon

    :( unexpected file format

  • mc

    Emanuele, Do you know an efficient method to store levels
    so users can create and share custom levels without using databases?

    For example, the users can export the custom level in some short text code where you just need to copy&paste the level data to play it… or by email like greeting cards. Is that possible?
    (being the main problem truncated long url data if they share the code by GET method)

  • Flash Survival Game

    I really want this moving AC for my game,but please tell witch Flash Ver. are u using,and what piece of AC belongs to witch part?

  • heliumdream

    @mc

    i use xml files many times to house two dimensional arrays, which correspond to game maps. you should be able to add functionality to a flash game that would allow a user to edit maps, and save them locally as a xml file. later your flash game can have a user browse to this xml file to load it as a map.

  • Pingback: Create a Flash game like Pixel Purge step 3 – Fire rate and spread - Emanuele Feronato()

  • The game I would like to learn about is Bloons Tower Defense. They have 4 out now, and have been very successful. :)