Talking about Pixel Purge game, Actionscript 3, Flash and Game development.
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.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.