键盘事件 as3 不起作用

发布于 2024-12-07 10:45:45 字数 866 浏览 1 评论 0原文

这件事让我敲了两个小时才弄清楚。
我决定将其发布在这里,以帮助其他人不要拔毛:)。

本质上,错误是我没有从 Flash 构建器环境中接收键盘事件(在 adobe flash cs5 中也可以看到相同的错误/问题)。我设置了stage.focus = stage,没有帮助。我添加了其他事件侦听器(mouse_down、frame_enter),效果很好,我添加了 MovieClip 子项并侦听这些子项上的事件,但仍然是同样的问题。

package
{
  public class Test extends Sprite 
  {

    public function Test() 
    {
        this.addEventListener(Event.ADDED_TO_STAGE,init);
    }

    public function init(stage:Stage):void 
    {
        this.removeEventListener(Event.ADDED_TO_STAGE,init);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
    }


    private function keyPressed(e:KeyboardEvent):void 
    {
        trace("keyPressed");
    }

    private function keyReleased(e:KeyboardEvent):void 
    {
        trace("keyReleased");
    }
  }
}

This was something that had me banging my head for 2 hours before I figured it out.
I decided to post it here, to help others not pull their hair out :).

Essentially the bug was I was not receiving the keyboard event from within my flash builder environment (The same bug/issue is visible with adobe flash cs5). I set the stage.focus = stage, did not help. I added other event listeners (mouse_down, frame_enter) which worked fine, I added MovieClip children and listened for events on those children, still the same issue.

package
{
  public class Test extends Sprite 
  {

    public function Test() 
    {
        this.addEventListener(Event.ADDED_TO_STAGE,init);
    }

    public function init(stage:Stage):void 
    {
        this.removeEventListener(Event.ADDED_TO_STAGE,init);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
    }


    private function keyPressed(e:KeyboardEvent):void 
    {
        trace("keyPressed");
    }

    private function keyReleased(e:KeyboardEvent):void 
    {
        trace("keyReleased");
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一直在等你来 2024-12-14 10:45:46
public function init(stage:Stage):void 

ADDED_TO_STAGE is a `listener Event` not a stage instance. 

因此,请使用 event:Event 而不是 stage:Stage

你需要导入所需的类。

public function init(stage:Stage):void 

ADDED_TO_STAGE is a `listener Event` not a stage instance. 

so instead of stage:Stage use event:Event.

and u needs to import needed classes.

简美 2024-12-14 10:45:46

标记发生更改的行。顺便说一句,您的代码无法编译,请检查错误日志。

package  {

import flash.display.Sprite; /// changed line
import flash.events.Event; /// changed line
import flash.events.KeyboardEvent; /// changed line


public class Test extends Sprite 
{

public function Test() 
{
    this.addEventListener(Event.ADDED_TO_STAGE,init);
    /* i like it this way
    stage ? init(null) : addEventListener(Event.ADDED_TO_STAGE,init);
    */

}

public function init(e:Event):void  /// changed line
{
    this.removeEventListener(Event.ADDED_TO_STAGE,init);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}


private function keyPressed(e:KeyboardEvent):void 
{
    trace("keyPressed");
}

private function keyReleased(e:KeyboardEvent):void 
{
    trace("keyReleased");
}
}

}

Marked the line that changed. Your code doesnt compile btw, check for error logs.

package  {

import flash.display.Sprite; /// changed line
import flash.events.Event; /// changed line
import flash.events.KeyboardEvent; /// changed line


public class Test extends Sprite 
{

public function Test() 
{
    this.addEventListener(Event.ADDED_TO_STAGE,init);
    /* i like it this way
    stage ? init(null) : addEventListener(Event.ADDED_TO_STAGE,init);
    */

}

public function init(e:Event):void  /// changed line
{
    this.removeEventListener(Event.ADDED_TO_STAGE,init);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}


private function keyPressed(e:KeyboardEvent):void 
{
    trace("keyPressed");
}

private function keyReleased(e:KeyboardEvent):void 
{
    trace("keyReleased");
}
}

}
他不在意 2024-12-14 10:45:45

使用键盘命令需要监听键盘事件。此过程与侦听 AS3 中任何其他事件的过程相同。您需要使用addEventListener()方法来注册KeyboardEvent。然而,与其他对象不同的是,由于键盘不需要附加到项目中的任何特定对象,因此键盘事件通常注册到舞台。在下面的代码中,舞台对象注册每次按下键盘键时触发的键盘事件。

与 AS2 不同,AS3 键盘事件不是全局的。它们被发送到舞台,并通过显示列表冒泡到具有焦点的任何显示对象。

package
{
import flash.display.*;
import flash.events.*;

  public class Test extends Sprite 
  {
   public function Test() 
   {
     init();
   }

   public function init():void 
   {
      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
      stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
   }


   private function keyPressed(e:KeyboardEvent):void 
   {
      trace("keyPressed");
   }

   private function keyReleased(e:KeyboardEvent):void 
   {
    trace("keyReleased");
   }
  }
}

Using keyboard commands requires listening to keyboard events. This process is identical to the process for listening to any other event in AS3. You need to use the addEventListener() method to register with a KeyboardEvent. However, unlike other objects, due to the fact that the keyboard is not necessary attached to any specific object in the project, the Keyboard Event is usually registered with the stage. In the code below, the stage object registers for a keyboard event to be triggered each time a keyboard key is pressed down.

Unlike in AS2, in AS3 Keyboard events are not global. They are issued to the stage, and they bubble through the display list to whatever display object has focus.

package
{
import flash.display.*;
import flash.events.*;

  public class Test extends Sprite 
  {
   public function Test() 
   {
     init();
   }

   public function init():void 
   {
      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
      stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
   }


   private function keyPressed(e:KeyboardEvent):void 
   {
      trace("keyPressed");
   }

   private function keyReleased(e:KeyboardEvent):void 
   {
    trace("keyReleased");
   }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文