一个类中的AS3键盘事件?

时间:2022-11-03 00:05:13

I seem to have trouble getting a keyboard event within a class to work, I have an ENTER_FRAME event which works fine, but the keyboard event never gets called. Any ideas? here's the code

我似乎无法在类中使键盘事件工作,我有一个工作正常的ENTER_FRAME事件,但键盘事件永远不会被调用。有任何想法吗?这是代码

package 
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.*;
public class mainGame extends MovieClip 
{
    var myPlayer:player = new player();
    function mainGame():void 
    {   
        trace("arg!");
        addChild(myPlayer);
        addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
        addEventListener(Event.ENTER_FRAME, update);
    }
    function keyDown(evt:KeyboardEvent):void
        {
                            trace("This never happens");
            myPlayer.x++;
        }
        function update(evt:Event):void
        {
            trace("This happens fine");
        }

}
}

thanks in advance!

提前致谢!

3 个解决方案

#1


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. In your code, your event listener is being added to mainGame, so it is only going to fire when mainGame is on the stage, and the user has clicked (or mouse-overed, I forget) the mainGame movie clip.

与AS2不同,AS3中的键盘事件不是全局的。它们被发布到舞台上,它们通过显示列表向任何显示对象具有焦点。在你的代码中,你的事件监听器被添加到mainGame中,因此只有当mainGame在舞台上时才会触发,并且用户已经点击(或者鼠标忽略,我忘了)mainGame影片剪辑。

When you want to get keyboard events globally, as I said, they all start with the stage before bubbling, so if you register for events with the stage, you'll get all keyboard events. So as kekoav said, the way to do that is

当你想要全局获取键盘事件时,正如我所说,它们都是在冒泡前从舞台开始,所以如果你在舞台上注册事件,你将获得所有键盘事件。正如kekoav所说,做到这一点的方法是

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);

However, your problem is that stage in that code is not a global variable. It's a property that is common to all DisplayObjects, but it is null until the object is added to the display list.

但是,您的问题是该代码中的阶段不是全局变量。它是所有DisplayObject共有的属性,但在将对象添加到显示列表之前它是null。

So to dispense with the explanations, the code above will work if you call it after mainGame has been added to the stage. If there's no easy way for you to do that, you could add your keyboard listener inside another listener that knows when to fire:

因此,为了省略这些解释,如果在将mainGame添加到舞台后调用它,上面的代码将起作用。如果你没有简单的方法,你可以在另一个知道何时触发的监听器中添加你的键盘监听器:

    function mainGame():void {       
        //...
        addEventListener(Event.ADDED_TO_STAGE, stageAddHandler);
        //...
    }

    private function stageAddHandler(e:Event):void {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
        removeEventListener(Event.ADDED_TO_STAGE, stageAddHandler);
    }

#2


Add your keyboard event listeners to the stage instead of to your class.

将键盘事件侦听器添加到舞台而不是类。

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);

#3


 import flash.events.Event;
 import flash.events.KeyboardEvent;

place your keyboard event inside the enterframe event, that way it updates everytime the frame is updated.

将键盘事件放在enterframe事件中,这样每次更新帧时它都会更新。

            function update(evt:Event):void     
            {     
                    trace("This happens fine"); 
                     function keyDown(evt:KeyboardEvent):void     
                     {     
                        trace("This never happens");     
                        myPlayer.x++;     
                     }     
            }  

#1


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. In your code, your event listener is being added to mainGame, so it is only going to fire when mainGame is on the stage, and the user has clicked (or mouse-overed, I forget) the mainGame movie clip.

与AS2不同,AS3中的键盘事件不是全局的。它们被发布到舞台上,它们通过显示列表向任何显示对象具有焦点。在你的代码中,你的事件监听器被添加到mainGame中,因此只有当mainGame在舞台上时才会触发,并且用户已经点击(或者鼠标忽略,我忘了)mainGame影片剪辑。

When you want to get keyboard events globally, as I said, they all start with the stage before bubbling, so if you register for events with the stage, you'll get all keyboard events. So as kekoav said, the way to do that is

当你想要全局获取键盘事件时,正如我所说,它们都是在冒泡前从舞台开始,所以如果你在舞台上注册事件,你将获得所有键盘事件。正如kekoav所说,做到这一点的方法是

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);

However, your problem is that stage in that code is not a global variable. It's a property that is common to all DisplayObjects, but it is null until the object is added to the display list.

但是,您的问题是该代码中的阶段不是全局变量。它是所有DisplayObject共有的属性,但在将对象添加到显示列表之前它是null。

So to dispense with the explanations, the code above will work if you call it after mainGame has been added to the stage. If there's no easy way for you to do that, you could add your keyboard listener inside another listener that knows when to fire:

因此,为了省略这些解释,如果在将mainGame添加到舞台后调用它,上面的代码将起作用。如果你没有简单的方法,你可以在另一个知道何时触发的监听器中添加你的键盘监听器:

    function mainGame():void {       
        //...
        addEventListener(Event.ADDED_TO_STAGE, stageAddHandler);
        //...
    }

    private function stageAddHandler(e:Event):void {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
        removeEventListener(Event.ADDED_TO_STAGE, stageAddHandler);
    }

#2


Add your keyboard event listeners to the stage instead of to your class.

将键盘事件侦听器添加到舞台而不是类。

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);

#3


 import flash.events.Event;
 import flash.events.KeyboardEvent;

place your keyboard event inside the enterframe event, that way it updates everytime the frame is updated.

将键盘事件放在enterframe事件中,这样每次更新帧时它都会更新。

            function update(evt:Event):void     
            {     
                    trace("This happens fine"); 
                     function keyDown(evt:KeyboardEvent):void     
                     {     
                        trace("This never happens");     
                        myPlayer.x++;     
                     }     
            }  

相关文章