Make Your Own Cross-Fading Slideshow
UPDATE: This post demonstrates how to build a cross-fading slide show with embedded images. Since it was written I’ve posted another cross-fade sample that utilizes XML. If you’d rather see that one, click here.
Ever wonder how to get that way-cool cross fading effect in your slide shows? Me too! So I sat down the other day and started toying around with the Tween class and a short time later I arrived at the solution below.
I start off by importing the Tween and easing classes. The first line imports the Tween class and the second imports all of the easing classes:
import mx.transitions.Tween;
import mx.transitions.easing.*;
Next I define a few required variables:
var currClip:MovieClip = pic1;
var arrPosition:Number = -1;
var picArray:Array = new Array("pic1","pic2","pic3","pic4","pic5");
var showpicTime:Number = 5;
var tweenTime:Number = 3;
In this example, cross fading was achieved by alternating two movie clips named pic1 and pic2. When one fades in, the other fades out and vice versa. I use currClip to define the active clip (the clip that last faded in) currClip. More about that later. The cross-fade is actually one clip tweening from an _alpha of 100 to an _alpha of 0 while the other tweens from 0 to 100.
For simplicity, I’m using embedded images with defined Identifiers and placing those identifiers in a hard-coded array named picArray. The arrPosition variable will keep track of which image should be displayed next. The code can be easily modified to use external images and XML.
showPicTime defines the number of seconds that each image will be display at 100% alpha before fading out and tweenTime is the number of seconds it will take for the tween to complete.
My next step is to attach the initial images and fade the first in:
pic1.attachMovie(picArray[++arrPosition],"pic",_root.getNextHighestDepth());
pic2.attachMovie(picArray[++arrPosition],"pic",_root.getNextHighestDepth());
pic1._alpha = pic2._alpha = 0;
new Tween(pic1, "_alpha", Regular.easeIn, 0, 100 , 3, true);
var picInt:Number = setInterval(crossfade,showpicTime*1000);
++arrPosition is a quick way to increase the value of arrPosition by one while using it. Since I initially set arrPosition to -1, the first statement is actually attaching the image defined in the first (0) element of picArray and the second statement is attaching image in the the second (1) element.
Next I set the _alpha of both clips to 0 and begin the initial Tween on the pic1. The “new Tween” line tweens the _alpha property of pic1 from 0 to 100 over the course of 3 seconds. The ‘true’ at the end makes the tween happen over seconds. Changing that value to false would result in the tween taking place over a number of frames instead of seconds.
The variable picInt causes the function named crossfade to fire every 5000 milliseconds (5 seconds). This interval can be changed by changing the value of showpicTime.
The final piece of the puzzle is to define the crossfade function:
function crossfade() {
if (currClip == pic1) {
var tween1:Tween = new Tween(pic2, "_alpha", Regular.easeIn, 0, 100 , tweenTime, true);
var tween2:Tween = new Tween(pic1, "_alpha", Regular.easeIn, 100, 0 , tweenTime, true);
if(++arrPosition >= picArray.length) {arrPosition = 0;}
tween2.onMotionFinished = function() {
pic1.pic.removeMovieClip();
pic1.attachMovie(picArray[arrPosition],"pic",pic1.getNextHighestDepth());
}
currClip = pic2;
} else {
var tween1 = new Tween(pic1, "_alpha", Regular.easeIn, 0, 100 , tweenTime, true);
var tween2 = new Tween(pic2, "_alpha", Regular.easeIn, 100, 0 , tweenTime, true);
if(++arrPosition >= picArray.length) {arrPosition = 0;}
tween2.onMotionFinished = function() {
pic2.pic.removeMovieClip();
pic2.attachMovie(picArray[arrPosition],"pic",pic1.getNextHighestDepth());
}
currClip = pic1;
};
};
The first thing the function does is check which clip is the active clip so it knows which clip to fade in and which clip to fade out. It then starts the tween for both pic1 and pic2. The clip that is fading out is monitored by an onMotionFinished event which removes the existing image and attaches the next image in the array as soon as the fade out is complete. That way, the new image is ready to fade in once the active image begins to fade out.
The final line of code sets the value of currClip to the active clip so the appropriate tweens run the next time crossfade is fired.
-rG
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.



