Talking about Pixel Purge game, Actionscript 3, Flash and Game development.
Are you playing Pixel Purge?
It’s a good mix between a shooter and an avoider.
I am going to show you how to create a replica of this game, and I’m starting from player movement.
In Pixel Purge, you control a spaceship with arrows or WASD keys in an level that’s as big as twice the visible area of the game. When you move the spaceship, it remains in the center of the stage, with the rest of the game scrolling consequently, until it travels too close to level borders. When it happens, the game itself stops scrolling and the spaceship leaves the center of the stage. Play the game to see what I mean.
This is the feature I am showing in this prototype. It’s quite simple, being a simple concept.
Classes are made for the sake of tutorial understanding, so experienced users may find them a little weird, but they will be improved as soon as I’ll add more elements to the game.
Anyway, this is how things will work: besides the routine to detect pressed keys, we have a background that’s twice as width and height than the movie itself. In this case, the movie is 640×480 and the background is 1280×960 pixels. The player starts in the middle of the background, with x
and y
properties respectively at 640
(1280/2) and 480
(960/2). The background is also centered in the stage, at x
= -320
and y
= -240
.
This way at the very beginning of the game, both the background and the player are perfectly centered on the stage. Then, when the ship moves, in a way very similar to
Flash game creation tutorial – part 1 (with AS3 classes), the background scrolls in the opposite way, keeping the ship centered in the screen, until it gets too close to background borders. Then, the background stops scrolling. There is really nothing new, so I would start with the code:
This is the main class:
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class main extends Sprite {
private var game_container:game_container_mc;
private var left,up,right,down:Boolean;
public function main() {
left=up=right=down=false;
game_container=new game_container_mc();
addChild(game_container);
stage.addEventListener(KeyboardEvent.KEY_DOWN,on_key_down);
stage.addEventListener(KeyboardEvent.KEY_UP,on_key_up);
addEventListener(Event.ENTER_FRAME,on_enter_frame);
}
private function on_enter_frame(e:Event):void {
if (left) {
game_container.player.engine(-1,0)
}
if (right) {
game_container.player.engine(1,0)
}
if (up) {
game_container.player.engine(0,-1)
}
if (down) {
game_container.player.engine(0,1)
}
game_container.player.move_player();
game_container.move_container();
}
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;
}
}
}
}
This is game_container_mc
class, managing the background itself:
package {
import flash.display.Sprite;
public class game_container_mc extends Sprite {
public var player:player_mc;
public function game_container_mc() {
var level_bg:level_bg_mc = new level_bg_mc();
addChild(level_bg);
player=new player_mc();
addChild(player);
x=-320;
y=-240;
}
public function move_container():void {
// scrolling keeping the player centered in game screen
x=320-player.x;
y=240-player.y;
// stop scrolling if the player gets too close to background borders
if (x>0) {
x=0;
}
if (x<640*-1) {
x=-640;
}
if (y>0) {
y=0;
}
if (y<480*-1) {
y=-480;
}
}
}
}
And finally player_mc
class:
package {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
public class player_mc extends Sprite {
private var thrust:Number=1;
private var xspeed,yspeed:Number;
private var friction:Number=0.95;
public function player_mc() {
xspeed=yspeed=0;
x=640;
y=480;
}
public function move_player():void {
xspeed*=friction;
yspeed*=friction;
x+=xspeed;
y+=yspeed;
// preventing spaceship to fly off the stage
if (x>1270) {
x=1270;
}
if (x<10) {
x=10;
}
if (y>950) {
y=950;
}
if (y<10) {
y=10;
}
}
public function engine(x_eng:int,y_eng:int):void {
xspeed+=thrust*x_eng;
yspeed+=thrust*y_eng;
}
}
}
This is the result:
move the "ship" with arrow keys, get close to the edges to see what happens.
Download the source code. If you like this series, next time I'll show you how to fire.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.