闪光CS5引用显示对象在非文件类的构造函数闪光、函数、对象、文件

2023-09-08 12:59:58 作者:重拾搁浅的梦

学习访问放置在舞台上在​​Flash CS5中的一类非文件类的其他对象的这个好方法后(发现in这个线程),我遇到了一个障碍点。我使用

After learning of this excellent method of accessing an object placed on the stage in Flash CS5 in a class other than the document class (found in this thread), I have run into a stumbling point. I am using

this.stage.getChildAt(0).greenLight1.visible=false;

要访问g​​reenLight1(在我的.fla为位图指定的实例名称)时,请将实例名称为一个符号greenLight1无形。这完美的作品,我用它这是在几个不同类别的子功能。我遇到的问题是,你不能在这些非文件类的构造函数中使用,或由构造函数直接调用任何功能。如果我尝试这样做,我得到了下面的错误#1009:无法访问空对象引用的属性或方法

to turn a symbol with the instance name greenLight1 invisible when accessing greenLight1 (the instance name specified in my .fla for the bitmap). This works perfectly where I was using it which is in a sub function of a few different classes. The problem I'm running into is that you can't use this in the constructor of these non-document classes, or in any function called directly by the constructor. If I try to do that, I get the following "Error #1009: Cannot access a property or method of a null object reference."

所以我想,我不是正确地访问这一点,并开始用这句话来打。当我切换到

So I figure that I'm not accessing this correctly and start to play with the phrase. When I switch it to

this.getChildAt(0).greenLight1.visible=false;

我得到一个不同的错误1119:通过静态类型flash.display使用的参考可能未定义的属性greenLight1的访问。的DisplayObject所以我显然失去了一些东西。

I get a different error "1119: Access of possibly undefined property greenLight1 through a reference with static type flash.display:DisplayObject." So I'm obviously missing something here.

现在我尝试了不同的策略,我发现在这个线程我也在上面的线程的链接我成立提及。多亏了我的其他线程艾伦的评论我居然得到了该方法的工作。然而,当我尝试把该引用到构造函数类中的任何其他非文档类的我居然得到相同的错误#1009:无法访问空对象引用的属性或方法但是,如果我在一个子功能,使用它工作得很好。因此,同样的问题,它似乎。

Now I tried a different tactic that I found in this thread which I also mentioned in the above thread link I set up. And thanks to Allan's comment in my other thread I actually got that method working. However, when I try to place that reference into the constructor class of any other non-document class I actually get the same "Error #1009: Cannot access a property or method of a null object reference." But it works just fine if I use it in a sub function. So same problem it seems.

我的项目由持有不同的位图符号和几个按钮,我已经放在我的图书馆,并拖到舞台(我已经给了他们所有的唯一实例名称)一Game.fla文件。我已经设置了我的文档类Game.as,并在它的内部,我可以访问所有这些项目没有失败简单地使用他们的实例名称。现在在我的子类称为Player.as,我已经设置了)所谓的LightsOut(函数使用的开关/箱该关灯。正是在那里,我用this.stage.getChildAt(0).greenLight1.visible = FALSE;它完美地工作。这也是我设置了不同的战术我上面提到的,并试图Game.GL1.visible = FALSE;并且完美工作为好。但后来在我的子类称为Controller.as在那里我有我所有的鼠标和键盘处理程序,我试图建立一个事件侦听器的我的舞台按钮。所以,我把它放在控制器构造函数在那里我有我所有的其他事件侦听器设置和已经工作的(他们使用stage.addeventlistener ......),它在这里,我开始看到一些错误。所以,我想我会尝试只得到了greenLight1关闭,因为我已经知道,code的工作,试用过this.stage.getChildAt ......线和Game.GL1 ...只有行,看着他们使我在上面提到的错误。于是我又回到了我的Player.as类,并尝试他们在它的播放器构造类,并再次得到了,即使我使用的是同一条线路下的Player.as类就好了同样的错误。请注意,我已经通过在舞台上两个控制器和球员,并一直在使用它就好了两个。我也尝试了呼叫建立在所谓的初始化构造另一个函数();并试图访问greenLight1在那里,以及时得到了同样的错误,但它仍然在工作的LightsOut()。另外不知道它的问题,但所有的类扩展MovieClip的为好。我不知道这是否会帮助,但这里是随着运行LightsOut功能的播放器类的开头我已经剥离出来的code休息,因为它是不相关的:

My project consists of a Game.fla file that holds various bitmap symbols and several buttons that I've placed into my Library and dragged to the stage (and I've given them all unique instance names). I've set up my document class Game.as and inside of it, I can access all of these items without fail simply using their instance name. Now in my sub class called Player.as, I've set up a function called lightsOut() which turns the lights off using a switch/case. It is there that I'm using "this.stage.getChildAt(0).greenLight1.visible=false;" and it is working perfectly. This is also where I set up the different tactic I mentioned above and tried "Game.GL1.visible=false;" and that worked perfectly as well. But then in my sub class called Controller.as where I have all of my mouse and keyboard handlers, I tried to set up a event listener for one of my stage buttons. So I placed it in the Controller constructor function where I have all of my other event listeners set up and already working (they use "stage.addeventlistener...") and it was here that I started to see errors. So I figured I would try to just get the greenLight1 to turn off since I already knew that code was working and tried both the "this.stage.getChildAt...." line and the "Game.GL1..." line only to watch them cause the errors I mentioned above. So I went back to my Player.as class and tried them in it's Player constructor class and once again got the same errors even though I'm using the same line lower in the Player.as class just fine. Note that I have passed the stage to both Controller and Player and have been using it just fine in both. I also tried setting up a call to another function in the constructor called Init(); and got the same error when trying to access greenLight1 in there as well, yet it still worked in lightsOut(). Also not sure if it matters, but all classes extend MovieClip as well. I don't know if this will help but here is the beginning of the Player class along with the lightsOut function I've stripped out the rest of the code as it's unrelated:

package {
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.DisplayObject;

public class Player extends MovieClip {
    private var _stage:Object;
    private var _lights:uint;

    public function Player(stage:Object):void {
        this._stage = stage;
        this._stage.getChildAt(0).greenLight1.visible=false; //errors
        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }
    private function lightsOut():void {
        switch(_lights) {
            case 1:
            this._stage.getChildAt(0).greenLight1.visible=false;//works
            break;
            case 2:
            this._stage.getChildAt(0).greenLight2.visible=false;//works
            break;
            case 3:
            this._stage.getChildAt(1).greenLight3.visible=false;//works
            break;
        }
    }

我是什么在这里失踪?

What am I missing here?

编辑: 确定这里是文档类(至少是重要的是要控制和播放器):

OK here is the document class (at least what is important to Controller and Player):

public class Game extends MovieClip {
    private var _player:Player = new Player(stage);
    private var _controller:Controller = new Controller(_player, stage);

    public function Game():void {
        addChild(_player);
        addEventListener(Event.ADDED_TO_STAGE, added);

下面是Controller类的重要组成部分,我添加了评论什么失败(真正的原因,我开始这一职位摆在首位):

Here is the important part of the Controller class and I added a comment to what is failing (the real reason I started this post in the first place):

public class Controller extends MovieClip {
private var _stage:Stage;
private var _model:Object;

public function Controller(model:Object, stage:Stage):void {
    this._model = model;
    this._stage = stage;

    _stage.addEventListener(KeyboardEvent.KEY_DOWN, processKeyDown);
    _stage.addEventListener(KeyboardEvent.KEY_UP, processKeyUp);
    _stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    this._stage.getChildAt(0).fireButton.addEventListener(MouseEvent.CLICK, onClick); //this is what isn't working
    this._stage.getChildAt(1).greenLight1.visible=false; //added this for testing

我只加了,看看我是否能得到greenLight1去无形的像我一样,当我把它在运行LightsOut()函数我Player.as指出的是,最后一行。但它也不行的时候,在构造放在这里。帮帮我!!!!笑

That last line I only added to see if I could get greenLight1 to go invisible like I can when I call it in the lightsOut() function I noted in Player.as. But it does NOT work either when placed here in the Constructor. HELP!!!! lol

推荐答案

你遇到的问题是,你实例化你的控制器类之前,你有一个舞台对象传递给它。您必须等到以后 ADDED_TO_STAGE 事件被触发实例化一切:

The problem that you're having is that you are instantiating your controller class before you have a stage object to pass to it. You must wait until after the ADDED_TO_STAGE event is fired to instantiate everything else:

public class Game extends MovieClip {
    private var _player:Player;
    private var _controller:Controller;

    public function Game():void
    {
        addEventListener(Event.ADDED_TO_STAGE, added);
    }
    private function added(evt:Event):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, added);
        _player = new Player(stage);
        _controller = new Controller(_player,stage);
        addChild(_player);
    }
}

有一个更好的办法

您并不需要传递的阶段,虽然您的播放器和控制器。如果你只是传递一个引用,你会通过引用文档类。这是一个更好的做法,以进入,因为它prepares您更高级的编程方法,如的设计模式的。

You do not need to pass stage though to your player and controller. If you just pass a reference to this, you will pass a reference to the document class. This is a better practice to get into as it prepares you for more advanced programming approaches like design patterns.

所以,你将有你的文档类:

So you would have your document class:

public class Game extends MovieClip {
    private var _player:Player;
    private var _controller:Controller;

    public function Game():void
    {
        addEventListener(Event.ADDED_TO_STAGE, added);
    }
    private function added(evt:Event):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, added);
        _player = new Player(this);
        _controller = new Controller(_player,this);
        addChild(_player);
    }
}

和您的播放器类:

public class Player extends MovieClip {
    private var docRef:Game;
    private var _lights:uint;

    public function Player($docRef:Game):void {
        this.docRef = $docRef;
        docRef.greenLight1.visible=false; //no longer needs to wait for player to be on the stage
    }
}

的好处是显而易见的,因为你知道你的文档类是在舞台上,玩家并不需要是在舞台上,才可以与它进行交互。它不需要永远是在舞台上。如果你需要的阶段,比如说添加听众喜欢你的控制器做,你可以使用该文件道具:

The advantage is obvious, since you know that you document class is on the stage, Player does not need to be on the stage before it can interact with it. It does not need ever be on the stage. If you need stage, say for adding listeners like you do in the Controller, you can use the documents stage property:

docRef.stage.addEventListener ...