AS3 ::函数中的值如何成为全局变量

时间:2014-02-19 15:00:42

标签: xml actionscript-3 image-gallery

所以这是我的问题,我有两个课程:

头等舱:MINIATURKA,它按URLRequest

加载所有img

第二课:MINIATURKI,它加载一个XML文件并创建一个类MINIATURKA(loaded jpg)的数组

img具有不同的宽度,我想将它们放在一行中,一个放在另一行旁边,因此类MINIATURKI中的数组代码应该如下所示:

以下是整个MINIATURKI类代码:

package 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.utils.setInterval;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;
    import caurina.transitions.*;

    public class Miniaturki extends MovieClip
    {
        private static var mini:Array;
        private var minaWidth:Number = 0;
        private var loader:URLLoader;
        private var xml1:XML;
        private var draging:Boolean = false;
        public var count:Number= -1;

        public function Miniaturki():void
        {
            loader = new URLLoader();
            loader.load(new URLRequest("obrazki.xml"));
            loader.addEventListener(Event.COMPLETE,onLoadXMLComplete);
        }

        private function onLoadXMLComplete(event:Event):void 
        {
            xml1 = new XML(URLLoader(event.target).data);
            var i:Number;
            mini = new Array(xml1.obrazek.length());
            count = xml1.obrazek.length();
            for (i=0;i<count;i++)
            {
                mini[i] = new Miniaturka(xml1.obrazek[i].attribute("id"),i);
                addChild(mini[i]);
                mini[i].x = i*889;
                trace(mini[i].width);
            }
            MovieClip(parent).nazwa.text = xml1.obrazek[0];
            MovieClip(parent).ilosc.text = "1/"+count;
            MovieClip(parent).scro.buttonMode = true;
            stage.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
        }

        private function beginDrag(event:MouseEvent):void
        {
            var minScroll:Number = 0;
            var maxScroll:Number = MovieClip(parent).scrolbar.width - MovieClip(parent).scro.width;
            var bounds:Rectangle = new Rectangle(minScroll,MovieClip(parent).scro.y,maxScroll,0);
            MovieClip(parent).scro.startDrag(false,bounds);
            stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);   
            MovieClip(parent).scro.addEventListener(Event.ENTER_FRAME,checkingProgress);
            draging = true;
        }   

        private function endDrag(event:MouseEvent):void
        {
            MovieClip(parent).scro.stopDrag();
            draging = false;
        }

        function checkingProgress(event:Event):void
        {   
            var maxScroll:Number = MovieClip(parent).scrolbar.width - MovieClip(parent).scro.width;     
            var procent:Number = MovieClip(parent).scro.x / maxScroll;
            if (draging)
            {
                Tweener.addTween(this,{x:(-procent*(this.width-MovieClip(parent).maska1.width)),time:1});
            }
        }
    }
}

问题在于,当我跟踪mini [i] .width值时,它会在类889的代码中追踪与此值[MINIATURKA]相同的值:button.graphics.drawRect(0, 0, 889, 500); < / p>

以下是整个MINIATURKA类代码:

