Install Circle Chain on your iPhone for free and get the source code!! 645 downloads to go - last updated: April 21, 2012

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	public class main extends Sprite {
		private var game_container:game_container_mc;
		private var player:player_mc;
		private var left,up,right,down:Boolean;
		private var crosshair:crosshair_mc;
		private var firing:Boolean=false;
		private var bullet:bullet_mc;
		public function main() {
			left=up=right=down=false;
			game_container=new game_container_mc();
			addChild(game_container);
			player=new player_mc();
			game_container.addChild(player);
			crosshair=new crosshair_mc();
			addChild(crosshair);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,on_key_down);
			stage.addEventListener(KeyboardEvent.KEY_UP,on_key_up);
			addEventListener(Event.ENTER_FRAME,on_enter_frame);
			addEventListener(MouseEvent.MOUSE_DOWN,on_mouse_down);
			addEventListener(MouseEvent.MOUSE_UP,on_mouse_up);
		}
		private function on_mouse_down(e:MouseEvent):void {
			firing=true;
		}
		private function on_mouse_up(e:MouseEvent):void {
			firing=false;
		}
		private function on_enter_frame(e:Event):void {
			crosshair.x=mouseX;
			crosshair.y=mouseY;
			if (left) {
				player.engine(-1,0);
			}
			if (right) {
				player.engine(1,0);
			}
			if (up) {
				player.engine(0,-1);
			}
			if (down) {
				player.engine(0,1);
			}
			player.move_player();
			game_container.move_container(player.x,player.y);
			if (firing) {
				var dist_x:Number=player.x+game_container.x-mouseX;
				var dist_y:Number=player.y+game_container.y-mouseY;
				bullet=new bullet_mc(player.x,player.y,Math.atan2(dist_y,dist_x));
				game_container.addChild(bullet);
			}
		}
		private function on_key_down(e:KeyboardEvent):void {
			switch (e.keyCode) {
				case 37 ://left
					left=true;
					break;
				case 38 :// up
					up=true;
					break;
				case 39 ://right
					right=true;
					break;
				case 40 ://down
					down=true;
					break;
			}
		}
		private function on_key_up(e:KeyboardEvent):void {
			switch (e.keyCode) {
				case 37 ://left
					left=false;
					break;
				case 38 :// up
					up=false;
					break;
				case 39 ://right
					right=false;
					break;
				case 40 ://down
					down=false;
					break;
			}
		}
		public function remove_bullet(b:bullet_mc):void {
			game_container.removeChild(b);
			b=null;
		}
	}
}

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package {
	import flash.display.Sprite;
	import flash.events.Event;
	public class bullet_mc extends Sprite {
		private var speed:uint=20;
		private var shooting_angle:Number;
		public function bullet_mc(px:int,py:int,angle:Number) {
			x=px;
			y=py;
			shooting_angle=angle;
			addEventListener(Event.ENTER_FRAME,on_enter_frame);
		}
		private function on_enter_frame(e:Event) {
			x-=speed*Math.cos(shooting_angle);
			y-=speed*Math.sin(shooting_angle);
			if (x>1280||x<0||y>960||y<0) {
				removeEventListener(Event.ENTER_FRAME,on_enter_frame);
				var par:main= this.parent.parent as main
				par.remove_bullet(this);
			}
		}
	}
}

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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (13 votes, average: 4.46 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 10 comments

  1. Me

    on September 28, 2010 at 7:13 pm

    Pretty cool so far, thanks!

  2. Harack

    on September 29, 2010 at 3:41 am

    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

  3. Swf.hu 2.0 – flash és webfejlesztés » Heti linkajánló – 2010/40

    on October 4, 2010 at 3:35 am

    [...] Create a Flash game like Pixel Purge step 2 – Firing bullets Az el?z? linkajánlóban említett ?rbéli lövöldöz?s játék második része jelent meg a héten ami már a lövedékekkel foglakozik. [...]

  4. Greg

    on October 4, 2010 at 5:44 am

    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

  5. plasmacannon

    on October 6, 2010 at 3:57 am

    :( unexpected file format

  6. mc

    on October 6, 2010 at 7:39 am

    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)

  7. Flash Survival Game

    on October 9, 2010 at 4:34 pm

    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?

  8. heliumdream

    on October 10, 2010 at 8:56 pm

    @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.

  9. Create a Flash game like Pixel Purge step 3 – Fire rate and spread - Emanuele Feronato

    on October 11, 2010 at 11:43 pm

    [...] 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. [...]

  10. Kris Foster

    on December 23, 2010 at 4:57 am

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