我怎么可以访问根文件以外的阶段

我怎么可以访问根文件以外的阶段

本文介绍了我怎么可以访问根文件以外的阶段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我固定之前哪里出了问题的根源是,阶段是从我在那里把code人迹罕至的(而不是在根目录)这个错误。我删除它,它运行得很好,但是,他们attatched的事件监听器不工作了。我需要这些事件监听器,但是当我试图把'阶段。回盈它不会工作。帮助?

这是什么的时候,它的工作我有(当该文件仍然是根):

  stage.addEventListener(KeyboardEvent.KEY_DOWN,关键pressed);
        stage.addEventListener(KeyboardEvent.KEY_UP,调用keyReleased);
 

现在我的code是这样的,它运行,只是没有事件侦听器的工作:

 的addEventListener(KeyboardEvent.KEY_DOWN,关键pressed);
        的addEventListener(KeyboardEvent.KEY_UP,调用keyReleased);
 

解决方案

属性,直到元素添加到了舞台。所以,你应该先听事件 Event.ADDED_TO_STAGE ,当你收到它,您可以将您的其它事件侦听器:

 的addEventListener(Event.ADDED_TO_STAGE,addedToStage);

功能addedTostage():无效
{
    removeEventListener(Event.ADDED_TO_STAGE,addedToStage);
    stage.addEventListener(KeyboardEvent.KEY_DOWN,关键pressed);
    stage.addEventListener(KeyboardEvent.KEY_UP,调用keyReleased);
}
 

I fixed this error before where the source of the problem was that 'stage' was inaccessible from where I was putting the code (not in root). I deleted it and it ran fine, but the Event listeners that they were attatched to did not work anymore. I need these event listeners but when I try to put 'stage.' back infront it wont work. help?

This is what I had when it worked (when this file was still the root):

        stage.addEventListener(KeyboardEvent.KEY_DOWN, keypressed);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyreleased);

Now my code is this, and it runs, just without the event listeners working:

        addEventListener(KeyboardEvent.KEY_DOWN, keypressed);
        addEventListener(KeyboardEvent.KEY_UP, keyreleased);
解决方案

The stage property is null until the element is added to the stage. So you should first listen to the event Event.ADDED_TO_STAGE, and when you receive it, you can add your other event listeners:

addEventListener(Event.ADDED_TO_STAGE, addedToStage);

function addedTostage():void
{
    removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keypressed);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyreleased);
}

这篇关于我怎么可以访问根文件以外的阶段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 10:18