Question

So this is my problem I have two classes:

First class: MINIATURKA which loads all img by URLRequest

Second class: MINIATURKI which loads a XML file and creates an array of class MINIATURKA(loaded jpg)

The img have different widths, and I want to place them in a row, one next to the another, so the code of the Array in class MINIATURKI should look like this:

Here is the whole MINIATURKI class code:

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

The problem is that when i trace the mini[i].width value it traces the same value as this value [889] in the code of class MINIATURKA: button.graphics.drawRect(0, 0, 889, 500);

Here is the whole MINIATURKA class code:

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

I can't put the creation of button sprite into the sizeHandler function, because the mini[i].width will trace as 0 :/

The proper value traces the: trace(je.width); from the above code. How to make the: je.width value work in this line as: button.graphics.drawRect(0, 0, je.width, 500);

Is it even possible?

Or is there another way to send the value direct to the another class?

I wan't to crate a galery like on this website: http://www.adartis.pl/#portfolio

EDIT 1: I have also tried with help of DispatchEVENT puted into either sizeHandler either nLoadComplete function, and I could read the value in the class Miniaturki, but I coudln't use it effectively in the for loop

Was it helpful?

Solution

Try to add a callback in Miniaturka. when the image load complete., the callback will be called. And the callback function will add Miniaturka to MINIATURKI class.

Here is an example.

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

And about MINIATURKA class

EDIT

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top