Copy link to clipboard
Copied
I'm showing image from tilelist on mouse click.
but how can i clear previous image on next mouse click?
var XMLSource:String = "imagelist.xml";
var xmlImglist:XML;
var urlLoader:URLLoader = new URLLoader();
var urlRequest:URLRequest;
urlRequest = new URLRequest(XMLSource);
urlLoader.addEventListener(Event.COMPLETE, playlistLoaded);
urlLoader.load(urlRequest);
function playlistLoaded(e:Event):void
{
xmlImglist = new XML(urlLoader.data);
for (var i:int=0; i<xmlImglist.BgImgList.length(); i++)
{
trace(i);
bgImageList.addItem({source:xmlImglist.BgImgList});
bgImageList.addEventListener(Event.CHANGE, showImage);
}
}
var imgLoader:Loader;
function showImage(e:Event):void
{
var imagepath:String = bgImageList.selectedItem.source;
imgLoader = new Loader();
addChild(imgLoader);
imgLoader.x = area_mc.x + 1;
imgLoader.y = area_mc.y + 20;
imgLoader.load(new URLRequest(imagepath));
}
One way would be to use the same Loader instaed of creating a new one every time. Since a Loader can only hold one object at a time, if you reload the same one with something new, then whatever it was holding beforehand is removed.
Copy link to clipboard
Copied
One way would be to use the same Loader instaed of creating a new one every time. Since a Loader can only hold one object at a time, if you reload the same one with something new, then whatever it was holding beforehand is removed.
Copy link to clipboard
Copied
Try this:
var imgLoader:Loader;
function showImage(e:Event):void
{
if ( imgLoader ) { // if was added
removeChild(imgLoader); // remove if was added
imgLoader = null; // remove from the memory old image
}
var imagepath:String = bgImageList.selectedItem.source;
imgLoader = new Loader();
addChild(imgLoader);
imgLoader.x = area_mc.x + 1;
imgLoader.y = area_mc.y + 20;
imgLoader.load(new URLRequest(imagepath));
}
each time you call showImage - flash will remove old image and upload new one
Copy link to clipboard
Copied
As Ned suggested - reuse the same loader. The code below works if you don't need any transitions.
import flash.display.Loader;
var XMLSource:String = "imagelist.xml";
var imgLoader:Loader;
var xmlImglist:XML;
var urlLoader:URLLoader = new URLLoader();
var urlRequest:URLRequest;
urlRequest = new URLRequest(XMLSource);
urlLoader.addEventListener(Event.COMPLETE, playlistLoaded);
urlLoader.load(urlRequest);
initLoader();
function initLoader():void
{
imgLoader = new Loader();
imgLoader.x = area_mc.x + 1;
imgLoader.y = area_mc.y + 20;
addChild(imgLoader);
}
function playlistLoaded(e:Event):void
{
xmlImglist = new XML(urlLoader.data);
for (var i:int = 0; i < xmlImglist.BgImgList.length(); i++)
{
trace(i);
bgImageList.addItem({source: xmlImglist.BgImgList});
bgImageList.addEventListener(Event.CHANGE, showImage);
}
}
function showImage(e:Event):void
{
imgLoader.unload();
imgLoader.load(new URLRequest(bgImageList.selectedItem.source));
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now