View Full Version : Remove all instances of a Loader
Rundevo
October 8th, 2008, 05:36 AM
Hi guys!
Trying to remove all instances of the thumbLoader, but since I canīt use getChildByName, I donīt know how to do it!
Any ideas?
for (var n:int = 0; n < images.length(); n++)
{
if (images[n].attribute("source") == event.target.name)
{
var thumbList = images.@source[n].parent().parent().image;
for (var o:int = 0; o < thumbList.@thumb.length(); o++)
{
thumbLoader = new Loader();
thumbLoader.load(new URLRequest(thumbList.@thumb[o]));
thumbLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, thumbHandler);
thumbLoader.x = o * 175 + 140;
thumbLoader.y = 590;
addChild(thumbLoader);
thumbLoader.name = thumbList.@source[o];
}
}
}
function thumbHandler(event:Event):void
{
fadeTween=new Tween(event.target.content,"alpha",None.easeNone,0,1,1,true);
}
It would be even better if I could use the Tweenclass to fade them out before removing them, but thatīs overkill for now, but yes, would be nice :)
Thanks in advance
/rundevo
theCodeBot
October 8th, 2008, 07:00 AM
When you create the loaders, add them to an array.
Then you can loop through the array when destroying - each index will be a reference to the loader, so you can say
for(var i=0;i<numLoaders;i++) {
destroy(loaderList[i]);
}
And fading is pretty easy with the tween class once you get it that way.
Rundevo
October 8th, 2008, 08:11 AM
Thanks for your reply!
However Iīm not quite sure what you mean by that, Iīll keep trying but Iīd be as happy as a, well something happy, if you could write afew more lines of code on the matter!
Thanks alot for your help so far, so very very appreciated as Iīm going crazy about this :)
/rundevo
theCodeBot
October 8th, 2008, 03:20 PM
//This line is new
var loaders:Array = [];
for (var n:int = 0; n < images.length(); n++)
{
if (images[n].attribute("source") == event.target.name)
{
var thumbList = images.@source[n].parent().parent().image;
for (var o:int = 0; o < thumbList.@thumb.length(); o++)
{
thumbLoader = new Loader();
thumbLoader.load(new URLRequest(thumbList.@thumb[o]));
thumbLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, thumbHandler);
thumbLoader.x = o * 175 + 140;
thumbLoader.y = 590;
addChild(thumbLoader);
thumbLoader.name = thumbList.@source[o];
//This line is new
loaders[o] = thumbLoader;
}
}
}
function thumbHandler(event:Event):void
{
fadeTween=new Tween(event.target.content,"alpha",None.easeNone,0,1,1,true);
}
//Everything here on is new
function killAllLoaders() {
for(var i=0;i<loaders.length;i++) {
destroy(loaders[i] as MovieClip);
}
}
function destroy(m:MovieClip) {
//1 Second fade
var t:Timer = new Timer(50,20);
t.addEventListener(TimerEvent.TIMER,function(e:Tim erEvent){m.alpha-=5});
t.addEventListener(TimerEvent.COMPLETE,function(e: TimerEvent){m.parent.removeChild(m)});
}
The destroy function is a little sloppy, sorry about that.
Rundevo
October 8th, 2008, 05:49 PM
Gee, thanks mate!
Never done something similar like that before, thereīs not an easier way? :)
Anyways, tried implementing it, but I cant seem to trigger the functions.
My code looks like this now,
function showPicture(event:MouseEvent):void
{
imageLoader=new Loader();
imageLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, imageHandler);
imageLoader.load(new URLRequest(event.target.name));
imageLoader.x=140;
imageLoader.y=120;
addChild(imageLoader);
showNavigation();
imageLoader.addEventListener(MouseEvent.CLICK, removePicture);
// For loop to add the thumbnails to the proper parent image
for (var n:int = 0; n < images.length(); n++)
{
if (images[n].attribute("source") == event.target.name)
{
var thumbList = images.@source[n].parent().parent().image;
var loaders:Array = [];
for (var o:int = 0; o < thumbList.@thumb.length(); o++)
{
thumbLoader = new Loader();
thumbLoader.load(new URLRequest(thumbList.@thumb[o]));
thumbLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, thumbHandler);
thumbLoader.x = o * 175 + 140;
thumbLoader.y = 590;
addChild(thumbLoader);
thumbLoader.name = thumbList.@source[o];
loaders[o] = thumbLoader;
thumbLoader.addEventListener(MouseEvent.ROLL_OVER, thumbOver);
thumbLoader.addEventListener(MouseEvent.CLICK, thumbClick);
function thumbOver(event:MouseEvent):void
{
loadCover.x = event.target.x;
loadCover.y = event.target.y;
loadCover.mouseEnabled = false;
addChild(loadCover);
}
function thumbClick(event:MouseEvent):void
{
alphaMCTE = new MCTE(imageLoader, true, "hide", "Alpha", 7, 200, "Back", "easeNone", 20);
imageLoader=new Loader();
imageLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, imageHandler);
imageLoader.load(new URLRequest(event.target.name));
imageLoader.x=140;
imageLoader.y=120;
addChild(imageLoader);
imageLoader.addEventListener(MouseEvent.CLICK, removePicture1);
}
}
}
function killAllLoaders():void
{
for (var i=0; i<loaders.length; i++)
{
destroy(loaders[i] as MovieClip);
}
}
function destroy(m:MovieClip):void
{
//1 Second fade
var t:Timer = new Timer(50,20);
t.addEventListener(TimerEvent.TIMER, timerEvent);
function timerEvent(e:TimerEvent):void
{
m.alpha-=5;
}
t.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
function timerComplete(e:TimerEvent):void
{
m.parent.removeChild(m);
}
}
test_mc.addEventListener(MouseEvent.CLICK, testRemove);
function testRemove(event:MouseEvent):void
{
killAllLoaders();
}
}
}
So the new functions you so generously helped me with is just outside the for loop but inside the showPicture function, which in turn is inside the xmlLoaded function!
Now, Iīve probably messed up somewhere along the way, but Iīm hoping you could be of great assistance yet again.
Never seen a function set up like you did it before!
Is that a "legal" way of setting up an eventlistener and including the function at the same time? Confused!! :)
Anyways, thankyou thankyou thankyou for all your help so far, I wasnīt even close to solve this before you came along :)
Assuming I set up everything correctly, what should I write in the testRemove function to remove all the thumbnails?
All the best!
/rundevo
Zephadias
October 8th, 2008, 07:34 PM
for one, take the functions definition out of the loop and outside the function. Thats just smart coding in the long run.
theCodeBot
October 8th, 2008, 11:19 PM
for one, take the functions definition out of the loop and outside the function. Thats just smart coding in the long run.
Lol i just now caught that he did that... Yeah, gotta chide for that. It's mostly not working because of that, actually. Don't declare functions from within other functions, and ESPECIALLY not from within a loop. Slow, inefficient, and seldom works besides. The only time you can realistically get away with doing that is what I posted for you (no, not making myself an exception lol)
Rundevo
October 9th, 2008, 06:23 AM
Great :) This E4X stuff is all news to me, and I canīt seem to work out how you could NOT declare a function from within a function when you need the xml to be loaded before you can actually do something with the xml content?
Iīm not doing this as a class, but directly on the timeline, would that change things?
I took the two functions you made out of the showPicture function, but they are all still inside the xmlLoaded function, but still I cant make the loaders go away. And "mostly" not working because of that? :)
Tracing loaders gives me [object loaders].
Anyways, feeling like such a noob here, would be grateful for abit more help :)
/rundevo
theCodeBot
October 9th, 2008, 06:48 AM
Great :) This E4X stuff is all news to me, and I canīt seem to work out how you could NOT declare a function from within a function when you need the xml to be loaded before you can actually do something with the xml content?
Iīm not doing this as a class, but directly on the timeline, would that change things?
I took the two functions you made out of the showPicture function, but they are all still inside the xmlLoaded function, but still I cant make the loaders go away. And "mostly" not working because of that? :)
Tracing loaders gives me [object loaders].
Anyways, feeling like such a noob here, would be grateful for abit more help :)
/rundevo
That trace is correct. And since you have to wait for the XML to load, you're supposed to use Event.COMPLETE on the loader.
//Sample
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE,parseit);
loader.load(new URLRequest("images.xml");
function parseit(e:Event) {
//e.target.data is a reference to the loaded stuff
}
Rundevo
October 9th, 2008, 07:08 AM
I was under the impression you only loaded the xml once, am I wrong there?
var xml:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("http://domain.com/admin/public/portfolio.xml"));
xmlLoader.addEventListener(Event.COMPLETE,xmlLoade d);
//---------------------------------< XML Loaded >-------------------------------//
function xmlLoaded(event:Event):void
{
xml = XML(event.target.data);
images = xml.item.image;
for (var i:int=0; i < imagesToLoad; i++)
{
}
function showPicture(event:MouseEvent):void
{
for (var n:int = 0; n < images.length(); n++)
{
if (images[n].attribute("source") == event.target.name)
{
var thumbList = images.@source[n].parent().parent().image;
var loaders:Array = [];
for (var o:int = 0; o < thumbList.@thumb.length(); o++)
{
}
}
}
function killAllLoaders():void
{
}
function destroy(m:MovieClip):void
{
}
function testRemove(event:MouseEvent):void
{
killAllLoaders();
trace(loaders)
}
}
}
//---------------------------------< /XML Loaded >-------------------------------//
If I take the functions you created outside of the showPicture function, I get an error saying that I cant find "loaders", so they are still within the showPicture function.
But I do get the trace statement to work from the testRemove function and its tracing all the loaders, but they are persistent little bastards and refuse to get removed!
Any ideas?
/rundevo
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.