How do I copy an image loaded into a movie clip into another movie clip?
I have written the following code to produce a slide show effect, complete with fade-in and fade-out. It works fine when I run it from a local directory. When I run from my web server, however, it takes too long to load the next external jpeg. As a result, I get more of a cut-in, fade-out effect.
I'm sure the problem is that I wait until I'm finished with my first picture before I load in the next one. My initial thought is to load the next image in the background while the first image is on screen. So I'd like to add in another movie clip to queue the next picture. In other words:
1. load the first pic
2. fade in the first pic
3. start loading the second pic while the first is static on screen
4. fade out the first pic
5. copy the second pic into the holder for the first pic
6. repeat steps 2--5 indefinitely
Here's my code:
ActionScript Code:
///////////////////////////////////////////////////////////
// This component uses a series of //
// consecutively numbered image files located in the //
// subdirectory '/right' to create a slideshow effect in //
// an empty movie clip. For each image, it fades the //
// image in, waits eight seconds, then fades the image //
// out. You must specify the number of images in the //
// subdirectory using the maxNum parameter below. For //
// example, if the first image is 'right0.jpg' and the //
// last is 'right9.jpg', then maxNum is set to 10. Note: //
// image file numbering must always start with zero! //
///////////////////////////////////////////////////////////
// number of image files (numbered 0...maxNum - 1)
var maxNum = 6;
// create an empty movie clip to hold the image
this.createEmptyMovieClip ("rightClip", 1);
// select the initial picture at random
var picNum = Math.floor(maxNum * Math.random());
var fileName = "right/right" + picNum +".jpg";
// set the starting alpha to transparent
var clipAlpha = 0;
this["rightClip"]._alpha = clipAlpha;
// load the image into the empty movie clip
this["rightClip"].loadMovie(fileName);
// initialize the other variables
var direction = "up";
var counter = 0;
///////////////////////////////////////////////////////////
// function onEnterFrame
//
// This function fades the image in, holds it on screen,
// or fades it out depending on the setting of the
// direction variable.
///////////////////////////////////////////////////////////
this.onEnterFrame = function()
{
///////////////////////////////////////////////////////
// fade in
///////////////////////////////////////////////////////
if (direction == "up")
{
// increase the opacity
clipAlpha += 5;
this["rightClip"]._alpha = clipAlpha;
// hold if at maximum opacity
if (clipAlpha >= 100) {
this["rightClip"]._alpha = 99;
direction = "hold";
}
}
//////////////////////////////////////////////////////////
// hold
//////////////////////////////////////////////////////////
else if (direction == "hold")
{
// increment the counter
counter++;
// fade out if time has elapsed
if (counter == 160)
{
direction = "down";
}
}
//////////////////////////////////////////////////////////
// fade out
//////////////////////////////////////////////////////////
else if (direction == "down")
{
// decrease the opacity
clipAlpha -= 5;
this["rightClip"]._alpha = clipAlpha;
// set up for the next image if at minimum opacity
if (clipAlpha <= 0)
{
this["rightClip"]._alpha = 0;
direction = "up";
counter = 0;
picNum = (picNum + 1) % maxNum;
fileName = "right/right" + picNum +".jpg";
this["rightClip"].loadMovie(fileName);
}
}
}
Any suggestions?