0

我的班级似乎无法进入舞台......几乎就像它不想达到一样。我不知道什么一直出错。这是我现在拥有的课程:

package {

import flash.events.*;
import flash.display.*;
import flash.net.*;

public class gallery extends Sprite{

    private var imgPath:String = 'images/';
    private var imgCurrent:int = 0;
    private var images:Array = new Array();
    private var iLoader:Loader;

    function gallery()
    {
        this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    private function onAddedToStage(e:Event):void {
        this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        sayStage();
    }

    public function sayStage():void {
        trace(this.stage);
    }

    public function setImgs(val:Array):void
    {
        for (var index:String in val){
            images[index] = val[index];
        }
    }

    public function getImgs():void
    {
        for (var index:String in images){
            trace(index + ':' + images[index] + ';');
        }
    }

    public function loadImg():void{

        iLoader = new Loader();
        iLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressStatus);
        iLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderReady);

        var fileRequest:URLRequest = new URLRequest(imgPath+'testimg.JPG');
        iLoader.load(fileRequest);

    }

    public function onProgressStatus(e:ProgressEvent) {   
        trace(e.bytesLoaded, e.bytesTotal); 
    }

    public function onLoaderReady(e:Event) {     
        stage.addChild(iLoader); // error!
    }

    public function updateImgCurrent(val:int):void
    {
        imgCurrent = imgCurrent + val;
    }

    public function getImgCurrent():int
    {
        return(imgCurrent);
    }

}

}

这就是我在我的 swf 文件中所做的:

var gal:gallery = new gallery();
var imagesGallery:Array = new Array();
imagesGallery.push('testimg.JPG');
imagesGallery.push('img2.JPG');
imagesGallery.push('img3.JPG');
gal.setImgs(imagesGallery);
gal.loadImg();

问题是将 iLoader 添加到舞台。当我这样做时,我收到错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at gallery/onLoaderReady()

为什么我无法访问 Stage?或者我想要达到的目标是完全错误的,我应该以另一种方式感知舞台吗?我真的希望你能帮助我,非常感谢提前!:)

4

1 回答 1

2

如果显示对象未添加到显示列表中,则其 stage 属性设置为null (source)
将其添加到显示列表后,它将被设置为对舞台对象的引用。
试试这个:

var gal:gallery = new gallery();
addChild(gal); // this will trigger ADDED_TO_STAGE event and initialize stage property
var imagesGallery:Array = new Array();
imagesGallery.push('testimg.JPG');
imagesGallery.push('img2.JPG');
imagesGallery.push('img3.JPG');
gal.setImgs(imagesGallery);
gal.loadImg();
于 2013-03-21T01:01:02.213 回答