add Loader to stage from class

喜夏-厌秋 提交于 2019-12-13 03:08:37

问题


Stage seems to be unaccessible from my class.. almost like it doesn't want to be reached. I do not know what keeps going wrong. This is the class as I have it right now:

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);
    }

}

}

and this is what I do in my swf-file:

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();

The problem is adding iLoader to the stage. When I do that, I receive error:

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

Why can't I access Stage? Or is it plain wrong what I am trying to achieve and should I perceive stage in another way? I really hope you can help me and many many thanks in advance! :)


回答1:


If a display object is not added to the display list, its stage property is set to null (source).
Once you add it to the display list, it will be set to a reference to the stage object.
Try this:

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();


来源:https://stackoverflow.com/questions/15537410/add-loader-to-stage-from-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!