Gabriel Algumacoisa
Gabriel Algumacoisa

Reputation: 13

Some Problems with my AS3 code

Some troubles to make my new flash game(i used AS3,CS5.5): I just try to make my character(hatt) walk and jump, but I can't make it at the same time, well, idk how... Furthermore, I don't know how make my character recognize the ground. And at last this thing here:

"Scene 1, Layer 'hatt', Frame 1, Line 6 Warning: 1090: Migration issue: The onKeyDown event handler is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'keyDown', callback_handler)."

Plz help me and give some hints plz... Thanks.

Here's the code:

var grav:Number = 10;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(Event.ENTER_FRAME, update);
function onKeyDown(evt:KeyboardEvent):void
{
  if (evt.keyCode == Keyboard.UP)
  {
    if (jumping != true)
    {
      jumpPow = -25;
      jumping = true;
    }
  }
}
function update(evt:Event):void
{
  if (jumping)
  {
    hatt.y +=  jumpPow;
    jumpPow +=  grav;
    if (hatt.y >= stage.stageHeight)
    {
     jumping = false;
     hatt.y = stage.stageHeight;
    }
  }
}

stage.addEventListener (KeyboardEvent.KEY_DOWN, myFunction) ;

function myFunction (event: KeyboardEvent){
  if(event.keyCode == Keyboard.LEFT) {
    hatt.x -= 5
  }
  if(event.keyCode == Keyboard.RIGHT) {
   hatt.x += 5
  }
}

Upvotes: 1

Views: 1553

Answers (1)

sanchez
sanchez

Reputation: 4530

remove this line:

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

remove function onKeyDown:

function onKeyDown(evt:KeyboardEvent):void
{
  if (evt.keyCode == Keyboard.UP)
  {
    if (jumping != true)
    {
      jumpPow = -25;
      jumping = true;
    }
  }
}

replace myFunction with this:

function myFunction (event: KeyboardEvent)
{
  if(event.keyCode == Keyboard.LEFT)  
    hatt.x -= 5;

  if(event.keyCode == Keyboard.RIGHT) 
    hatt.x += 5;

  if(event.keyCode == Keyboard.UP && jumping != true)
  {
     jumpPow = -25;
     jumping = true;
  }
}

Upvotes: 2

Related Questions