简体   繁体   中英

Stage properties in custom class, not Document Class

I need to use stage.width/height in my CustomClass so I found some topics about it.

if (stage)
            {
                init(ar,firma,kontakt,oferta,naglowek,tekst,dane);
            }
            else
            {
                addEventListener(Event.ADDED_TO_STAGE, init);
            }

But in my case it won't work because it isn't document class, I think. Any other solution?

UPDATE: CLASS CODE

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

            }


        }

    }
}

Hope it will helps.

I expect you are getting an error from the init function which expects lots of parameters but might just get one Event. It helps when you post here if you post the compile or runtime errors you are getting along with source code.

I think this should work for you, I've made a rough version which you can learn from and apply to your own class

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
}

}

Hopefully you can see the concept here, place your constructor variables in class variables when you create the class, then if on the stage call init() which uses those class variables or add an event listener which will call init(passing in an event this time) and then use the same class variables to do what you want.

Note how I remove the event listener when it isn't needed any more as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM