Create an Image Gallery using Flash ActionScript 3.0 and XML

Today you will learn basics of how to create an Image Gallery using Flash Actionscript 3.0 and XML, how to dispatch events, how to load images trough XML file and much more. In next tutorial I will extend this tutorial by adding more features like next – prev buttons, links or text from XML. Let’s begin


Settings all files

Create a new ActionScript File and save it as “” ( – this will be the main class for our flash file; the package classes extends the Sprite class (since we don’t have a timeline animation). Later we will build other classes for thumbs or big image. Finally create a new AS3 Flash file (590×500px) and open it, so that we have both files opened.


Create a simple design for our gallery – see attached. Also create a new symbol, convert this to MovieClip and give him an instance name called “container” that will represent the container where thumbs will be.


Create a folder “images” and inside of this 2 subfolders “large” – all large images will be here –  and “thumb” with all thumbs. I put 15 images and thumbs in these folder, images with a 500px maxim width and all thumbs 100 x 75px. Then create a new XML file called “gallery.xml” and add some nodes that will contain these thumbs and images:

<?xml version="1.0" encoding="UTF-8"?>
<image src="images/large/1.jpg" thumb="images/thumb/1.jpg" />
<image src="images/large/2.jpg" thumb="images/thumb/2.jpg" />
<image src="images/large/15.jpg" thumb="images/thumb/15.jpg" />

The XML file contain just thumbs and images, in next tutorial wi’ll add more features like content or links.

Let’s add some constants and variables to load XML content:

private var xml:XML;
private var urlLoader:URLLoader;
private static const XML_PATH:String = "gallery.xml"

In constructor function wi’ll create the urlLoader:

urlLoader = new URLLoader();
urlLoader.load(new URLRequest(XML_PATH));
urlLoader.addEventListener(Event.COMPLETE, loadXML);

After XML is loaded we use his content in loadXML function:

private function loadXML(e:Event):void
//display hole content from XML
xml = new XML(;

Trace xml variable to show the content. Let’s move on second part now, by creating some thumbs.


In main movie draw a rectangle with width 100px and height 75px; then convert this to a MovieClip with name “Thumb” and class “” (we didn’t create this class yet). Inside of this put a new blank MovieClip with instance name “container” on a new layer. Over this layer, create a new layer an put a mask with same size. The thumbs are ready, we just must create the class for this – “”;

Create a new ActionScript File and save it as “” ( – this will be the class for all thumbs; the package classes extends the MovieClip class. Here we will add the thumb image inside and will convert this to button:

import flash.display.Loader;
import flash.display.MovieClip;

public class ThumbClass extends MovieClip
public var image:String;

//we receive the values from
public function ThumbClass(thumb:String, image:String) {
//set this to button mode
this.buttonMode = true;
this.mouseChildren = false;

//store the image path
this.image = image;

//create a loader for image
var loader:Loader = new Loader();
loader.load(new URLRequest(thumb));



Next, let’s add some thumbs on stage.

Adding thumbs

In main movie drag and drop the blank container from library to the stage, give him instance name “container” and center this in middle of stage – this will be the container for thumbs. In add some variables:

private var thumbsLength:Number;
private var thumb:ThumbClass;
private var thumbWidth:Number = 100;
private var thumbHeight:Number = 75;

Then in function loadXML where we load the XML file we will call the to load each thumb image:

[as3]private function loadXML(e:Event):void
//display hole content from XML
xml = new XML(;
thumbsLength = xml.image.length();
//attach thumbs on stage
for (var i:int = 0; i < thumbsLength; i++) {
thumb = new ThumbClass(xml.image[i].@thumb, xml.image[i].@src);

//show thumbs with a nice effect
setTimeout(moveThumbs, 100*(i+1), thumb, Math.round(100 * i), i);

thumb.x = 230;//play with this value
thumb.y = 0;
//create a listener to position the thumb’s container in the middle of stage
this.addEventListener(Event.ENTER_FRAME, positionThumbs);

We add the thumbs as childrens of “container” MovieClip from stage, then we set all thumbs coordonates from X and Y to 230 – 0 for a nice effect; we create a timer to position each thumb to his right coordonates, position and finally we create a listener that will position the container in middle of stage. If you have a chance to look at the preview, you’ll see that all thumbs are very nice arranged by columns. To do this, create a new variable to control how many columns you want to be:

private var numOfColumns:Number = 5;
private var spacer:Number = 1;

Then the “moveThumbs” function that will arrange the thumbs by columns:

private function moveThumbs(pic:MovieClip, xDest:Number, i:Number) {
var c:Number = Math.floor((i % numOfColumns) * (thumbWidth + spacer));
var d:Number = Math.floor(i / numOfColumns) * (thumbHeight + spacer);
Tweener.addTween(pic, { x:c, y:d, time:1, transition:"easeOutSine" } );

//if last thumb was added then we remove the listener that position the thumb’s container
if (i == thumbsLength) {
this.removeEventListener(Event.ENTER_FRAME, positionThumbs);

And the function “positionThumbs” that will position the thumb’s container:

private function positionThumbs(e:Event):void
container.x = Math.floor((stage.stageWidth * 0.5) – (container.width * 0.5));
container.y = Math.floor((stage.stageHeight * 0.5) – (container.height * 0.5));

Phew, a lot of code here. Test your movie and see all thumbs arranged in the middle of stage. Okay, now we need to add a way that when we click on thumbs to open the big image. To do this, we need to create a new movieclip that will store the big image, a new class for this and a listener on thumbs that will trigger that function to open these large images.

Big image

In main movie, as you created the thumb’s MovieClip, repeat that steps only that this will have a new size – 500x500px. Convert the background to a new MovieClip with instance name “bg”, with name “BigImage” and class path to “” then set the coordonates X and Y for container to 6-6 and finally. When this image will be opened, we need a way to close her and to return back to thumbs – you are right, we must create a button that will do this. Draw a rectangle, convert this to MovieClip and give him an instance name “close_btn”. This button will be inside of the “bigImage” symbol, on his bottom side. The big image container should look like in the above image.

import caurina.transitions.Tweener;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.MovieClip;

public class ImageClass extends MovieClip
private var loader:Loader;

public function ImageClass() {
//make the close_btn button
close_btn.buttonMode = true;
close_btn.mouseChildren = false;
//on press this will hide the image
close_btn.addEventListener(MouseEvent.CLICK, hideImage);

public function loadImage(img:String)
{ //this function will be called from, when thumbs are clicked.
var img:String = img;
loader = new Loader();
loader.load(new URLRequest(img));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, showImage);

private function showImage(e:Event):void
//add image loaded as child to container
if (container.numChildren >= 1) {

//resize the background of image
bg.height = + 12;
bg.width = + 12;
//position the button to the bottom side of image
close_btn.y = bg.y + bg.height + 10;
close_btn.x = bg.x + bg.width – close_btn.width;

Tweener.addTween(this, { time:1, transition:"easeInOutQuint", x:Math.floor((stage.stageWidth * 0.5) – (this.width * 0.5)) } );
Tweener.addTween(this, { time:1, transition:"easeInOutQuint", y:Math.floor((stage.stageHeight * 0.5) – (this.height * 0.5)) } );

//when button is pressed will trigger a listener from that will hide this.
private function hideImage(e:MouseEvent):void
dispatchEvent(new Event(MainClass.HIDE_IMAGE, true));


Now that we create the class for images, in we will put a listener to activate the function that will make the large image visible:

this.addEventListener(MouseEvent.CLICK, showImage);

And the “showImage” function :

private function showImage(e:MouseEvent):void
//trigger the listener from to show the big image
dispatchEvent(new Event(MainClass.SHOW_IMAGE, true));

Ok, we are almost done. Let’s create that listeners in that will show/hide the images. Focus on for next minutes.

Hocus pocus…

Create 2 contants in

public static const SHOW_IMAGE:String = "show_big_image";
public static const HIDE_IMAGE:String = "hide_big_image";

Then in constructor function add 2 listeners:

this.addEventListener(SHOW_IMAGE, loadImage);
this.addEventListener(HIDE_IMAGE, hideImage);

…show images:

private function loadImage(e:Event):void
//we make invisible the container with thumbs and then we show the image
Tweener.addTween(container, { alpha:0, transition:"easeOutSine", time:0.5, onComplete: function doHide() {
container.visible = false;

private function showImage(value:String) {
//position the big image container
bigImage.x = Math.floor((stage.stageWidth * 0.5) – (bigImage.width * 0.5));
bigImage.y = 0 – bigImage.height;

bigImage.visible = true;


…hide images:

private function hideImage(e:Event):void {
//hide the big image container and show thumbs container visible back
Tweener.addTween(bigImage, { time:1, transition:"easeInOutQuint", y:Math.floor((stage.stageHeight + 30)), onComplete: function doShow() {
container.visible = true;
Tweener.addTween(container, { alpha:1, transition:"easeOutSine", time:0.5}) }} );


Save everything, and let’s recapitulate. We:

  • put images and thumbs in folders
  • add nodes with these in XML file,
  • create a container in middle of stage to hold the thumbs,
  • create the thumb’s container and the images
  • add function to show/hide images.

Hope you learn something from this and I don’t forget anything (you have sources attached too). Thank you for your time, stay tuned until next tutorial.

Twitter Digg Delicious Stumbleupon Technorati Facebook

30 Responses to “Create an Image Gallery using Flash ActionScript 3.0 and XML”

  1. Hello, can you post the original fla file?

  2. Hi, aren’t source files attached?

  3. Hello,
    Thanks. It worked fine with different images.

  4. Hello, can you post the original fla file?

  5. All sources can be found in .zip archive

  6. works fine, thanks mate:)

  7. @toms: you’re welcome

  8. Hi,
    i just tried to rebuild your GREAT tutorial. But now im stucked on a problem..

    Im at the first part, create thumbs:
    i created the with folders and the structur which fits to my project(as3 of course).
    I Also copied the movieclips out of your project into mine but if i want to publish, there is an error in the ThumbClass.
    “1119: Access of possibly undefined property container through a reference with static type”
    I copied the MCs, the declaration should be the same. Any ideas where the problem could be?

    nice work!!

  9. You forgot to copy the movieclip container I think into thumbs movieclip. Check if this exist there.


  10. good points and the details are more specific than somewhere else, thanks.

    – Norman

  11. You’re welcome Norman. Thank you for your comment

  12. Hi Robert, Great tutorial 🙂

    I’m having a strange problem, I went over everything you said several times, and kept getting errors stating that Flash didn’t understand things like ‘Tweeners’ etc.

    I have done some troubleshooting and reached the stage where I have noticed this:

    If I download the example files, they work absolutely fine, if I open up the example .fla – do absolutely NOTHING to it, but publish it, I get a run of errors and the .swf does nothing.

    Odd huh? Perhaps this needs some kind of plugin I’m missing? I’m using CS5 proper version etc.

  13. Hi Robert thanks for the help!

    I’ve got it all working now, except for one thing, when I click on a thumb the big-image loads up, when I click close it closes, but nothing else happens, i.e. no thumbs appear.

    I’ve checked the code and it’s identical to the tutorial, any ideas?

    I feel bad for all these problems because I actually think it’s a great tutorial

  14. Hmm…download again, something is strange.

  15. Cool galery, simple looking but smooth working. THX!

  16. Wow! Great tutorial Robert.
    I’m a newbie using flash, this is what I’ve been waiting for. Thank you Robert 🙂

  17. You’re welcome 🙂

  18. Hello Dear,

    Is it possible to post description of an image to show together with the large image either at the bottom or at the left-right side.


  19. Hi there, yes it’s possible.

  20. Hi Robert,

    thats a really helpful tutorial,
    unfortunately I get the same problem as Jamie reported on the 16th Jan, the container never reappears
    I noticed that when I compile your source code using CS4 the SWF is slightly smaller. Any thoughts?

  21. Update:
    I’m not sure what the doShow() function is that is called by onComplete: when the big image fades out.
    I replaced this with a new function that handles the container reappearing.

    unfortunately the container tweens in simultaneously with the image tweening out. Got to investigate why.

    Found out CS4 doesn’t like onComplete:function x() but prefers onComplete:x()

  22. There are any errors?

  23. can you kindly post the zip file. I can’t seem to find it. Thank you

  24. hey,
    how to add video into .xml file of adobe flash.

  25. Hello..

    i want to ask how can i insert some text for every picture in action script?or in xml file.How to do?

  26. 1. You need to modify the XML file;

    2. In “bigImage” MovieClip, create a new TextField, with name “txt”;

    3. In in
    public function loadImage(img:String), add a new parameter
    public function loadImage(img:String, t:String) {
    txt.text = t;

    4. In, add a new public variable: public var title:String;
    then modify the line public function ThumbClass(thumb:String, image:String) { in
    public function ThumbClass(thumb:String, image:String, t:String) {

    and add near this.image = image;
    title = t;

    5. In, on line thumb = new ThumbClass(xml.image[i].@thumb, xml.image[i].@src); add new parameter:
    thumb = new ThumbClass(xml.image[i].@thumb, xml.image[i].@src, xml.image[i].@title);

    That’s it. 5 steps 😉

  27. I’ve changed numOfColumns to 4. nothing happens, why?

  28. This should fix the problem of the container not reappearing in CS4.

    private function hideImage(e:Event):void {
    //hide the big image container and show thumbs container visible back
    Tweener.addTween(bigImage, { time:1, transition:”easeInOutQuint”, y:Math.floor((stage.stageHeight + 30)), onComplete: doShow})
    function doShow(){
    container.visible = true;
    Tweener.addTween(container, { alpha:1, transition:”easeOutSine”, time:0.5}) };