0

我需要在我的 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;
                }

            }


        }

    }
}

希望它会有所帮助。

4

1 回答 1

2

我希望您从 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
}

}

希望您能在这里看到这个概念,在创建类时将构造函数变量放在类变量中,然后如果在舞台上调用使用这些类变量的 init() 或添加将调用 init 的事件侦听器(传入事件 this time) 然后使用相同的类变量来做你想做的事。

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

于 2011-10-14T13:52:13.057 回答