Page 1 of 1

AS3 jumping problem

PostPosted: June 27th, 2011, 12:05 pm
by Konradix
The code:
Code: Select all
var rightKeyisDown:Boolean=false;
var leftKeyisDown:Boolean=false;
var upKeyisDown:Boolean=false;
var downKeyisDown:Boolean=false;

var gravity:Number=1;
var yVelocity:Number=0;
var canJump:Boolean=false;

stage.addEventListener(KeyboardEvent.KEY_DOWN,PressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP,ReleaseAKey);

function PressAKey(event:KeyboardEvent):void {
   if (event.keyCode==Keyboard.RIGHT) {
      rightKeyisDown=true;
   }
   if (event.keyCode==Keyboard.LEFT) {
      leftKeyisDown=true;
   }
   if (event.keyCode==Keyboard.UP) {
      upKeyisDown=true;
   }
   if (event.keyCode==Keyboard.DOWN) {
      downKeyisDown=true;
   }
}

function ReleaseAKey(event:KeyboardEvent):void {
   if (event.keyCode==Keyboard.RIGHT) {
      rightKeyisDown=false;
   }
   if (event.keyCode==Keyboard.LEFT) {
      leftKeyisDown=false;
   }
   if (event.keyCode==Keyboard.UP) {
      upKeyisDown=false;
   }
   if (event.keyCode==Keyboard.DOWN) {
      downKeyisDown=false;
   }
}

circle.addEventListener(Event.ENTER_FRAME,moveTheCircle);

function moveTheCircle(event:Event):void {

   if (rightKeyisDown) {

      circle.x+=5;
   }
   if (leftKeyisDown) {

      circle.x-=5;
   }
   if (upKeyisDown&&canJump) {

      yVelocity=-15;
      canJump=false;
   }
   yVelocity+=gravity;

   if (! floor.hitTestPoint(circle.x,circle.y,true)) {

      circle.y+=yVelocity;

   }
   if (yVelocity>20) {

      yVelocity=20;
   }

   for (var i,int = 0; i<10; i++) {
      if (floor.hitTestPoint(circle.x,circle.y,true)) {

         circle.y--;
         yVelocity=0;
         canJump=true;
      }
   }
}


I'm following this tutorial:
http://www.youtube.com/watch?v=Z160NtbM ... ion_112532 - part 1
http://www.youtube.com/watch?v=LVaEZMii ... re=related - part 2

I am doing everything right, I have no errors, but my character refuses to jump. I mash up key like crazy, watch tutorial back, mash up key, check my code, mash up key, test etc.

I would ask the creator of the video to help me, but the problem is that those tutorials were relased nearly year ago, so I doubt he'll remember.

Thanks in advance

Re: AS3 jumping problem

PostPosted: June 29th, 2011, 4:17 am
by Buff_
Konradix wrote:I would ask the creator of the video to help me, but the problem is that those tutorials were relased nearly year ago, so I doubt he'll remember.


He replied to someone's comment 3 weeks ago. That's pretty recent, so I suggest asking him.