【问题标题】:add Loader to stage from class将加载器从类添加到舞台
【发布时间】:2013-03-10 08:46:39
【问题描述】:

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

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?或者我想要达到的目标是完全错误的,我应该以另一种方式感知舞台吗?我真的希望你能帮助我,非常感谢提前! :)

【问题讨论】:

  • this.stage.addChild() 导致同样的错误

标签: actionscript-3 actionscript flash-cs4


【解决方案1】:

如果显示对象未添加到显示列表中,则其舞台属性设置为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();

【讨论】:

    猜你喜欢
    • 2015-09-21
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 2010-11-17
    相关资源
    最近更新 更多