package 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.display.LoaderInfo;
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.display.DisplayObject;
    import com.anttikupila.utils.JPGSizeExtractor;

    public class Miniaturka extends MovieClip 
    {
        private var je:JPGSizeExtractor = new JPGSizeExtractor();
        private var miniWidth:Number = 0;
        private var id:String;
        private var tween:Tween;
        private var tryb:Boolean;
        private var button:Sprite;
        private var index:Number;
        private var aktywna:Boolean = false;
        public var bLoad:Boolean = false;

        public function Miniaturka(id:String, index:Number):void 
        {
            var je:JPGSizeExtractor = new JPGSizeExtractor();
            this.id = id;
            this.index = index;
            tryb = false;
            var loader:Loader = new Loader();
            loader.load(new URLRequest("images/" + id + "m.jpg"));
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, nLoadComplete);
            je.addEventListener( JPGSizeExtractor.PARSE_COMPLETE, sizeHandler);
            je.extractSize("images/" + id + "m.jpg");

            function sizeHandler( e : Event ) : void 
            {
                trace(je.width);
            }

            button = new Sprite();
            button.graphics.beginFill(0x000000, 0);
            button.graphics.drawRect(0, 0, 889, 500);
            button.graphics.endFill();
            button.buttonMode = true;
            addChild(button);
            button.addEventListener(MouseEvent.MOUSE_OVER, onOver);
            button.addEventListener(MouseEvent.MOUSE_OUT, onOut);
            button.addEventListener(MouseEvent.CLICK, onClick);
            this.alpha = 1;
        }

        private function nLoadComplete(event:Event):void
        {
            var loader:Loader = new Loader();
            loader = LoaderInfo(event.target).loader;
            pusty.addChild(loader);
            tween = new Tween(pusty, "alpha", Regular.easeOut, 0, 0.6, 2, true);
            bLoad = true;
            setStan(false);
            miniWidth = loader.width;
            pusty.alpha = 0;
        }

        private function onOver(event:MouseEvent):void 
        {
        }

        private function onOut(event:MouseEvent):void 
        {
        }

        private function onClick(event:MouseEvent):void 
        {
        }   
    }
}

我无法将按钮精灵的创建放入sizeHandler函数中,因为mini[i].width将跟踪为0:/

正确的值跟踪:trace(je.width);从上面的代码。 如何使je.width值在此行中起作用:button.graphics.drawRect(0, 0, je.width, 500);

甚至可能吗?

还是有另一种方法可以将值直接发送给另一个类吗?

我不想在这个网站上创作一个galery:http://www.adartis.pl/#portfolio

编辑1: 我也在DispatchEVENT的帮助下尝试了sizeHandler nLoadComplete函数,我可以读取Miniaturki类中的值,但我不能在for loop中有效地使用它

1 个答案:

答案 0 :(得分:1)

尝试在Miniaturka中添加回调。当图像加载完成时,将调用回调。回调函数会将Miniaturka添加到MINIATURKI类。

这是一个例子。

public class MINIATURKI 
{

     public function yourFunction():void
     {
         xml1 = new XML(URLLoader(event.target).data);
         var i:Number;
         var currentX:Number = 0;
         mini = new Array(xml1.obrazek.length());            
         count = xml1.obrazek.length();
         for (i=0; i<count; i++)
         {   
             mini[i] = new Miniaturka(xml1.obrazek[i].attribute("id"),i, callback);
         }
     }

     //to save how many images have load complete
     private var loadCompleteCount:int = 0;

     //save the MINIATURKA instances
     private var savedImgs:Array = [];

     //this function will be called when the image load complete in MINIATURKA
     private function callback(m:MINIATURKA, index:int):void
     {

         savedImgs[index] = m;

         loadCompleteCount++;

         if (loadCompleteCount == count)
         {
              //when all load complete, add them
              for each (var ins:MINIATURKA in savedImgs)
              {
                  addChild(ins);
                  ins.x = currentX;
                  currentX += ins.width;
              }
         }
     }
}

关于MINIATURKA班

修改

public function Miniaturka(id:String, index:Number, $callback:Function):void {
    //your code

     imgIndex = index;

     callback =  $callback;
}

private var imgIndex:int;

private var callback:Function;

private function nLoadComplete(event:Event):void
{
        var loader:Loader = new Loader();
        loader = LoaderInfo(event.target).loader;
        pusty.addChild(loader);
        tween = new Tween(pusty, "alpha", Regular.easeOut, 0, 0.6, 2, true);
        bLoad = true;
        setStan(false);
        miniWidth = loader.width;
        pusty.alpha = 0;

        createButton(loader.width);
        callback.apply(null, [this, imgIndex]);
}

private function createButton(imgWidth:int):void
{
    button = new Sprite();
    button.graphics.beginFill(0x000000, 0);

    //if you draw 
    button.graphics.drawRect(0, 0, imgWidth, 500);
    button.graphics.endFill();
    button.buttonMode = true;
    addChild(button);
    button.addEventListener(MouseEvent.MOUSE_OVER, onOver);
    button.addEventListener(MouseEvent.MOUSE_OUT, onOut);
    button.addEventListener(MouseEvent.CLICK, onClick);
}