# AS3 cross fading image loaders

**URL:** <https://forum.kirupa.com/t/as3-cross-fading-image-loaders/265332>\
**Category:** flash\
**Created:** [July 7, 2008, 8:07pm UTC](https://forum.kirupa.com/t/as3-cross-fading-image-loaders/265332 "2008-07-07T20:07:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![reddronin](https://avatars.discourse-cdn.com/v4/letter/r/a9adbd/32.png) [@reddronin](https://forum.kirupa.com/u/reddronin)\
**Post date:** [July 7, 2008, 8:07pm UTC](https://forum.kirupa.com/t/as3-cross-fading-image-loaders/265332/1 "2008-07-07T20:07:40Z")

</div>

Hello everyone,

I’m not new to ActionScript, but am quite dysfunctional as a coder. I mean, please treat me as though I had a severe mental handicap…

I am trying to create a loader that loads an image onto the screen when a button is clicked. When another button is clicked, the first loader’s alpha fades (or is supposed to fade) to zero and the new loader is supposed to fade in to 1 (100%). I used some variables so that the URL would change depending on what button was pushed. Button a loads jpeg a. Button b loads jpeg b. Basic stuff. I also use a boolean variable to determine if loader1 or loader2 has content. I’ve tried using the removeChild() method, but am not quite getting it. No matter what I do I get errors. Also, sometimes this works, sometimes it doesn’t. Usually, it doesn’t. Am I just charging the compiler with too much overhead?

Also, everything is written in the Actions panel. I’m not sure how to write classes, or if I should.

If anyone can assist me, I would greatly appreciate moving on from this problem before I hurl myself into a bathtub filled with razor blades and rubbing alcohol.

Thanks! Code below:

var img:String;  
var dsnButtonOnOff:Boolean;  
var myImageLoader:Loader = new Loader();  
var myImageLoader2:Loader = new Loader();  
var loadVar:Boolean = false;

function loadSketch():void {  
logoTween = new Tween(logo, “alpha”, Strong.easeOut, logo.alpha, 0, 1.5, true);  
logoOffOn = false;  
if (loadVar == false) {  
var imageURLRequest:URLRequest = new URLRequest(“images/sketches/”+img+".png");  
myImageLoader.load(imageURLRequest);  
myImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);  
function imageLoaded(e:Event):void {  
addChild(myImageLoader);  
myImageLoader.alpha = 0;  
var imgAlphaInTween:Tween = new Tween(myImageLoader, “alpha”, Strong.easeOut, myImageLoader.alpha, 1, 2, true);  
myImageLoader.x = 245;  
myImageLoader.y = 149;  
}  
if (myImageLoader2.alpha == 1) {  
var imgAlphaOutTween:Tween;  
imgAlphaOutTween = new Tween(myImageLoader2, “alpha”, Strong.easeOut, myImageLoader2.alpha, 0, 2, true);  
myImageLoader2.contentLoaderInfo.addEventListener(Event.COMPLETE, removeLoader2);  
loadVar = true;  
}  
function removeLoader2(evt:Event):void {  
removeChild(myImageLoader2);  
}  
} else if (loadVar == true) {  
var imageURLRequest2:URLRequest = new URLRequest(“images/sketches/”+img+".png");  
myImageLoader2.load(imageURLRequest2);  
myImageLoader2.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded2);  
function imageLoaded2(e:Event):void {  
addChild(myImageLoader2);  
myImageLoader2.alpha = 0;  
var imgAlphaInTween2:Tween = new Tween(myImageLoader2, “alpha”, Strong.easeOut, myImageLoader2.alpha, 1, 2, true);  
myImageLoader2.x = 245;  
myImageLoader2.y = 149;  
}  
if (myImageLoader.alpha == 1) {  
var imgAlphaOutTween2:Tween;  
imgAlphaOutTween2 = new Tween(myImageLoader, “alpha”, Strong.easeOut, myImageLoader.alpha, 0, 2, true);  
myImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, removeLoader);  
loadVar = false;  
}  
function removeLoader(evt:Event):void {  
removeChild(myImageLoader);  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![reddronin](https://avatars.discourse-cdn.com/v4/letter/r/a9adbd/32.png) [@reddronin](https://forum.kirupa.com/u/reddronin)\
**Post date:** [July 8, 2008, 7:28pm UTC](https://forum.kirupa.com/t/as3-cross-fading-image-loaders/265332/2 "2008-07-08T19:28:30Z")

</div>

Okay, I’ve got this working, save for the fact that I still can’t get the removeChild(); method to work, and I can’t clear the bitmapData out of memory. I think once I figure this out, the above code will work much better… or just work. Does anyone have any suggestions? Thank you!
