本文介绍了从添加类加载器阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

舞台似乎是从我的类不可访问..几乎像它不希望要达到的。我不知道什么让走错了。这是类,因为我有它现在:

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

}

}

这是我做我的swf文件:

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

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

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! :)

推荐答案

如果显示对象未添加到显示列表,则其stage属性设置为 (来源)。
一旦你将它添加到显示列表,它会被设置为一个参考的阶段目标。
试试这个:

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

这篇关于从添加类加载器阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 18:54