我的AS3代码有些问题

时间:2012-08-20 21:45:34

标签: actionscript-3 flash

制作我的新Flash游戏(我使用AS3,CS5.5)的一些麻烦: 我只是试着让我的角色(hatt)走路和跳跃,但我不能同时做到,好吧,idk如何...而且,我不知道如何让我的角色认出地面。 最后这件事在这里:

“场景1,图层'hatt',第1帧,第6行警告:1090:迁移问题:在ActionScript 3.0中,Flash Player不会在运行时自动触发onKeyDown事件处理程序。您必须先为此处理程序注册使用addEventListener('keyDown',callback_handler)的事件。“

Plz帮助我并给出一些提示plz ...谢谢。

以下是代码:

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
  }
}

1 个答案:

答案 0 :(得分:2)

删除此行:

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

删除onKeyDown函数:

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

用此替换myFunction:

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;
  }
}