actionscript-3apache-flexflex4.5flash-builder4.5

Change image dynamically in Flash Builder 4.6


In my code I have defined the following: <s:Image id="test" x="50" y="50" width="30" height="30" click="onClick_clickHandler(event)" smooth="true" smoothingQuality="high" source="@Embed('icons/myImage_60_off.png')"/> What I want is to be able to change the source of the image every time the user clicks on the image - similar to the way favourites work on a browser. I have no idea how to change the source of the image from my code.

Thank you


Solution


  • I finally did it!

    public var image_loader:Loader; // define a new loader

    image_loader = new Loader(); // create the new loader at the desired location (in my case in the initialization method of the page

    image_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded); //add a listener and a function to assign to the listener
    depending on where the command has to be executed

    image_loader.load(new URLRequest('location of the image')); //this will load the image dynamically

    function imageLoaded(event:Event):void {image_Id.source = image_loader;} //where image_Id is the id of the s:Image tag to be modified

    also, I had to remove the source from the s:Image tag previously posted
    The rest is just logic of the way the application needs to implement the functionality, so it's pretty much left to the developer's desires