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

Read all posts about "" game

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:

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:

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers