Flash Actionscript3 Tutorial : Animated Slideshow
August 20th, 2009
The Slideshow
the end product
You need Flash 10 to view the awesome. It’s a free and fast upgrade.
i’m using photos from Humanhand Blog
This tutorial is for Flash CS3/CS4 using Actionscript 3.0
Step 1. Gather all resources and code libraries.
We’ll need some images to slide around, and the TweenLite library.
- Setup your project directory “Slideshow”.
- In Flash CS3/4 - Create a new Flash File (Actionscript 3.0), save it to Slideshow/Slideshow.fla
- Get the latest TweenLite for Actionscript 3.0 from http://blog.greensock.com/tweenliteas3/
extract it and move the *gs* directory to Slideshow/gs/

Back in Flash CS3/4, make sure your library is open, drag some images from your computer into it.

Step 2. Setup library linking
Right click / Command+click on the first library image to open it’s library properties pane

- Click on ”Allow Smoothing”
- Click on ”Export for Actionscript”
- Rename the class to ”Photo_0” because class names should be Uppercased
- then hit *OK*

Do this to all images in your library. It’s tedious, but we’re learning right now, and we’ll learn how to improve management of your assets later.
Rename the classes appropriately (like “Photo_2”)
and always make sure to Allow Smoothing on each image, else your animations will look ugly.

Step 3. Setup your main class file, Slideshow.as
- Find your Document Properties Panel
- Under “Publish” find Class: and type in the text area, Slideshow
yours may look different, if you can’t find the properties panel, go to the main menu, choose “Window” and make sure “Properties” is checked, if not, click it to reveal the properties panel

Now go to File -> New and create a new Actionscript File.

save this file as… Slideshow/Slideshow.as

Step 4. Code !
setup our anonymous package
package{
}
import classes that we need
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.utils.Timer;
import flash.events.TimerEvent;
import gs.TweenLite;
import gs.easing.*;
create the Slideshow class that extends flash.display.Sprite, and Slideshow constructor
public class Slideshow extends Sprite
{
public function Slideshow()
{
}
}
this is what we have so far:
package{
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.utils.Timer;
import flash.events.TimerEvent;
import gs.TweenLite;
import gs.easing.*;
public class Slideshow extends Sprite
{
public function Slideshow()
{
}
}
}
inside the class
- define an array to hold all of our images
- define a variable to keep track of which slide we are on called curSlide
- define a timer
- and finally define some constants for our slideshow and animation
you can change the global animation speed and pause duration to whatever you want, they define time in seconds.
private var slides:Array;
private var curSlide:int;
private var timer:Timer;
private static const ANIMATION_SPEED:Number = 1.5;
private static const PAUSE_DURATION:Number = 3 + ANIMATION_SPEED;
inside the constructor
stick the photos in the array
since each Photo extends BitmapData, we need to pass a width and height as parameters otherwise the flash compiler will complain
lets use (0,0)
slides = [
new Photo_0(0,0),
new Photo_1(0,0),
new Photo_2(0,0),
new Photo_3(0,0),
new Photo_4(0,0),
new Photo_5(0,0),
new Photo_6(0,0),
new Photo_7(0,0),
new Photo_8(0,0)
];
we will be adding these photos onto stage so we can see the slides in this slideshow.
in order to do that, the photos must be a DisplayObject, so we’ll wrap each one in a new Bitmap object (which extends flash.display.DisplayObject).
the Bitmap objects are stored in the slides array
slides = [
new Bitmap(new Photo_0(0,0)),
new Bitmap(new Photo_1(0,0)),
new Bitmap(new Photo_2(0,0)),
new Bitmap(new Photo_3(0,0)),
new Bitmap(new Photo_4(0,0)),
new Bitmap(new Photo_5(0,0)),
new Bitmap(new Photo_6(0,0)),
new Bitmap(new Photo_7(0,0)),
new Bitmap(new Photo_8(0,0))
];
now lets add them to stage, and hide them to the left edge of the stage, initially out of sight
and vertically center them on stage.
var i:int;
var l:int = slides.length;
for ( i; i < l; i++ ) {
addChild(slides[i]);
slides[i].x = stage.stageWidth;
slides[i].y = stage.stageHeight/2 - slides[i].height/2;
}
after that, create the timer object
timer = new Timer(PAUSE_DURATION);
so now we have
package{
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.utils.Timer;
import flash.events.TimerEvent;
import gs.TweenLite;
import gs.easing.*;
public class Slideshow extends Sprite
{
private var slides:Array;
private var curSlide:int;
private var timer:Timer;
private static const ANIMATION_SPEED:Number = 1.5;
private static const PAUSE_DURATION:Number = 3 + ANIMATION_SPEED;
public function Slideshow()
{
slides = [
new Bitmap(new Photo_0(0,0)),
new Bitmap(new Photo_1(0,0)),
new Bitmap(new Photo_2(0,0)),
new Bitmap(new Photo_3(0,0)),
new Bitmap(new Photo_4(0,0)),
new Bitmap(new Photo_5(0,0)),
new Bitmap(new Photo_6(0,0)),
new Bitmap(new Photo_7(0,0)),
new Bitmap(new Photo_8(0,0))
];
var i:int;
var l:int = slides.length;
for ( i; i < l; i++ ){
addChild(slides[i]);
slides[i].x = stage.stageWidth;
slides[i].y = stage.stageHeight/2 - slides[i].height/2;
}
timer = new Timer(PAUSE_DURATION);
}
}
}
create event listeners, and start the timer.
after the timer object has been created, add an event listener.
timer = new Timer(PAUSE_DURATION);
timer.addEventListener(TimerEvent.TIMER, slide);
and call
create a quick helper method to find the horizontal center for the sliding image to land, we’ll use this a couple places
private function getCenter(bitmap:Bitmap):Number // find the x endpoint, the horizontal center for the next slide
{
return stage.stageWidth/2 - bitmap.width/2;
}
create an initial_slide method to call once to animate in the first slide
private function initial_slide():void // this happens first, and only once
{
}
in this method, we animate the first slide into view from the right side. then increment the curSlide counter; our TweenLite.to parameters are
- what we will animate, the first slide, same as saying slides[0]
- our global animation speed value
- an object that defines 2 options for this animation
x: here we use the getCenter() method we just wrote, and pass in the first image so that it returns an accurate x target getCenter(slides[curSlide])
ease: i like how Cubic.easeInOut looks, you can change this to your preference
private function initial_slide():void // this happens first, and only once
{
// animate the next slide in
TweenLite.to(slides[curSlide], ANIMATION_SPEED, {x:getCenter(slides[curSlide]), ease:Cubic.easeInOut});
// increment curSlide
curSlide++;
}
create the slide method that will run every four and a half seconds
public function slide(event:TimerEvent):void{
}
we’ll handle slideshow looping in this method
and use TweenLite’s to() static method to move slides in and out.
inside the slide listener
define a couple variables that will be animated
var newSlide:Bitmap = slides[curSlide];
var oldSlide:Bitmap;
set where the new slide comes from, off stage right
newSlide.x = stage.stageWidth;
check if we looped and set the oldSlide variable
if(curSlide == 0) // slideshow looped, animate out last slide in slides
{
oldSlide = slides[slides.length-1];
}
else // animate old slide out
{
oldSlide = slides[curSlide-1];
}
do our animations
nextSlide starts off stage to the right, animates left to the center
oldSlide animates left from the center to the far left off stage
// animate the next slide in
TweenLite.to(newSlide, ANIMATION_SPEED, {x:getCenter(newSlide), ease:Cubic.easeInOut});
// animate the old slide out
TweenLite.to(oldSlide, ANIMATION_SPEED, {x:-oldSlide.width, ease:Cubic.easeInOut});
finally increment the curSlide counter and loop if necessary
// increment curSlide or loop
if(curSlide++ == slides.length-1)
{
curSlide = 0;
}
the entire slide event listener method looks like this
private function slide(event:TimerEvent):void
{
var newSlide:Bitmap = slides[curSlide];
var oldSlide:Bitmap;
// set where this slide comes from , off stage right
newSlide.x = stage.stageWidth;
// check if we looped. set oldSlide
if(curSlide == 0) // slideshow looped, animate out last slide in slides
{
oldSlide = slides[slides.length-1];
}
else // animate old slide out
{
oldSlide = slides[curSlide-1];
}
// animate the next slide in
TweenLite.to(newSlide, ANIMATION_SPEED, {x:getCenter(newSlide), ease:Cubic.easeInOut});
// animate the old slide out
TweenLite.to(oldSlide, ANIMATION_SPEED, {x:-oldSlide.width, ease:Cubic.easeInOut});
// increment curSlide or loop
if(curSlide++ == slides.length-1)
{
curSlide = 0;
}
}
Here is the entire Slideshow.as file, all the code you need to complete this project
package{
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.utils.Timer;
import flash.events.TimerEvent;
import gs.TweenLite;
import gs.easing.*;
public class Slideshow extends Sprite
{
private var slides:Array;
private var curSlide:int;
private var timer:Timer;
private static const ANIMATION_SPEED:Number = 1.5;
private static const PAUSE_DURATION:Number = 3 + ANIMATION_SPEED;
public function Slideshow()
{
slides = [
new Bitmap(new Photo_0(0,0)),
new Bitmap(new Photo_1(0,0)),
new Bitmap(new Photo_2(0,0)),
new Bitmap(new Photo_3(0,0)),
new Bitmap(new Photo_4(0,0)),
new Bitmap(new Photo_5(0,0)),
new Bitmap(new Photo_6(0,0)),
new Bitmap(new Photo_7(0,0)),
new Bitmap(new Photo_8(0,0))
];
var i:int;
var l:int = slides.length;
for ( i; i < l; i++ ){
addChild(slides[i]);
slides[i].x = stage.stageWidth;
slides[i].y = stage.stageHeight/2 - slides[i].height/2;
}
timer = new Timer(PAUSE_DURATION*1000);
timer.addEventListener(TimerEvent.TIMER, slide);
timer.start();
initial_slide();
}
private function getCenter(bitmap:Bitmap):Number // find the x endpoint, the horizontal center for the next slide
{
return stage.stageWidth/2 - bitmap.width/2;
}
private function initial_slide():void // this happens first, and only once
{
// animate the next slide in
TweenLite.to(slides[curSlide], ANIMATION_SPEED, {x:getCenter(slides[curSlide]), ease:Cubic.easeInOut});
// increment curSlide
curSlide++;
}
private function slide(event:TimerEvent):void
{
var newSlide:Bitmap = slides[curSlide];
var oldSlide:Bitmap;
// set where this slide comes from , off stage right
newSlide.x = stage.stageWidth;
// check if we looped. set oldSlide
if(curSlide == 0) // slideshow looped, animate out last slide in slides
{
oldSlide = slides[slides.length-1];
}
else // animate old slide out
{
oldSlide = slides[curSlide-1];
}
// animate the next slide in
TweenLite.to(newSlide, ANIMATION_SPEED, {x:getCenter(newSlide), ease:Cubic.easeInOut});
// animate the old slide out
TweenLite.to(oldSlide, ANIMATION_SPEED, {x:-oldSlide.width, ease:Cubic.easeInOut});
// increment curSlide or loop
if(curSlide++ == slides.length-1)
{
curSlide = 0;
}
}
}
}
Download the full source everything you need that we just did in these 4 steps, including the latest greensock tweening engine
it is better to download the latest *gs* library from the greensock site because there are frequent updates.
that’s it for this tutorial. what you now have is a working slideshow that loops.
once you have a good understanding of this simple process, it should be pretty simple to make minor modifications.
i suggest you try this tutorial first, download the source, and play with it. change the constants that we defined at the top to make the delay and animation speeds more to your liking. and also explore different easing methods like Quint.easeIn, Quart.easeOut, Sine.easeInOut. etc.
then if you want to add more features like user interaction / control, or better and easier management and loading of images for massive photo sets, or preloading, then post your requests in the comments below.
now here’s a picture of my cat.






thank you. I've been looking for something like this for a few days! I appreciate that you went from a very beginners level.
hey this was a great help looking to, is it possible to put a black fade on one side?
@warren yes, create a black fade graphic (vector is fine) from black to black, and on one side, make the alpha 0. i'd make it about 15% - 20% the width of the entire app, and 100% the height of the entire app. then fit it against one side on the top most layer. copy it and paste it on the same layer. with the copy, rotate it 180degrees, and place it hugging the opposite side. that's it.
Brilliant slideshow! Thanks! But i'd like to put some snow falling down, so i've imported a flake that I transform in movie clip, the I've created a class called particle.as with following code: package { import flash.display.MovieClip; public class Particle extends MovieClip { //We need different speeds for different particles. //These variables can be accessed from the main movie, because they are public. public var speedX:Number; public var speedY:Number; function Particle ():void { } } } then in the action frame I put this var numberOfParticles:Number = 30; var particlesArray:Array = new Array(); //This loop creates 40 particles that are positioned randomly on the stage. for (var i=0; i < numberOfParticles; i++) { var particle:Particle = new Particle(); //Give random x and y speed to the particle. Math.random returns a random number n, where 0 <= n < 1. particle.speedX = Math.random(); particle.speedY = 3 + Math.random(); //Set the starting position particle.y = Math.random() * stage.stageHeight; particle.x = Math.random() * stage.stageWidth; //Set random alpha between 0.2 and 1 particle.alpha = 0.2 + Math.random() * 0.8; //Add the particle to the stage and push it to array for later use. addChild (particle); particlesArray.push (particle); //Set a random scale to each particle. Scale is between 0.5 and 1. particle.scaleX = 0.5 + Math.random() * 0.5; particle.scaleY = particle.scaleX; } addEventListener (Event.ENTER_FRAME, enterFrameHandler); function enterFrameHandler (e:Event):void { //Loop through the particles for (var i = 0; i < particlesArray.length; i++) { var particle = particlesArray[i]; particle.y += particle.speedY; particle.x += particle.speedX; //If the particle is below the bottom, position it back to the top if (particle.y > stage.stageHeight) { particle.y = 0; particle.x = Math.random() * stage.stageWidth; } } } But I get the 2 following errors: First error 1046: Type was not found or was not a compile-time constant: Event function enterFrameHandler (e:Event):void { Second error Warning: 3590: void used where a Boolean value was expected. The expression will be type coerced to Boolean. _tweenVisible = !Boolean("visible" in $lookup); Both animations work perfectly fine on their own, but together it's bit of a mess... Can you help me? I dont know what i do wrong. Thank you
Sorry about the layout of comment above, didn't know would display like that. If you can make sense of it id love some help. Thanks again
@Alejandra Otero on the first line on the first frame on the main timeline (before var numberOfParticles:Number = 30;) import the Event class.
import flash.events.Event;
Hi there, thank you for this great tutorial! I've got it working for some of my slideshows. One question I do have is how would you add some control to the slideshow? for example and next and previous button. It would be excellent if you could help me with this! Thank you!