Hi - i’m working on a platformer game and i’m trying to get the basics down…
here are some of the issues i’m having.
Try it here: http://dl.dropbox.com/u/2529964/playformer_1.swf
TO START OFF WITH PRESS SPACE TO JUMP ARROW KEYS MOVE
[LIST=1]
[]getting jumping right. if you play the swf my variables are always wrong and i can’t seem to make it so you can both jump and fall
[]Walls (and their collision) - is the way i’m doing it fine or is there a way to have my walls generated through the array like i’m doing for the platforms
[/LIST]
Help most welcome (:
Thanks,
Sam
package {
import flash.text.*;
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;
import flash.utils.getTimer;
import flash.media.Sound;
public class Main extends MovieClip {
//variables go here
private var startGameBtn:startGameBtn_symbol=new startGameBtn_symbol ;
private var levelContainer:MovieClip = new MovieClip();
private var jumpSpeed:int=0;
private var variableText:TextField = new TextField();
private var gameMode:String="menu";
private var hero:Object;
private var characterMc:MovieClip=new character ;
private var jumpHeight:int;
private var timeDiff:Number;
private var firstJump:Boolean=true;
static const gravity=20;
private var lastTime:Number=0;
private var mapShift:Boolean=false;
private var onPlatform:Boolean=false;
private var vX:int=0;
private var vY:int=0;
/* 0 is nothing, 1 is boundaries, 2 is platform, 4 is ground there are 6 colums and 12 rows*/
private var level1Plan:Array = new Array(
1,1,1,1,1,1,
1,0,0,0,0,1,
1,0,0,0,0,1,
1,0,2,0,0,1,
1,0,0,0,2,1,
1,2,0,0,0,1,
1,0,0,0,0,1,
1,0,0,2,0,1,
1,0,0,0,0,1,
1,2,2,2,2,1,
1,0,0,0,0,1,
1,4,4,4,4,1);
//objects that can be stood on
private var platformObjects:Array;
//main
public function Main():void {
trace("hi");
menu();
listeners();
}
//setup menu
public function menu():void {
trace("its the menu");
startGameBtn.buttonMode=true;
startGameBtn.x = (stage.stageWidth) / 2;
startGameBtn.y = (stage.stageHeight) / 2;
addChild(startGameBtn);
}
//start game after leav menu
public function gogame(event:MouseEvent):void {
removeChild(startGameBtn);
gameMode="play";
startgame();
stage.focus=stage;
variableText.x=200;
variableText.y=10;
variableText.width=500;
variableText.text = String(("inAir = " + hero.inAir + " jump = " + hero.jump + " on platformm? = " + onPlatform));
addChild(variableText);
}
//creates the hero
public function createHero():void {
hero=new Object ;
hero.mc=characterMc;
hero.direction=0;
hero.vx=0.0;
hero.vy=0.0;
hero.inAir=false;
hero.falling=true;
hero.jump=false;
hero.jumpSpeed=-90;
hero.walkSpeed=60;
hero.mc.x=200;
hero.mc.y=104;
addChild(hero.mc);
}
public function listeners():void {
startGameBtn.addEventListener(MouseEvent.CLICK,gogame);
this.addEventListener(Event.ENTER_FRAME,mainLoop);
stage.addEventListener(KeyboardEvent.KEY_DOWN,moveKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,moveKeyUp);
}
public function startgame():void {
createLevel();
createHero();
levelContainer.x=0;
levelContainer.y=0;
addChild(levelContainer);
}
public function updateText():void {
variableText.text = String(("inAir = " + hero.inAir + " jump = " + hero.jump + " on platformm? = " + onPlatform));
}
public function createLevel():void {
var levelColumns:int=Math.ceil(level1Plan.length/12);
var row:int=0;
//add the left walls
for (var i:int = 0; i<level1Plan.length; i++) {
if (level1Plan*==1) {
if (i/levelColumns==int(i/levelColumns)) {
row++;
}
}
if (level1Plan*==2) {
var newPlatform:MovieClip = new platform();
newPlatform.x = (i-(row-1)*levelColumns)*newPlatform.width;
newPlatform.y = (row-1)*newPlatform.height;
trace(newPlatform.x);
trace(newPlatform.y);
levelContainer.addChild(newPlatform);
}
if (level1Plan*==4) {
var newGround:MovieClip=new ground();
newGround.x=0;
newGround.y=stage.stageHeight-newGround.height;
levelContainer.addChild(newGround);
}
}
var totalWalls:int = (((stage.stageHeight/64)) * 2);
//add right walls
for (var u:int=0; u<(totalWalls); u++) {
var newWall:MovieClip = new wall();
if (u<=(totalWalls / 2)) {
newWall.x=0;
newWall.y=u*newWall.height;
} else {
trace(levelContainer.width);
newWall.x=levelContainer.width-newWall.width;
newWall.y=((u/2))*newWall.height-448;
}
levelContainer.addChild(newWall);
}
}
public function mainLoop(e:Event):void {
if (lastTime==0) {
lastTime=getTimer();
}
timeDiff=getTimer()-lastTime;
lastTime+=timeDiff;
if (gameMode=="play") {
moveCharacter(timeDiff);
checkCollision();
updateText();
}
}
public function checkCollision():void {
var noChildren:int=levelContainer.numChildren;
for (var i:int = 0; i<noChildren; i++) {
var blockTest:DisplayObject=levelContainer.getChildAt(i);
//if falling and hit test
if ((hero.mc.hitTestObject(blockTest)) && (String(blockTest)!= "[object wall]")) {
trace('stopped');
hero.inAir=false;
hero.jump=false;
hero.mc.y=blockTest.y-hero.mc.height;
onPlatform=true;
} else {
if (!firstJump) {
onPlatform=false;
hero.inAir=true;
trace("fall off platform" + int(Math.random()*10));
}
}
if ((hero.mc.hitTestObject(blockTest)) && (String(blockTest)== "[object wall]")) {
if (hero.mc.x<100) {
hero.mc.x=64;
}
if (hero.mc.x>100) {
hero.mc.x=stage.stageWidth -(levelContainer.width - stage.stageWidth) ;
trace(hero.mc.x);
}
}
}
//stop left collision
if (hero.mc.x >= (((stage.stageWidth - hero.mc.width))) && (hero.direction == 1)) {
hero.vx=0;
hero.mc.x=0;
levelContainer.x=- stage.stageWidth+hero.mc.width;
mapShift=true;
} else if ((hero.mc.x <= (63) && (hero.direction == -1))) {
hero.vx=0;
hero.mc.x=stage.stageWidth;
levelContainer.x=0;
mapShift=true;
} else {
}
/*
for (var i:int = 0; i<levelContainer.numChildren; i++) {
var hitBlock:DisplayObject=levelContainer.getChildAt(i);
if ((hero.mc.hitTestObject(hitBlock))) {
trace("hit");
hero.dy=0;
}
}
*/
}
public function moveCharacter(timeDiff:Number) {
if (timeDiff<1) {
return;
}
var timeDiffSec=timeDiff/100;
if (hero.direction==1) {
//moving right
hero.vx=hero.walkSpeed;
} else if (hero.direction == -1) {
//moving left
hero.vx=- hero.walkSpeed;
} else if (hero.direction == 0) {
//stationary
hero.vx=0;
}
//faling
if ((hero.inAir) && (!hero.jump) && (!onPlatform)) {
hero.vy=120;
onPlatform=false;
}
//jumping algorithms
if (hero.jump) {
hero.inAir=true;
if (hero.vy<0) {
hero.vy+=timeDiffSec*gravity;
} else if (hero.vy == 0) {
trace("apex");
} else if (hero.vy > 0) {
hero.vy+=timeDiffSec*gravity;
} else {
hero.inAir=false;
}
}
//move
hero.mc.x+=hero.vx*timeDiffSec;
hero.mc.y+=hero.vy*timeDiffSec;
/*
if (hero.mc.y>stage.stageHeight-hero.mc.height) {
hero.jump=false;
hero.inAir=false;
hero.vy=0;
trace("hit bottom");
hero.mc.y=stage.stageHeight-hero.mc.height;
}
*/
//press key down
}
public function moveKeyDown(event:KeyboardEvent) {
if (event.keyCode==Keyboard.RIGHT) {
hero.direction=1;
} else if (event.keyCode==Keyboard.LEFT) {
hero.direction=-1;
} else if (event.keyCode==Keyboard.SPACE) {
if (! hero.inAir) {
hero.vy=hero.jumpSpeed;
hero.jump=true;
firstJump=false;
}
}
}
public function moveKeyUp(event:KeyboardEvent) {
if (event.keyCode==Keyboard.RIGHT) {
hero.direction=0;
} else if (event.keyCode==Keyboard.LEFT) {
hero.direction=0;
}
}
}
}