Trying to get the down arrow to go to next level in flash.

A place for tutorials on how to get the most out of Flash

Trying to get the down arrow to go to next level in flash.

Postby flashguy1! » Wed Aug 08, 2012 8:00 pm

I'm New , but I've been lurking here for nearly a year or so.Seeing some great games being made by fans of playshapes , and finally deciding to throw my hat into the ring.I myself just got into flash maybe two weeks ago meaning I'm pretty much a newb.But the tutorials found here have really helped me out in trying to understand some stuff. :D

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;
      
   }
}
Last edited by flashguy1! on Thu Aug 09, 2012 7:12 am, edited 1 time in total.
flashguy1!
Newly Registered
 
Joined: Wed Aug 08, 2012 7:28 pm

Re: Trying to get the down arrow to go to next level in flas

Postby OwnerOfSuccuby » Thu Aug 09, 2012 4:53 am

Put code in dore - if you have a key - then dore go to frame where it is open and make doorOpen=true;

Then check

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();

I little do not understand your problem :roll: What does not work can you say more detail ?
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm

Re: Trying to get the down arrow to go to next level in flas

Postby flashguy1! » Thu Aug 09, 2012 7:11 am

OwnerOfSuccuby Wrote:Put code in dore - if you have a key - then dore go to frame where it is open and make doorOpen=true;

Then check

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();

I little do not understand your problem :roll: What does not work can you say more detail ?


Appreciate you trying to help me OwnerOfSuccuby.I actually got help from a guy on another site , turns out my code needed a good deal of work done on it.In the end he solved my problem and I have my platform tutorial file working.It turned out like this

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 = 1;

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
{
   
   if(currentLevel < 2)
   {
      doorOpen = false;
      //keyCollected = false;
      currentLevel++;
      trace("Next Level: " + currentLevel);
      back.collisions.gotoAndStop(currentLevel);
      //back.visuals.gotoAndStop(currentLevel);
      back.other.lockedDoor.gotoAndStop(currentLevel);
      //back.other.doorKey.gotoAndStop(currentLevel);
      back.visuals.visible = true; //for debugging before graphics
      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

   }
   else trace("level not yet made");
   
   
   
/*   if (currentLevel == 2)
   {
      trace("next map");
      //gotoLevel2(); //there are no functions with that name
   }*/
}

//function gotoLevel2()

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 && keyCollected == true)
   {// if the door hasn't been opened yet
      // 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
      }
   }
   if (player.hitTestObject(back.other.lockedDoor) && doorOpen && downPressed)
   {
      nextLevel();
   }

   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;
   }
}
flashguy1!
Newly Registered
 
Joined: Wed Aug 08, 2012 7:28 pm


Return to Tutorials



Who is online

Users browsing this forum: No registered users