Actionscript 3 自定义类中的阶段属性,而不是文档类

Actionscript 3 自定义类中的阶段属性,而不是文档类,actionscript-3,flash-cs5,document-class,Actionscript 3,Flash Cs5,Document Class,我需要在我的CustomClass中使用stage.width/height,所以我找到了一些关于它的主题 if (stage) { init(ar,firma,kontakt,oferta,naglowek,tekst,dane); } else { addEventListener(Event.ADDED_TO_STAGE, init

我需要在我的CustomClass中使用stage.width/height,所以我找到了一些关于它的主题

if (stage)
            {
                init(ar,firma,kontakt,oferta,naglowek,tekst,dane);
            }
            else
            {
                addEventListener(Event.ADDED_TO_STAGE, init);
            }
但在我的例子中,它不会起作用,因为我认为它不是文档类。还有其他解决办法吗

更新: 类别代码

package 
{
    import fl.transitions.Tween;
    import fl.motion.easing.*;
    import flash.filters.*;
    import flash.events.MouseEvent;
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.ui.Mouse;
    import flash.display.*;
    public class Wyjazd extends MovieClip
    {
        public function Wyjazd(ar:Array=null,firma:Object=null,kontakt:Object=null,oferta:Object=null,naglowek:Object=null,tekst:Object=null,dane:Object=null)
        {
            if (stage)
            {
                //The stage reference is present, so we're already added to the stage
                init(ar,firma,kontakt,oferta,naglowek,tekst,dane);
            }
            else
            {
                addEventListener(Event.ADDED_TO_STAGE, init);
            }


        }

        public function init(ar:Array,firma:Object=null,kontakt=null,oferta:Object=null,naglowek:Object=null,tekst:Object=null,dane:Object=null):void
        {

            //Zmienne "globalne" dla funkcji
            var time:Number;
            var wciecie:Number;
            var wciecie2:Number;
            var offset:Number = 15.65;
            var offset2:Number = 20;
            var posX:Array = new Array(12);
            var posY:Array = new Array(12);
            var spr:Array = new Array(12);
            var targetLabel:String;
            var wybranyOb:Object = ar[0];
            var names:Array = new Array('Szkolenie wstępne BHP','Szkolenie okresowe BHP','Szkolenie P.Poż','Kompleksowa obsługa P.Poż','Pomiar środowiska pracy','Szkolenie z udzielania pierwszej pomocy','Ocena ryzyka zawodowego','Przeprowadzanie postępowań po wypadkowych','Przeprowadzanie audytów wewnętrznych ISO','Hałas w środowisku komunalnym','Medycyna pracy','Szkolenia dla kierowców');
            //Pobieranie pozycji
            for (var i:Number = 0; i<ar.length; i++)
            {
                posX[i] = ar[i].x;
                posY[i] = ar[i].y;

            }



            //Filtry
            function increaseBlur(e:MouseEvent,docPos:Number):void
            {
                var myBlur:BlurFilter =new BlurFilter();
                myBlur.quality = 3;
                myBlur.blurX = 10;
                myBlur.blurY = 0;
            }
            //Funkcje
            function startPos():void
            {
                time = 0.2;
                for (var i:Number = 0; i<ar.length; i++)
                {
                    //if (wybranyOb.name == ar[i].name)
                    //{
                    //var wybranyPos:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],0.01,true);
                    //wybranyPos = new Tween(ar[i],"y",Linear.easeOut,-30,posY[i],time,true);
                    //}
                    //else
                    //{
                    var position:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
                    position = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
                    //}
                    //time = 0.2;
                }
                position = new Tween(naglowek,"x",Linear.easeOut,naglowek.x,2000,time,true);
                position = new Tween(tekst,"x",Linear.easeOut,tekst.x,2000,time,true);
                position = new Tween(dane,"x",Linear.easeOut,dane.x,2000,0.25,true);
            }

            //Nasłuchy
            oferta.addEventListener(MouseEvent.CLICK, wyskokOferta);
            oferta.addEventListener(MouseEvent.MOUSE_OVER,glowOferta);
            oferta.addEventListener(MouseEvent.MOUSE_OUT,unglowOferta);
            kontakt.addEventListener(MouseEvent.CLICK,wyskokKontakt);
            kontakt.addEventListener(MouseEvent.MOUSE_OVER,glowKontakt);
            kontakt.addEventListener(MouseEvent.MOUSE_OUT,unglowKontakt);
            firma.addEventListener(MouseEvent.CLICK,wyskokFirma);
            firma.addEventListener(MouseEvent.MOUSE_OVER,glowFirma);
            firma.addEventListener(MouseEvent.MOUSE_OUT,unglowFirma);
            function glowFirma(e:MouseEvent):void
            {
                var myGlow:GlowFilter=new GlowFilter();
                myGlow.color = 0xe6da13;
                myGlow.inner = true;
                firma.filters = [myGlow];
            }
            function unglowFirma(e:MouseEvent):void
            {
                firma.filters = [];
            }
            function glowKontakt(e:MouseEvent):void
            {
                var myGlow:GlowFilter=new GlowFilter();
                myGlow.color = 0xe6da13;
                myGlow.inner = true;
                kontakt.filters = [myGlow];
            }
            function unglowKontakt(e:MouseEvent):void
            {
                kontakt.filters = [];
            }
            function glowOferta(e:MouseEvent):void
            {
                var myGlow:GlowFilter=new GlowFilter();
                myGlow.color = 0xe6da13;
                myGlow.inner = true;
                oferta.filters = [myGlow];
            }
            function unglowOferta(e:MouseEvent):void
            {
                oferta.filters = [];
            }
            function wyskokKontakt(e:MouseEvent):void
            {
                startPos();
                var tweenKontakt = new Tween(dane,"x",Linear.easeOut,2000,350,0.25,true);

            }
            function wyskokFirma(e:MouseEvent):void
            {
                startPos();
                trace("Firma");
            }
            function wyskokOferta(e:MouseEvent):void
            {

                time = 0.2;
                wciecie = 15.65;
                wciecie2 = 20.05;
                for (var i:Number = 0; i < ar.length; i++)
                {

                    var tween:Tween = new Tween(ar[i],"x",Sine.easeOut,ar[i].x,oferta.x + wciecie,time,true);
                    tween = new Tween(ar[i],"y",Sine.easeOut,ar[i].y,oferta.y + wciecie2,time,true);

                    ar[i].addEventListener(MouseEvent.CLICK,onClick);
                    spr[i] = i;

                    time +=  0.02;
                    wciecie +=  offset;
                    wciecie2 +=  offset2;
                }
            }

            function onClick(e:MouseEvent)
            {
                startPos();
                time = 0.2;
                var k:Number = 0;
                targetLabel = e.currentTarget.name;

                for (var i:Number = 0; i < ar.length; i++)
                {
                    if (targetLabel==ar[i].name)
                    {
                        //wybranyOb = ar[i];
                        var tween:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
                        tween = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
                        tween = new Tween(naglowek,"x",Linear.easeOut,2000,60,0.2,true);
                        tween = new Tween(tekst,"x",Linear.easeOut,2000,500,0.25,true);
                        naglowek.text = names[i];


                    }
                    else
                    {
                        var tween1:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
                        tween1 = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
                    }
                    //time +=  0.02;
                }

            }


        }

    }
}
包
{
导入fl.transitions.Tween;
导入fl.motion.easing.*;
进口闪光滤光片。*;
导入flash.events.MouseEvent;
导入flash.display.Stage;
导入flash.display.MovieClip;
导入flash.ui.Mouse;
导入flash.display.*;
公共类Wyjazd扩展了MovieClip
{
公共函数Wyjazd(ar:Array=null,firma:Object=null,kontakt:Object=null,oferta:Object=null,naglowek:Object=null,tekst:Object=null,dane:Object=null)
{
如果(阶段)
{
//阶段引用已存在,因此我们已添加到阶段
初始(ar、firma、kontakt、oferta、naglowek、tekst、丹麦);
}
其他的
{
addEventListener(Event.ADDED_TO_STAGE,init);
}
}
公共函数init(ar:Array,firma:Object=null,kontakt=null,oferta:Object=null,naglowek:Object=null,tekst:Object=null,dane:Object=null):void
{
//Zmienne“globalne”dla funkcji
var时间:个数;
变量wciecie:编号;
var wciecie2:编号;
风险值偏移量:数值=15.65;
var抵销2:数量=20;
var posX:Array=新数组(12);
变量posY:Array=新数组(12);
var spr:数组=新数组(12);
var-targetLabel:String;
var-wybranyOb:Object=ar[0];
变量名称:数组=新数组“Szkolenie P.P.P.P.P.P.P.P.P.380“,“Szkolenie P.P.P.P.P.Po“,“KoKoKoleniE P.P.P.P.P.P.P.P.P.P',“BZKoleniE S KoleniE w w.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P',“P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P,”,”,”,”,”,”,”,”,“P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P.P,“P阿帕西','Szkolenia dla kierowców');
//波比拉尼

对于(var i:Number=0;i我希望您从init函数中得到一个错误,该函数需要很多参数,但可能只得到一个事件。如果您在这里发布编译或运行时错误,并且与源代码相处融洽,这将对您有所帮助

我认为这应该对你有用,我已经做了一个粗略的版本,你可以从中学习并应用到你自己的课堂上

public class CustomClass extends MovieClip
{

protected var _company:String;
protected var _data:Object;

public function CustomClass( company:String='', data:Object=null )
{
    _company = company;
    _data = data;
    if (stage)
    {
        init();
    }
    else 
   {
       addEventListener(Event.ADDED_TO_STAGE, init);
   }

public function init(e:Event=null):void
{
    removeEventListener(Event.ADDED_TO_STAGE, init);
    //do something with _data
    //do something with _company
}

}
希望您能在这里看到这个概念,在创建类时将构造函数变量放在类变量中,然后在使用这些类变量的stage call init()上,或者添加一个事件侦听器,该侦听器将调用init(这次传入一个事件),然后使用相同的类变量来执行您想要的操作


请注意,当不再需要事件侦听器时,我是如何删除它的。

您的类是否扩展了
DisplayObject
,它是否应该在显示列表中?您是否控制文档类或显示列表中的任何
DisplayObject
,或者它是某些纯逻辑/不可见组件的一部分?我的类扩展了MovieCli它在显示列表中。我控制一些按钮。您的init函数是否将…rest作为其参数?否则它只需要使用一个事件类参数,例如:function init(e:Event):void此代码在CustomClass中的何处?在其构造函数中?我得到了“Error 1120:Access of udefined property Event”指向addEventListener(Event.ADDED_TO_STAGE,init);确定该错误表示尚未导入类事件。因此,只需将import flash.events.Event;与其余导入一起添加即可。