
http://as3gametuts.com
I'm stuck on this one at the end http://as3gametuts.com/2012/04/05/platformer-8/ .Pretty much I'm trying figure out how to get this part of the code to work when you press down to load my layered frame 2 stuff , which would be level 2.I'm Thankful for any help from whoever helps me on this.
- Code: Select All Code
else if(e.keyCode == Keyboard.DOWN){
downPressed = true;
if(doorOpen && player.hitTestObject(back.other.lockedDoor)){
//proceed to the next level if the player is touching an open door
nextLevel();
Everytime I change the code to the above I get a right bracket error , then I get an up key handler problem and it doesn't run any more.It currently looks like this , without the changes and runs.So far it's suppose to work like this(get a key or object that when it touches a locked door it opens).From there your suppose to push the down key and be transported to level 2.
- Code: Select All Code
var scrollX:Number = 0;
var scrollY:Number = 500;
var xSpeed:Number = 0;
var ySpeed:Number = 0;
var speedConstant:int = 4;
var gravityConstant:Number = 2;
var maxSpeedConstant:Number = 18;
var friction:Number = 0.87;
var keyCollected:Boolean = false;
var doorOpen:Boolean = false;
var currentLevel:int;
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 leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var jumpConstant:Number = -36;
var doubleJumpReady:Boolean = false;
var upReleasedInAir:Boolean = false;
stage.addEventListener(Event.ENTER_FRAME, loop);
function nextLevel():void
{
currentLevel++;
trace("Next Level: " + currentLevel);
if (currentLevel == 2)
{
//gotoLevel2(); //there are no functions with that name
}
}
function loop(e:Event):void
{
if (keyCollected == false)
{// if we still haven't collected the key
if (player.hitTestObject(back.other.doorKey))
{// and if the player collides with the key
back.other.doorKey.visible = false;// hide the key from view
keyCollected = true;// set our Boolean to true
}
}
if (doorOpen == false)
{// if the door hasn't been opened yet
if (keyCollected == true)
{// and if the player has already collected the key
if (player.hitTestObject(back.other.lockedDoor))
{// check if the door and the player are touching
// if all of these conditions are met...
back.other.lockedDoor.gotoAndStop(2);
// ...switch the door's image to its 2nd frame;
doorOpen = true;// ...set the variable to true
}
function gotoLevel2():void{
back.other.gotoAndStop(2); //updates door and key
back.visuals.gotoAndStop(2); //updates the visuals
back.collisions.gotoAndStop(2); //updates the collisions
scrollX = 0; //resets the player's x position in the new level
scrollY = 500; //resets the player's y position in the new level
keyCollected = false; //resets the keyCollected variable
back.other.doorKey.visible = true; //makes the key visible again
doorOpen = false; //resets the doorOpen variable
back.other.lockedDoor.gotoAndStop(1); //makes the door return to its locked image
}
}
}
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;
upReleasedInAir = false;//upon landing, reset to false
doubleJumpReady = true;//upon landing, reset to true
}
else
{
if (upPressed == false)
{// if the player releases the up arrow key
upReleasedInAir = true;// set the variable to true
}
downBumping = false;
}
if (doubleJumpReady && upReleasedInAir)
{// if both variables are true
if (upPressed)
{//and if the up arrow is pressed
ySpeed = jumpConstant;//set the y speed to the jump constant
doubleJumpReady = false;//then, prevent additional double jumps
}
}
if (leftPressed)
{
xSpeed -= speedConstant;
player.scaleX = -1;// face to the left
}
else if (rightPressed)
{
xSpeed += speedConstant;
player.scaleX = 1;
}// face to the right
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;
}
if (upPressed)
{
ySpeed = jumpConstant;
}
}
else
{//if we are not touching the floor
ySpeed += gravityConstant;//accelerate downwards
}
if (xSpeed > maxSpeedConstant)
{//moving right
xSpeed = maxSpeedConstant;
}
else if (xSpeed < (maxSpeedConstant * -1))
{//moving left
xSpeed = (maxSpeedConstant * -1);
}
xSpeed *= friction;
ySpeed *= friction;
if (Math.abs(xSpeed) < 0.5)
{
xSpeed = 0;
}
scrollX -= xSpeed;
scrollY -= ySpeed;
back.x = scrollX;
back.y = scrollY;
sky.x = scrollX * 0.2;
sky.y = scrollY * 0.2;
}//end of loop!
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;
}
}