Hi there,
Thanks heaps for this code, Im a flash noob and ive searched high and low for something like this, cheers!!
A quick of question:
Why is it when I try to add additional images to the source file provided, they wont show up? These are the steps Im taking:
Add a new image to my library (Bitmap 6 – following same naming convention as used in the example),
convert that image to a movie clip called pic6,
add it to my array in the actionscript on the 3rd frame,
dragging the movieclip onto the second frame on top of the other images.
Here is the variable declaration:
var currClip:MovieClip = pic1;
var arrPosition:Number = -1;
var picArray:Array = new Array(“pic1″,”pic2″,”pic3″,”pic4″,”pic5″,”pic6″);
var showpicTime:Number = 2;
var tweenTime:Number = 1;
Can anyone see something glaringly obvious that I am missing out?
Once again, thanks for the code and the help
You also have to give the library object a linkage identifier. Right-click on pic6 in the library then click linkage then check the “Export for ActionScript box. pic6 should then show up as the identifier. Click OK and you should be good to go.
Cheers, knew it would be something easy
Hi – thanks for the great script – I’m a beginner with actionscript though I do php/mysql – anyway I thought I’d try to add a caption as a second cross fade item so that I’d have the caption off screen until you rollover the image
I duplicated the functions and added new vars but I cant get the caption to display – I havent done any of the ‘move on rollover’ stuff yet
I’ve stripped out the pictures code to simplify but still doesnt work – this is what I have:
>>>>
stop();
import mx.transitions.Tween;
import mx.transitions.easing.*;
var currClipCap:MovieClip = cap1;
var arrPosition2:Number = -1;
var capArray:Array = new Array(“cap1″,”cap2″,”cap3″);
var showpicTime:Number = 5;
var tweenTime:Number = 3;
cap1.attachMovie(capArray[++arrPosition2],”cap”,cap1.getNextHighestDepth());
cap2.attachMovie(capArray[++arrPosition2],”cap”,cap2.getNextHighestDepth());
cap1._alpha = cap2._alpha = 0;
new Tween(cap1, “_alpha”, Regular.easeIn, 0, 100 , tweenTime, true);
var picInt:Number = setInterval(crossfade2,showpicTime*1000);
function crossfade2() {
if (currClipCap == cap1) {
var tween1c:Tween = new Tween(cap2, “_alpha”, Regular.easeIn, 0, 100 , tweenTime, true);
var tween2c:Tween = new Tween(cap1, “_alpha”, Regular.easeIn, 100, 0 , tweenTime, true);
if(++arrPosition2 >= capArray.length) {arrPosition2 = 0;}
tween2c.onMotionFinished = function() {
cap1.cap.removeMovieClip();
cap1.attachMovie(capArray[arrPosition2],”cap”,cap1.getNextHighestDepth());
}
currClipCap = cap2;
} else {
var tween1c = new Tween(cap1, “_alpha”, Regular.easeIn, 0, 100 , tweenTime, true);
var tween2c = new Tween(cap2, “_alpha”, Regular.easeIn, 100, 0 , tweenTime, true);
if(++arrPosition2 >= capArray.length) {arrPosition2 = 0;}
tween2c.onMotionFinished = function() {
cap2.pic.removeMovieClip();
cap2.attachMovie(capArray[arrPosition2],”cap”,cap1.getNextHighestDepth());
}
currClipCap = cap1;
};
};
anything obvious?
I dont understand why the image is on frame 2 ?
Hi Mark,
It would be a easier for you if you enclosed both the image and the caption into the same container. That way, you won’t have to worry about so many tweens. I’ll be posting an updated cross-fade tutorial using xml, external images, and captions within the next couple of days that will show you what I mean.
The images are on frame2 so I can preload them.
Hi there,
You said that your code can be modified to use external images and XML.
Can you explain how this works?
Kind regards,
Jason
Hi Jason,
I actually ended up posting on it here: http://www.rabidgadfly.com/?p=49
Glenn
Thanks Glen
Im new to actionscript also. And I was wondering if it would be easy to add a back and next button to skip through the pictures using the tween class like you have it.
Thank you,
Ivan
Hi Ivan,
Sure, you would just have to code the buttons to stop the interval, run the next/prev pic function manually, and then restart the interval.
Glenn
hello,
thank you very much for this tutorial,. it is unique on the web, i have been searching some time for this )))) will be recommending this blog.
R
Hi Glenn,
i’m a beginner in action script and it’s great code that i looked for since a while.
However, i used this slideshow as an external .swf that i load with “loadmovieclip”
I works well on the first show.
But when i go to another frame and come back to the one with the slide show, the cross-fading in spoiled. Just like it loads again.
i think i need to unload the movie but i don’t figure out how.
Hey Glen,
Ivan asked earlier if you could add back/next buttons and control the crossfading manually with buttons, would you mind explaining how one would go about achieving this.
It’s great code and would work really well for what I’m trying to achieve. I’m also looking to make this functional with a scroller of thumbs that would link to the images individually as well as the back/next buttons.
Any info you could offer would be a huge help.
Thanks,
-N
Hi Glen,
Thank you for a great tutorial and slideshow!
I have a quick question about loading the first image. Instead of waiting/watching the preloader for the first image to load, I need the image to load immediately, as if it were a part of a HTML page. I tried putting the first image on the last layer on the third frame–and while that does work, because it stays on the bottom, you can see the image on cross-fades of other images. For example, when images 4 and 5 are cross-fading, you can see image 1, very slightly. Does that make sense?
Could you give me a few pointers to avoid the first image from showing during transitions, or having to wait for it on load?
Thank you in advance for your help and time!
@Ivan and N: The back/next buttons will take bit of rewiring to make it work. Because the images are not all preloaded, there’s a good chance on the first pass-through that the next image will not be loaded when the next button is clicked. So, what needs to be done there is
* Add a preloader graphic to show if next image is not loaded when “next” is clicked
* Add a next function that will
1. Clear the interval
2. call the crossfade() function
3. reset the interval
The previous button requires re-loading the inactive clip with the clip previous to the one onscreen. Since the app was designed to go forward only, code will also have to be added to wrap the array index backward to the last image in the array if the user goes beyond the first image. As with the next button, this means that the user may be accessing an image that hasn’t been loaded yet and so a preloader image should be added for this as well.
If anyone has read this far, you can probably tell that there’s a couple of hours of work to be done. Unfortunately, at this time, I don’t have the time to dedicate to it. However, when I do get a little extra time, my plan is to post an AS3 version which will have that functionality.
@macgirl: To prevent the first image from fading in you can just comment out the initial tween.
Hi Glen,
Firstly, cheers for the great code!
I have a similar (but different) problem to macgirl.
I too am wanting the first image to load in straight away, but when I comment out the first tween (before the if statement) the first image stays on for twice as long as the others in the array.
Do you have any suggestions on how to change/fix this?
Thanks for your time and help
This has been the most useful tutorial I have come across.
Ben
Hi Ben,
The problem is that the alpha of the clips is initially set to 0.
Change this line:
pic1._alpha = pic2._alpha = 0;
to this:
pic2._alpha = 0;
That should do it for you.
Glenn
Hi Glen,
I tried doing that but still stuck with the same problem.
I tried with the initial tween both uncommented and commented, and still the same problem.
Any ideas on what might be causing it?
Thanks for your help……….and patience
Hi!
I am sorry if this is extremely comical, but I would love to create a slideshow such as the one you have above, however, I am not sure how to do so (even from reading this entire tutorial!).
Unfortunately, I have no experience in “ActionScript” or anything else such as XML, therefore it makes following the tutorial extremely difficult. I am using Dreamweaver 8, but I am not sure how to make a slideshow. I have tried downloading the script (but not too sure what to do after downloading it
) and have tried to closely follow other tutorials, but unfortunately, they are all geared towards people who have a pretty good understanding in the concepts/language involved in the process and so many things are assumed as “known.”
It might be too much to ask you for something for an extreme beginner, so I will not do that, however, can you direct me to some resource where I can get a better understanding of all of the things involved here, so that I can return and understand the tutorial?
Thanks and much appreciated,
John
@John: Thanks for the visit and for taking time to comment. You’re right, sometimes a certain level of knowledge is assumed. After reading your comment I took a look at my post and noticed that I never specify the technology that I’m posting about.
The cross-fading slideshow was created with Adobe Flash 8 using Actionscript 2. Flash is an authoring environment used to create many of the animations, games, videos, interactive sites and site components, and occasionally ultra annoying advertisements, that you see on the web today.
Flash compiles its applications into SWF files which are designed to be as small and efficient as possible given the content they contain. Dreamweaver on the other hand is a web development tool providing a robust environment to develop web sites using HTML or scripting languages such as ColdFusion, ASP, or PHP. You would use Dreamweaver to develop the page on which to place a SWF file.
In the future I’ll make an effort to, at the very least, mention the technology I’m posting about.
Thanks again for the comment,
Glenn
@Ben: Delete lines 12-16 and then paste the following lines where the deleted lines were:
pic1.attachMovie(picArray[++arrPosition],”pic”,pic1.getNextHighestDepth());
pic2.attachMovie(picArray[++arrPosition],”pic”,pic2.getNextHighestDepth());
pic2._alpha = 0;
var picInt:Number = setInterval(crossfade,showpicTime*1000);
Thanks for the speedy response. I have downloaded a trial version of the Flash CS3 from: http://www.adobe.com/products/flash/
It looks like they have templates on there, however they are nothing like the ones you’ve created.
I think I will take a day or two to collect myself and see exactly what my question is here!
Thanks again for the reply,
John
Hi Glen,
)
Thanks for the response (again!
I am still having the same problem. I have downloaded a fresh copy of your source, replaced lines 12-16 with the code you provided and still the exact same thing, twice as long on the first image, as the other images.
Is there any helping me? or would the code need some reqorking?
Cheers
Ben
This looks great. Can’t wait to try. Unfortunately, the link for the source file does not work. (it’s broken on the XML tutorial also). Any chance to get this fixed?
Thank you.
Dan
Hey Glenn,
The source file link is broken. I wanted to try out the code, but am not sure how to go about it. Can it be fixed? Thanks!
David
The link is fixed. Sorry about that!
It is possible to make this loop back to the beginning? Also, do you have a version of this in AS3?
Hi Shauna, The slideshow loops continuously. If I ever get some spare time I plan to create an AS3 version but that’s not going to be for a while.
Hi,
Thank you very much for the work and sample you have provided. I am learning Flash and would like to know if you can direct me to the steps I would need to be able to modify the ‘brown frame’ you have used. It appears to be a movie clip which acts as a mask. How did you create this? Can I modify this ‘ brown frame’ using flash or is it more complex than that?
Thanks,
Jeff
HI, thanks for a great and simple slideshow! I’m trying to add navigation buttons and it works except that it messes up the interval, in fact it seems like i might have 2 going on, because after I hit a button to interrupt the slideshow order, the timing for each slide becomes varied and strange. the code for one button is:
buttons.firstButton.onRelease = function() {
clearInterval(picInt);
arrPosition = 0;
if (currClip == pic1){
pic2.attachMovie(picArray[arrPosition],”pic”,pic1.getNextHighestDepth());
} else {
pic1.attachMovie(picArray[arrPosition],”pic”,pic1.getNextHighestDepth());
}
var picInt:Number = setInterval(crossfade,showpicTime*1000);
crossfade();
buttons.gotoAndPlay(‘pic0′);
};
So the problem seems to be where I restart the interval, can you offer any advice in how to fix it? Thanks!
ok i figured it ou, i changed this:
var picInt:Number = setInterval(crossfade,showpicTime*1000);
to this:
picInt = setInterval(crossfade,showpicTime*1000);
so has anyone gotten this to work with the addition of navigation buttons so the viewer can interrupt the slideshow and go to which ever slide they want? I have an almost working version, but for some reason when you click a button, it briefly shows the pic1 photo before fading to the correct photo. I can’t seem to get it right so maybe someone else has done it successfully? any good suggestions on where i can get online help?
Can you tell me what code I have to add to stop the infinite looping?
Something rather simple I hope. Great script though.
Thanks
Hi Darin,
Change lines 22 & 31 from:
if(++arrPosition >= picArray.length) {arrPosition = 0;}
to
if(++arrPosition == picArray.length) {clearInterval(picInt);}
That will make it stop at the last image
thank you so much!!!
Hi Glen, thank you very much for the script, it’s awsome!
I was wondering if there’s any way to attach the images in the middle of the main movie clip. What I’m trying to do is to make a slideshow with pictures of same height but different lenght, and I would like to have the middle point of them all in the same position. How can I do this?
Could you please help me?
Thanks a lot.
Hi camilla,
Replace the crossfade() function with the following one. I added four lines which are commented ‘center image to Stage’:
function crossfade() {
if (currClip == pic1) {
var tween1:Tween = new Tween(pic2, “_alpha”, Regular.easeIn, 0, 100 , tweenTime, true);
var tween2:Tween = new Tween(pic1, “_alpha”, Regular.easeIn, 100, 0 , tweenTime, true);
if(++arrPosition >= picArray.length) {arrPosition = 0;}
tween2.onMotionFinished = function() {
pic1.pic.removeMovieClip();
pic1.attachMovie(picArray[arrPosition],”pic”,pic1.getNextHighestDepth());
//center image to Stage
pic1._x = (Stage.width – pic1._width) / 2
pic1._y = (Stage.height – pic1._height) / 2
}
currClip = pic2;
} else {
var tween1 = new Tween(pic1, “_alpha”, Regular.easeIn, 0, 100 , tweenTime, true);
var tween2 = new Tween(pic2, “_alpha”, Regular.easeIn, 100, 0 , tweenTime, true);
if(++arrPosition >= picArray.length) {arrPosition = 0;}
tween2.onMotionFinished = function() {
pic2.pic.removeMovieClip();
pic2.attachMovie(picArray[arrPosition],”pic”,pic1.getNextHighestDepth());
//center image to Stage
pic2._y = (Stage.width – pic2._width) / 2
pic2._y = (Stage.height – pic2._height) / 2
}
currClip = pic1;
};
};
Hi Glen, thanks a lot, but it still doesn’t work…I think the problem is that to attach the images we use the upper left corner on the x and y position. What I would need is to use the middle point…so that the centre of the images always remains the same….is it possible?
Thanks!
Camilla
Hi camilla,
You’ll just need to modify the centering code to suit your needs.
If I understand what you’re saying it should probably look like this:
pic2._y = Stage.width / 2;
pic2._y = Stage.height / 2;
Or, you can just change the registration point of your image clips to 0,0
Thanks again! It works!
Hi Glen,
Great script you’ve done.
I ‘ve succeed to make it work on local machine but after uploading it cant find image
url.
Do I need to specify absolute path? like for example http://server.com/slides/?
Thanks in advance.
@rvk: You can use an absolute path but it doesn’t have to be absolute. The path just has to be relative to the page that the swf is running on.
Hei, thank you again for the tutorial
Is it possible to make the images random, so that everytime you enter the websites homepage, you begin with a different image.
Thank you!!
Hi Lillian,
I’m after the same thing. I’m trying to randomise the pics but with little success. Any ideas?
Thanks, Chris
Hi Glenn,
Great script. I see it has been some time since the last comment/question.
Well I have one:
The script cross fades pictures and that means that in the middle both pic are transparent and therefore you can see the BG color. The slide show would work much smoother if pic2 to would just fade in over the pic1 and then pic1 would fade out. That way you would only see the back picture till the top picture faded in. That way there would be no difference in the feel of the slide show depending on what BG color you would chose.
Do you perhaps have a solution for that?
Thanks big time.
Met
rabid,
great code. still working in 2.0 and on a basic level. I have one problem. I use this basic cross fade structure as a SWF and load it into another SWF. When I go to another swf and come back this code reloads and also seems to be playing from the previous load. How does one keep this from happening?
Thanks
Great code. I am trying to do this with the blinds transition. I’m new to actionscript and can’t seem to figure this out. Can you tell me how I could do this?
can you do the same thing using flex 3?