I decided to follow a tutorial for my casual game I’m making because its the first time I’ve attempted to make a game in Flash and I’m not great using actionscript 3 so needed the help, the tutorial can be found here. So i was following the tutorial and i got my character walking and jumping on platforms, also got some scenery done to make it look like the level i created in a previous post. And have a start button but I’ve got no ending to the game or ways to die, if you fall you just continuously fall.
This is the coding in my game –
/// A variable holds a value that can be modified by the program ///
/// Boolean = true or false ///
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var doubleJumpReady:Boolean = false;
var upReleasedInAir:Boolean = false;
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
var leftBumpPoint:Point = new Point(-30, -55);
var rightBumpPoint:Point = new Point(30, -55);
var upBumpPoint:Point = new Point(0, -120);
var downBumpPoint:Point = new Point(0, 0);
var scrollX:Number = 0;
var scrollY:Number = 500;
var xSpeed:Number = 0;
var ySpeed:Number = 0;
/// how fast you move ///
var speedConstant:int = 1;
/// how fast you come to a stop ///
var frictionConstant:Number = 0.9;
/// how fast you fall after jumping ///
var gravityConstant:Number = 1.5;
/// how fast you jump ///
var jumpConstant:Number = -25;
/// maximum speed run ///
var maxSpeedConstant:Number = 18;
stage.addEventListener(Event.ENTER_FRAME, loop);
/// Will run keyDownHandler when key is down, and keyUpHandler when realeased ///
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
function keyDownHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = true;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = true;
} else if(e.keyCode == Keyboard.UP){
upPressed = true;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = true;
}
}
function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = false;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = false;
} else if(e.keyCode == Keyboard.UP){
upPressed = false;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = false;
}
}
/// Updates the background depending on which key is pressed ///
/// when right or left key is pressed players xSpeed is increased in that
/// direction, same for ySpeed for up and down. speed gets decreased by
/// multiplying them by the friction variable which is 0.95 ///
function loop(e:Event):void{
if(back.collisions.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
trace(“leftBumping”);
leftBumping = true;
} else {
leftBumping = false;
}
if(back.collisions.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
trace(“rightBumping”);
rightBumping = true;
} else {
rightBumping = false;
}
if(back.collisions.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
trace(“upBumping”);
upBumping = true;
} else {
upBumping = false;
}
if(back.collisions.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
trace(“downBumping”);
downBumping = true;
} else {
/// double jump doesnt work but if i remove it game doesnt play? ///
if(upPressed == false){
upReleasedInAir = true;
if(doubleJumpReady && upReleasedInAir){
if(upPressed){
ySpeed = jumpConstant;
doubleJumpReady = false;
}
}
}
downBumping = false;
}
if(leftPressed){
xSpeed -= speedConstant;
player.scaleX = -1; /// face to the left ///
} else if(rightPressed){
xSpeed += speedConstant;
player.scaleX = 1; /// face to the right ///
}
/*if(upPressed){
ySpeed -= speedConstant;
} else if(downPressed){
ySpeed += speedConstant;
}*/
if(leftBumping){
if(xSpeed < 0){
xSpeed *= -0.5;
}
}
if(rightBumping){
if(xSpeed > 0){
xSpeed *= -0.5;
}
}
if(upBumping){
if(ySpeed < 0){
ySpeed *= -0.5;
}
}
if(downBumping){
if(ySpeed > 0){
ySpeed = 0;
}
if(upPressed){
ySpeed = jumpConstant;
}
} else {
ySpeed += gravityConstant;
}
if(xSpeed > maxSpeedConstant){ //moving right
xSpeed = maxSpeedConstant;
} else if(xSpeed < (maxSpeedConstant * -1)){ //moving left
xSpeed = (maxSpeedConstant * -1);
}
xSpeed *= frictionConstant;
ySpeed *= frictionConstant;
if(Math.abs(xSpeed) < 0.5){
xSpeed = 0;
}
scrollX -= xSpeed;
scrollY -= ySpeed;
back.x = scrollX;
back.y = scrollY;
/// Sky doesnt work, not sure why ///
sky.x = scrollX * 0.2;
sky.y = scrollY * 0.2;
}
There are a few things that don’t work and I’m not sure why like the double jump doesn’t work and the sky doesn’t show up but other then that i don’t have any errors.
This is what my game looks like:
When playing:
Start button:
I’m not particularly happy with my game at the moment because i wanted to be able to actually end the game and be able to die, its not really a game at the moment. I will continue to develop it so that i can get a better understanding of actionscript 3 and hopefully have a fully working game in the future.