Search code examples
imageactionscriptloadingevent-listener

How to know which one fired eventListener


I am loading images for a game before the game begin. So the main function sends links of images to an image loading object. This is what happen in my image loading object when I do image.load(link) in my main :

    public function charge(str:String, img_x:int, img_y:int, layer:Sprite):void
    {
        trace("load");
        urlRequest = new URLRequest(str);
        
        loaderArray[cptDemande] = new Loader();
        loaderArray[cptDemande].contentLoaderInfo.addEventListener(Event.COMPLETE, loading_done);
        loaderArray[cptDemande].load(urlRequest);
        
        posX[cptDemande] = img_x;
        posY[cptDemande] = img_y;
        
        layerArray[cptDemande] = layer;
        
        cptDemande++;           
    }

The parameters img_x:int, img_y:int and layer:Sprite are related to displaying the images afterward. I am using arrays to be able to add the images to the stage when the loading is all done.

The event listener fire this function :

    public function loading_done(evt:Event):void
    {
        cptLoaded++;
        evt.currentTarget.contentLoaderInfo.removeEventListener(Event.COMPLETE, loading_done);
        if((cptDemande == cptLoaded) && (isDone == true))
        {
            afficher();
        }
    }

what I want is to be able to target the good loader to remove the event listener. What I am currently using(evt.currentTarget) doesn't work and generate an error code :

1069 Property data not found on flash.display.LoaderInfo and there is no default value


Solution

  • Tracing evt.currentTarget shows that currentTarget is the LoaderInfo property. Try updating your code as follows:

    public function loading_done(evt:Event):void
    {
        cptLoaded++;
    
        // Current target IS the contentLoaderInfo
        evt.currentTarget.removeEventListener(Event.COMPLETE, loading_done);
        //evt.currentTarget.contentLoaderInfo.removeEventListener(Event.COMPLETE, loading_done);
        if((cptDemande == cptLoaded) && (isDone == true))
        {
            afficher();
        }
    }
    

    Just a wee tip for you while I'm at it, you could make life a lot easier for yourself by storing all the properties of your images on an Object and then pushing these onto a single Array, rather than managing a separate Array for each property.

    Something like this:

        private var loadedImages:Array = new Array();       
    
        public function charge(str:String, img_x:int, img_y:int, layer:Sprite):void
        {   
            var urlRequest:URLRequest = new URLRequest(str);
    
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loading_done);
            loader.load(urlRequest);
    
            var imageData:Object = { };
            imageData.loader = loader;
            imageData.posX = img_x;
            imageData.posY = img_y;
            imageData.layer = layer;
    
            // Now we have a single Array with a separate element for 
            // each image representing all its properties
            loadedImages.push(imageData);
    
            cptDemande++;           
        }