# Problem with Sprites on slideshow

**URL:** <https://forum.kirupa.com/t/problem-with-sprites-on-slideshow/311728>\
**Category:** flash\
**Created:** [July 14, 2010, 8:36pm UTC](https://forum.kirupa.com/t/problem-with-sprites-on-slideshow/311728 "2010-07-14T20:36:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![StackOverflow](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@StackOverflow](https://forum.kirupa.com/u/StackOverflow)\
**Post date:** [July 14, 2010, 8:36pm UTC](https://forum.kirupa.com/t/problem-with-sprites-on-slideshow/311728/1 "2010-07-14T20:36:22Z")

</div>

Okay, I got it to work without Sprites, but I had to use them after some modifications I was asked to do. The problem is that I can’t seem to find the right way to add/remove them…

CODE ----------------------------------------------------  
/\* Cutting imports \*/

var imageLoader:Loader = new Loader();  
var imageLoader2:Loader = new Loader();  
var myTween:TweenLite;  
var myImages\_array:Array = new Array;

var img = 0;  
var ordre = 0;  
var image\_total = 0;

var transition = 0;  
var musique = 0;  
var duree = 0;  
var site\_root = “”;

function load\_xml():void  
{  
myImages\_array = [];  
var rnd = Math.floor(Math.random()\*100000000);  
var url:String = root.loaderInfo.parameters.site\_root + “services/slideshow.ashx?id=” + root.loaderInfo.parameters.sujet\_no + “&rn=” + rnd;  
var loadit:URLLoader = new URLLoader();  
loadit.addEventListener(Event.COMPLETE, completeHandler);  
loadit.load(new URLRequest(url));

```
function completeHandler(e:Event):void {
    var myXML:XML = new XML(e.target.data);
    var myOptions:XMLList = myXML.options.elements();
    var myImages:XMLList = myXML.images.elements();
    
    transition = myOptions.(@id=="transition");
    musique = myOptions.(@id=="musique");
    duree = myOptions.(@id=="duree");
    
    for each(var image:XML in myImages)
    {
        //myImages_array.push(image.@["path"]);
        var loader:Loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
        loader.load(new URLRequest(root.loaderInfo.parameters.site_root + image.@["path"]));
    }
    // After the load, we should have a new array (myImages_array) with all the sprites in it (if I do addChild(myImages_array[0]) here, it works)
    // However, it won't work when I try it on my MCs...
    
    init_slideshow();
}

```

}

function imageLoaded(e:Event):void  
{  
var image:Bitmap = e.target.loader.content;

```
if (image.width &gt; 1920)
{
    image.scaleY = 1920 / image.width;
    image.width = 1920;
}
if (image.height &gt; 1080)
{
    image.scaleX = 1080 / image.height;
    image.height = 1080;
}

var imageData:BitmapData = image.bitmapData;

var sprite:Sprite = new Sprite();

sprite.graphics.beginFill(0xFFFFFF);
sprite.graphics.drawRect(0, 0, 1920, 1080);
sprite.graphics.endFill();

var spriteBitmap:Bitmap = new Bitmap(imageData);

spriteBitmap.x = (1920 / 2) - (spriteBitmap.width / 2);
spriteBitmap.y = (1080 / 2) - (spriteBitmap.height / 2);

sprite.addChild(spriteBitmap);

myImages_array.push(sprite); *// Sprites are successfully added to the array*

```

}

function init\_slideshow()  
{  
image\_total = myImages\_array.length;

```
// Load the first two images (for the fade effect)
load_image(imageLoader,img);
load_image(imageLoader2,img+1);
img = img + 1;
animate_slideshow();

```

}

function animate\_slideshow()  
{  
if(ordre == 0)  
{  
myTween = new TweenLite(myLoader, 2, {alpha:0,delay:duree, onComplete:next\_slide});  
}  
else  
{  
myTween = new TweenLite(myLoader2, 2, {alpha:0,delay:duree, onComplete:next\_slide});  
}  
}

function next\_slide()  
{  
if(img \>= image\_total-1)  
{  
img = -1;  
}

```
if(ordre == 0)
{
    setChildIndex(myLoader,0);
    setChildIndex(myLoader2,1);
    
    myLoader.alpha = 1;
    **load_image(myLoader,img+1);***//myLoader = MC on stage*
    //load_image(imageLoader,img+1);*//imageLoader = Loader*
    img = img + 1;

    ordre = 1;
}
else
{
    setChildIndex(myLoader,1);
    setChildIndex(myLoader2,0);
    
    myLoader2.alpha = 1;
    **load_image(myLoader2,img+1);***// **myLoader 2 =** MC on stage*
    //load_image(imageLoader2,img+1);*// **imageLoader2 =** Loader*
    img = img + 1;

    ordre = 0;
}

animate_slideshow();

```

}

function load\_image(myMc,img)  
{  
\*\*myMc.unload(); \*\*_// Used when I was doing .load(), not useful anymore I guess…_

```
if(ordre == 0)
{
    for(var i:uint = 0; i &lt; myLoader.numChildren; i++){
           myLoader.removeChildAt(i);
    }
}
else
{
    for(var z:uint = 0; z &lt; myLoader2.numChildren; z++){
          myLoader2.removeChildAt(z);
    }
}

**myMc.addChild(myImages_array[img]); *// Gives me an error***

```

}

imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event){  
myLoader.addChild(e.target.content);  
});

imageLoader2.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event){  
myLoader2.addChild(e.target.content);  
});

## load\_xml();

myLoader and myLoader2 are MovieClips in the stage (timeline). imageLoader and imageLoader2 were used to load images before. I think I will not need those anymore, but can someone tell me how to add the sprites into my slideshow? Thanks!
