theARTboy
where art meets design
Platform game with Flash
Categories: flash

This started as a basic platform style game mechanic. I then worked to make the background scroll as the character reached near the edges. It became apparent that something needed to happen when the character hit the ground. In the interest of simplicity, I didn’t make a separate particles class, which would make cleaner code.

Click to the game area and then use the arrow keys to move and jump.



Platform Demo

The meat of the operation in effect so far is:
[as3]
package {

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.display.DisplayObject;
import flash.events.MouseEvent;

public class Main extends MovieClip {

private var guy:MovieClip;
private var LEFT:Boolean;
private var RIGHT:Boolean;
private var UP:Boolean;
private var grav:Number;
private var friction:Number;
private var speedX:Number;
private var speedY:Number;
private var maxSpeedY:Number;
private var connected:Boolean;
private var tempPlatform:Platform;
private var platformArray:Array;
private var particles:Array;
private var tempParticle:Particle;
private var dx:Number;
private var dy:Number;
private var again:MovieClip;

public function Main() {
// constructor code
init();
}
private function init():void {
again=new Again();
again.x=stage.stageWidth*0.5;
again.y=100;
grav = 0.9;
friction=0.3;
speedX = 0;
speedY = -8;
maxSpeedY = 8;
LEFT = false;
RIGHT = false;
UP = false;
connected = true;
platformArray=new Array();
particles=new Array();
guy=new Guy();
addChild(guy);
guy.x = 300;
guy.y = 100;
createPlatforms();
stage.addEventListener(KeyboardEvent.KEY_DOWN,kd);
stage.addEventListener(KeyboardEvent.KEY_UP,ku);
addEventListener(Event.ENTER_FRAME,gameLoop);
}
private function createPlatforms():void {
for (var j:int=0; j<20; j++) {
var platform:MovieClip = new Platform();
platform.x = platform.width * j+50*j+10;
platform.y = 300;
platformArray.push(platform);
addChild(platform);
}
}
private function gameLoop(e:Event):void {
checkCollisionPlatform();
if (LEFT && connected) {
speedX = -5;
}
if (RIGHT&&connected) {
speedX = 5;
}
if (UP && connected) {
connected = false;
speedY = -12;
}
if (! connected) {
guy.y += speedY;
if (speedY < maxSpeedY) {
speedY += grav;
} else {
speedY = maxSpeedY;
}
}else{
//apply friction
if (speedX > .5) {
speedX -= friction;
} else if (speedX<-(.5)) {
speedX += friction;
} else {
speedX = 0;
}
}
//final updates of objects
if(guy.x<=100||guy.x>=450){
for(var i:int=platformArray.length-1;i>=0;i–){
platformArray[i].x-=speedX;
}
}
guy.x += speedX;
if (guy.x<100){guy.x=100;}
if (guy.x>450){guy.x=450;}
checkBottom();
}
private function checkBottom():void{
if(guy.y>=stage.stageHeight){
removeEventListener(Event.ENTER_FRAME,gameLoop);
explode(guy.x);
guy.parent.removeChild(guy);
}
}
private function explode(X:Number):void{
for(var i:int=200;i>=0;i–){
var particle=new Particle();
particle.dx=Math.random()*10-5;
particle.dy=Math.random()*(-10);
particle.x=X;
particle.y=stage.stageHeight-3;
particles.push(particle);
addChild(particle);

}
addEventListener(Event.ENTER_FRAME,explodeLoop);
}
private function clicker(e:MouseEvent):void{
again.parent.removeChild(again);
again.removeEventListener(MouseEvent.CLICK,clicker);
for (var i:int=platformArray.length-1;i>=0;i–){
platformArray[i].parent.removeChild(platformArray[i]);
platformArray.splice(i,1);
}

stage.focus=stage;
init();
}
private function explodeLoop(e:Event):void{
if(particles.length==0){
removeEventListener(Event.ENTER_FRAME,explodeLoop);
addChild(again);
again.addEventListener(MouseEvent.CLICK,clicker);
again.buttonMode=true;
}
for (var i:int=particles.length-1;i>=0;i–){
tempParticle=particles[i];
tempParticle.dy+=grav;
tempParticle.x+=tempParticle.dx;
tempParticle.y+=tempParticle.dy;
if(tempParticle.y>=stage.stageHeight){
tempParticle.y=stage.stageHeight;
particles.splice(i,1);
}
}
}
private function checkCollisionPlatform():void {
for(var i:int=platformArray.length-1;i>=0;i–){
tempPlatform=platformArray[i];
if ((Math.abs(guy.y-tempPlatform.y)<10)&&(guy.hitTestObject(tempPlatform))) {
connected = true;
speedY = 0;
guy.y=tempPlatform.y;
break;
} else {
connected = false;
}
}
}
private function kd(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.LEFT :
LEFT = true;
guy.scaleX = -1;
break;
case Keyboard.UP :
UP = true;
break;
case Keyboard.RIGHT :
RIGHT = true;
guy.scaleX = 1;
break;
}
//trace(e.keyCode);
}
private function ku(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.LEFT :
LEFT = false;
break;
case Keyboard.UP :
UP = false;
break;
case Keyboard.RIGHT :
RIGHT = false;
break;
}
}
}

}
[/as3]


×

Leave a Reply

You must be logged in to post a comment.