I’m making a relatively simple platformer and i’ve gotten my character to move left/right etc. but im having trouble working out the code for the jump algorithm
tried to make it just move up but looks horrible and not sure what to do
Thanks
this is the entire of the code
package {
import flash.text.*;
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;
import flash.media.Sound;
public class Main extends MovieClip {
//variables go here
private var startGameBtn:startGameBtn_symbol = new startGameBtn_symbol();
private var jumpSpeed:int=0;
private var gameMode:String="menu";
private var hero:Object;
private var characterMc:MovieClip = new character();
private var jumpAmount:Number=1;
private var jumpMaximum:int=10;
private var jumpChange:Number=1;
private var vX:int=0;
private var vY:int=0;
//objects that can be stood on
private var fixedObjects:Array;
//main
public function Main():void {
trace("hi");
menu();
listeners();
}
//setup menu
public function menu():void {
trace("its the menu");
startGameBtn.buttonMode=true;
addChild(startGameBtn);
}
//start game after leav menu
public function gogame(event:MouseEvent):void {
removeChild(startGameBtn);
gameMode="play";
startgame();
stage.focus=stage;
}
//creates the hero
public function createHero():void {
hero = new Object();
hero.mc=characterMc;
hero.dx=0.0;
hero.dy=0.0;
hero.inAir=false;
hero.jump=false;
hero.jumpSpeed=0.8;
hero.walkSpeed=0.2;
hero.mc.x=200;
hero.mc.y=stage.stageHeight-hero.mc.height;
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 {
createHero();
}
public function mainLoop(e:Event):void {
if (gameMode=="play") {
moveCharacter(hero);
}
}
public function moveCharacter(char:Object) {
char.mc.x+=char.dx;
char.mc.y+=char.dy;
}
public function moveKeyDown(event:KeyboardEvent) {
if (event.keyCode==Keyboard.RIGHT) {
hero.dx=10;
} else if (event.keyCode==Keyboard.LEFT) {
hero.dx=-10;
} else if (event.keyCode==Keyboard.SPACE) {
if (! hero.inAir) {
hero.jump=true;
jump();
}
}
}
public function moveKeyUp(event:KeyboardEvent) {
if (event.keyCode==Keyboard.RIGHT) {
hero.dx=0;
} else if (event.keyCode==Keyboard.LEFT) {
hero.dx=0;
}
}
public function jump():void {
hero.dy = -0.2;
while (hero.dy > -5){
jumpChange = jumpChange * 1.2;
hero.dy -= hero.dy * jumpChange;
}
trace (hero.dy);
trace (hero.mc.y);
}
}
}