1. Code
  2. Game Development

Create a Nifty Polaroid Photo Viewer With Flash and Photoshop

Scroll to top
21 min read

Ever wanted to make a manageable and beautiful photo viewer app? In this tutorial, I'll show you how to design and code a nifty Polaroid photo viewer, focusing on the design and tweening.


Final Result Preview

Let's take a look at the final result we will be working towards:


Section 1: Organization

In this section we'll discuss our folder structure, also we'll acquire our stock imagery.


Step 1: Creating Our Folders

First let's create a folder in which our project will be located. We'll name it "Polaroid Viewer".

Afterwards just copy this folder structure:

The folder structure which we'll use in our tutorialThe folder structure which we'll use in our tutorialThe folder structure which we'll use in our tutorial

Step 2: Understanding Our Folder Structure

When creating very visual projects, it's very important to first really know what you want to design. Inside the Design folder I always have my visual concepts and also in the Design folder I edit the images I'll need for a project.

After I've finished working on my concept I proceed to making a working product, I do this inside the Project folder. Inside the Project folder is the very known bin, lib and src folder structure:

  • bin: Everything which is being exported and is needed to launch the final product, including data assets.
  • lib: Our Flash documents.
  • src: All our code, our own classes, as well as other frameworks.

Step 3: Acquire Stock Imagery

Now that we've created our folders, let's proceed to the images. In this tutorial we'll need at least two images:

You'll need a Stock Xchange account to download these images, if you don't yet have one, you should sign up! It's a website filled with free stock imagery!

Click the links to download the images, afterwards save or move them to our Design folder.

Also we'll need images to place inside the polaroid frames. Here's a list of images which I've used, but feel free to use your own images.


Section 2: Design

In this section we're going to edit our stock images. After doing so, we'll import these images inside our Flash document, which we'll create later.

We're going to edit our images with Photoshop, I'll be using CS5, however you can always use older versions. If you're not so confident in your Photoshop skills, you can download the images which I've already edited. Just download the source files from this tutorial, and you can find the images inside the Edited Images folder.


Step 4: Preparing Our Wooden Floor

In the final example all our polaroid pictures lie on a wooden floor. We're now going to create this background.

Create a New Document in Photoshop with the dimensions 600x600px.

Add the wooden texture image to the document. Scale it so you're pleased with the final result.

Our background has been scaled down.Our background has been scaled down.Our background has been scaled down.

I've also added a Gradient Map, setting the colors to black and white.

Add a gradient map to our background

Lastly I've changed the gradient map's transition to 80%. To do so, click on one of the lower arrows, and afterwards click on the dot in the middle of the gradient. Drag this dot to 80%.

Editing the Gradient MapEditing the Gradient MapEditing the Gradient Map

And voila! We've created our wooden background! Save it as a Photoshop Document (.psd) in our Design folder, and name it Background.psd.

Our background which we'll use in our projectOur background which we'll use in our projectOur background which we'll use in our project

Step 5: Preparing the Polaroid Frame

We want to place our own images inside the polaroid frame. We must do two things, we'll first need to remove the polaroid frame from its (white) background, and also we'll need to cut the "photo" part away.

To do this, we'll first create a new 600x600px Photoshop document. Paste the polaroid picture inside the document, scale it down till it fits.

The polaroid frame image has been opened in PhotoshopThe polaroid frame image has been opened in PhotoshopThe polaroid frame image has been opened in Photoshop

Why do we scale the image down? Because we're creating a 600x600px Flash document, and it's unnecessary to have and load a huge image if we won't even scale it larger than 600 px.


Step 6: Isolating the Polaroid Frame

Select the pen tool (P) and click around the polaroid frame's outer edge. When working with the Pen tool, be sure to set the Pen tool in its Paths mode.

Set the Pen Tool to paths mode.Set the Pen Tool to paths mode.Set the Pen Tool to paths mode.

Press Control + Enter (Windows) or Cmd + Enter (Mac) to select your current path.

Select with the pen tool the outer area of the polaroid pictureSelect with the pen tool the outer area of the polaroid pictureSelect with the pen tool the outer area of the polaroid picture

With your path selected click on the "Add Layer Mask" button. We've now isolated the polaroid from its background!

Add a layer mask to the polaroid

Step 7: Cutting the Photo Part Away From the Polaroid Frame

This step is very similar to Design Step 4. Again select the pen tool, and click around the inner area of the polaroid's frame. Afterwards select the current path with Ctrl + Enter/Cmd + Enter.

Select with the pen tool the inner area of the polaroid pictureSelect with the pen tool the inner area of the polaroid pictureSelect with the pen tool the inner area of the polaroid picture

Now Alt-click on the mask in the Layers Panel. You're now editing the mask directly.

Editing the Layer Mask of our polaroid pictureEditing the Layer Mask of our polaroid pictureEditing the Layer Mask of our polaroid picture

Select the Marquee tool (M). Right-click your selection and select Fill > Black.

Alt-click again on the mask icon, to return back to our "normal" Layer view. We've successfully edited our mask and now our polaroid is ready for use. Let's save the image as a Photoshop document (.psd), I've named my document Polaroid.psd.

The polaroid picture has been editedThe polaroid picture has been editedThe polaroid picture has been edited

Section 3: Flash

In this section we're (finally) going to use Flash. We're going to set our Flash document to use the folder bin for exporting, and the folder src for our code. Time to open Flash!


Step 8: Creating Our Flash Document

Create a new Flash Document (ActionScript 3.0) with Flash Professional. Set the dimensions to 600x600px and the framerate to 24 fps. Set the Document Class to Main. Also change the background to black (#000000). Save it as "Polaroid.fla", inside the lib folder.


Step 9: Changing Our Publish Settings

Afterwards go to File > Publish Settings, change Polaroid.swf to ../bin/Polaroid.swf, you can uncheck the HTML option. Now everything we export (only a SWF) will be in the bin folder. Do not close the Publish Settings window.

Change our publish settings of our Flash DocumentChange our publish settings of our Flash DocumentChange our publish settings of our Flash Document

Step 10: Changing Our Source Path

Click the Flash tab. Afterwards click on the Settings button next to Script: ActionScript 3.0.

Now we can change the source path. You'll see a folder icon with the text ".", change that to "../src".

Flash will use the src folder for all its classes.

Change the ActionScript settings of our Flash DocumentChange the ActionScript settings of our Flash DocumentChange the ActionScript settings of our Flash Document

Step 11: Importing Our Graphics to Flash

It's now time to start importing our graphics in to our Flash Document. If you had any trouble with the images, you can always download the source files from this tutorial at the top. There'll be a folder named "Edited Images", inside there'll be the edited images which you can use.

Create a new Flash Document (ActionScript 3.0) with Flash Professional. Set the dimensions to 600x600px and the framerate to 24 fps. Also change the background to black (#000000).

We've saved our images as .psd files because they work so greatly with other Adobe products, like Flash. After creating our document, select File > Import to Library and select Polaroid.psd and Background.psd. After doing so our library looks like this:

We've imported our assets in to our Flash Document's library

Step 12: Creating Our Background Sprite

Drag Background.psd from the Library to our stage, afterwards convert it to a movie clip. Name it Background, and check the box "Export for ActionScript". Set the base class to flash.display.Sprite. After doing so, you can remove the background again from the stage.

Convert the background to a symbolConvert the background to a symbolConvert the background to a symbol

Why did I set Background's base class to Sprite instead of the default base class MovieClip? Because the only difference between a MovieClip and a Sprite is that MovieClips can contain multiple frames, and Sprites cannot. We don't need multiple frames for our Background, so let's set the base class of Background to Sprite.


Step 13: Creating and Our Polaroid Movie Clip

Just like we did with our background, drag polaroid.psd from the Libary to our stage. Convert it to a movie clip and name it Polaroid, again check the Export for ActionScript box. Set the base class to flash.display.Sprite.


Step 14: Creating Our Polaroid's Layers

Our Polaroid is still on the stage, double-click it, so we can edit it. So far Polaroid only has one layer, rename this layer to Polaroid Frame.

Create two new Layers, naming them Mask and Image Holder.

The layers inside the symbol PolaroidThe layers inside the symbol PolaroidThe layers inside the symbol Polaroid

Step 15: Creating the Image Holder

Our Polaroid will contain an image, this image must be displayed somewhere, this will be done in a movie clip named "Image Holder".

Draw a rectangle of approximately the size as the "photo area" of the polaroid, do this in the Image Holder layer. It doesn't matter which color the rectangle will be. After creating this rectangle, convert it to a movie clip, name it "Image Holder".

Give Image Holder an instance name of imageHolder.


Step 16: Adding a Mask to the Image Holder

Image Holder will contain the images which we will load. Sometimes these images are too big -- take for example a 400x100px image; this image is much too wide, and its edges would extend out of the polaroid. We want to prevent this. Therefore we must hide all imagery which extends outside of the Image Holder's area.

To do this, we must create a mask. A mask tells a layer which part should be displayed, and which part should be hidden.

If we want every image to be displayed inside Image Holder, without any edges protruding, we should set the mask's size to the same dimensions as Image Holder itself.

Copy Image Holder, and paste it in place with Ctrl + Shift + V (Windows) / Cmd + Shift + V (Mac) on the layer Mask. Afterwards break the copied Image Holder with Ctrl + B (Windows) / Cmd + B (Mac).

Lastly we must tell our Mask layer that it is a Mask. Right-click on the layer Mask and select the option Mask.


Section 4: Code

It's time to start coding, if you had any trouble with the Flash Section, you can just copy the folder Flash Section Completed. Inside that folder is everything you need to start following these steps.


Step 17: Creating an XML File

To load our images, we'll use an XML file. The XML file will tell our product where the images are located.

Open a text editor and type the following:

1
2
<images>
3
	<image>
4
		<file>IMAGE_1.jpg</file>
5
	</image>
6
	<image>
7
		<file>IMAGE_2.jpg</file>
8
	</image>
9
	<image>
10
		<file>IMAGE_3.jpg</file>
11
	</image>
12
</images>

You should change IMAGE_1.jpg, IMAGE_2.jpg, etc. to real filenames, which refer to images located inside the bin folder. If you don't have any images, download the images that I mentioned in Step 3.

Save this XML file as images.xml, inside the bin folder.


Step 18: Which Classes Do We Need?

Before we start coding, it's important to know which tasks our project must do:

  • We must load our images from an XML file.
  • We must display these images inside a Polaroid.
  • We must prevent other Polaroids from zooming in when we are already zoomed in on a Polaroid.

To code all these functionalities in one class would make that class extremely complex, it's better to seperate each task to a different class.

  • ImageLoader: Will load the XML file and pass the images to PolaroidHolder.
  • PolaroidHolder: When it receives the images, it will create a Polaroid for every image, so it also makes sure that only one Polaroid is zoomed in at a time.
  • Polaroid: The image will be displayed inside this class.

Step 19: Creating Our Main Class

The Main class is the class which Polaroid.fla calls when it gets initiated.

Create a new ActionScript file and name it "Main.as", save it inside the src folder. The Main class looks as follows.

1
2
package {
3
4
	import flash.display.MovieClip;
5
6
	public class Main extends MovieClip {
7
8
		public function Main():void {
9
			
10
		}
11
	}
12
}

It's so empty because we first need to create our other classes, while we're busy we're going to keep on adding code to the Main class.


Step 20: Explaining Absolute and Relative File Paths

In this project we're loading several assets: an xml file and various images. We can use relative file paths or absolute paths. Let me explain the difference with an example; we have the following folder structure:

An imaginary folder setup, we're using this example to understand the difference between absolute and relative file pathsAn imaginary folder setup, we're using this example to understand the difference between absolute and relative file pathsAn imaginary folder setup, we're using this example to understand the difference between absolute and relative file paths

Loader.swf must load image1.jpg. From its current position Loader.swf must "open" the images directory and load image1.jpg. We tell Loader.swf to load images/image1.jpg. This type of loading is called relative file path loading, because the url is relative to Loader.swf's current position.

So how do you load image1.jpg, regardless of where Loader.swf is located? This is done with absolute file path loading. The file path starts from the upper core, from the root. If it's on a computer, its root is a hard drive. We're deploying our project online, so the root would be the site server.


Step 21: Why We Need Absolute File Paths

When our project is done, we'd like to deploy it. Most probably it would be embedded inside an HTML page. There's one small problem. If Loader.swf wants to load, it will load from its current location, from the location of the HTML page. We don't want this, and therefore we must use absolute file path loading.

Now comes the biggest problem, we won't know the location of the HTML file. We need to find the absolute path to the .swf file, and edit that path so we can load our imagery.

We can get the absolute path to the .swf file with root.loaderInfo.url.

It's time to create our ImageLoader class.


Step 22: Creating Our ImageLoader Class

Create a new ActionScript file and name it ImageLoader.as, save it inside the .src folder. The class looks like this, it has been thoroughly commented:

1
2
package  {
3
	//import the classes we need

4
	import flash.net.URLLoader;
5
	import flash.net.URLRequest;
6
	import flash.events.Event;
7
	import flash.display.Loader;
8
	import flash.display.Bitmap;
9
	import flash.display.Sprite;
10
	
11
	public class ImageLoader extends Sprite{
12
		//will load our xml

13
		private var xmlLoader:URLLoader = new URLLoader();
14
		
15
		//stores the xml

16
		private var xml:XML;
17
		
18
		//a list of file paths to the images

19
		private var imageFileList:XMLList;
20
		
21
		//the path to the .swf file

22
		private var swfPath:String;
23
		
24
		//the project folder path (where the folders bin, src and lib are located)

25
		private var projectPath:String;
26
		
27
		//we must know the title of our project, so we can edit the .swf url to get our projectPath

28
		private var projectTitle:String;
29
		
30
		//we need to count how many images we've already loaded

31
		private var count:uint = 0;
32
		
33
		//we must store the images, the images are Bitmaps

34
		public var images:Vector.<Bitmap> = new Vector.<Bitmap>;
35
		
36
		public function ImageLoader(_projectTitle:String) {
37
			//we must know when ImageLoader has been added, then we can figure out what the root's url is

38
			addEventListener(Event.ADDED, whenAdded);
39
			
40
			//store the project's title

41
			projectTitle = _projectTitle;
42
			
43
			//when imageLoader is done with loading, call xmlLoaded

44
			xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
45
		}
46
		
47
		private function whenAdded(event:Event):void {
48
			//store the swf path

49
			swfPath = root.loaderInfo.url;
50
			
51
			//get our projectPath

52
			projectPath = swfPath.substr(0, swfPath.length - projectTitle.length);
53
		}
54
		
55
		//this function will load the images from an xml, and store the images

56
		public function load(url:String):void {
57
			//load the xml, which we'll need to load the images

58
			xmlLoader.load(new URLRequest(projectPath + url));			
59
		}
60
		
61
		private function xmlLoaded(event:Event):void {
62
			//store the xml, which xmlLoader has loaded

63
			xml = new XML(event.target.data);
64
			
65
			//store all the file links in an XMLList

66
			imageFileList = new XMLList(xml.image.file);
67
			
68
			//now that we have the file information, we can load the images

69
			loadImages();
70
		}
71
		
72
		private function loadImages():void {
73
			for each (var imageFile:String in imageFileList){
74
				//create

75
				var imageLoader:Loader = new Loader();
76
				//whenever an image has been loaded, call imageLoaded

77
				imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded, false, 0, true);
78
				imageLoader.load(new URLRequest(projectPath + imageFile));
79
			}
80
		}
81
		
82
		private function imageLoaded(event:Event):void {
83
			//we're counting how many images we've already loaded, we've just loaded (another) one

84
			count++;
85
			
86
			//get the image Bitmap

87
			var image:Bitmap = Bitmap(event.target.loader.content);
88
			
89
			//store the image in our images Vector

90
			images.push(image);
91
			
92
			//if this is the last image we needed to load

93
			if(count == imageFileList.length()){
94
				//we're done, dispatch an event

95
				dispatchEvent(new Event('imagesLoaded'));
96
			}
97
			
98
			//--cleanup

99
			//unload the loader

100
			event.target.loader.unload();
101
		}
102
		
103
		public function cleanUp():void {
104
			xmlLoader.data = null;
105
			xmlLoader = null;
106
			xml = null;
107
			imageFileList = null;
108
			count = 0;
109
			images = null;
110
		}
111
	}
112
	
113
}

Step 23: Improving Our Main Class

Now that we have our ImageLoader class, we can improve our Main class.

1
2
package {
3
4
	import flash.display.MovieClip;
5
	import flash.events.Event;
6
7
	public class Main extends MovieClip {
8
		//a custom class we've made which will load all images from an xml file as Bitmaps

9
		private var imageLoader:ImageLoader;
10
		
11
		//this class will hold and display all our Polaroids

12
		private var polaroidHolder:PolaroidHolder;
13
14
		public function Main():void {
15
			//load our images, give imageLoader the name of the .swf file, so it can load using absolute paths

16
			imageLoader = new ImageLoader('Polaroid.swf');
17
			addChild(imageLoader);
18
			imageLoader.addEventListener('imagesLoaded', displayImages, false, 0, true);
19
			imageLoader.load('pictures.xml');
20
		}
21
22
		private function displayImages(event:Event):void {
23
			//we've loaded all our images

24
			polaroidHolder = new PolaroidHolder(imageLoader.images);
25
			addChild(polaroidHolder);
26
27
			//also clean up the imageLoader

28
			imageLoader.cleanUp();
29
			imageLoader = null;
30
		}
31
	}
32
}

Step 24: Creating Our PolaroidHolder Class

The PolaroidHolder will receive the images as a Vector. After receiving the images, it will create a Polaroid for each image. Let's create our PolaroidHolder class:

1
2
package {
3
	import flash.display.MovieClip;
4
	import flash.display.Bitmap;
5
	import flash.events.MouseEvent;
6
7
	public class PolaroidHolder extends MovieClip {
8
9
		//this vector will hold all our Polaroids

10
		public var polaroids:Vector.<Polaroid>  = new Vector.<Polaroid>;
11
		//with this variable we no

12
		private var zoomedIn:Boolean;
13
		//the Polaroid we're zoomed in on

14
		private var currentPolaroid:Polaroid;
15
16
		public function PolaroidHolder(images:Vector.<Bitmap>) {
17
			// constructor code

18
			for each (var image:Bitmap in images) {
19
				//create a new Polaroid

20
				var polaroid:Polaroid = new Polaroid(image);
21
22
				//position and rotate it

23
				polaroid.x = Math.random() * 200 + 100;
24
				polaroid.y = Math.random() * 200;
25
				polaroid.rotation = Math.random() * 60 - 30;
26
27
				//add a clicking eventListener

28
				polaroid.addEventListener(MouseEvent.CLICK, onPolaroidClick);
29
				addChild(polaroid);
30
31
				//add it to the polaroids vector

32
				polaroids.push(polaroid);
33
			}
34
		}
35
36
		private function onPolaroidClick(event:MouseEvent):void {
37
			//there's been a click on a Polaroid!

38
			if (currentPolaroid == null) {
39
				//no currentPolaroid has been set, we're zooming in

40
				//set the clicked Polaroid as the current Polaroid

41
				currentPolaroid = event.currentTarget as Polaroid;
42
43
				//mute all other Polaroids (the ones which aren't zoomed in)

44
				for each (var polaroid:Polaroid in polaroids) {
45
					if (polaroid != currentPolaroid) {
46
						polaroid.mute();
47
					}
48
				}
49
			} else {
50
				//check if the click was on the current polaroid

51
				if (event.currentTarget == currentPolaroid) {
52
					//the same polaroid has been clicked (again), time to zoom out

53
54
					//unmute all other Polaroids, they can respond again to events

55
					for each (var polaroid:Polaroid in polaroids) {
56
						if (polaroid != currentPolaroid) {
57
							polaroid.unmute();
58
						}
59
					}
60
61
					//now we're not zoomed in, so we don't have a current polaroid

62
					currentPolaroid = null;
63
				}
64
			}
65
		}
66
67
	}
68
69
}

In lines 50 and 62, we call the functions mute() and unmute(). Mute makes the Polaroid numb to all events, it won't respond to mouse overs, or clicks. This is precisely what we want when we're zoomed in. We don't want other Polaroids to do anything. After we're zooming out, we want the Polaroids to respond to events; by calling unmute, the Polaroids will respond again to mouse overs and clicks.


Step 25: Creating Our Polaroid Class

Now it's time to create our most interesting class, our Polaroid class! For this class we'll need the popular tweening library tweener. Download it, and save the library inside the src folder (see this guide for more details).

1
2
package {
3
	import flash.display.MovieClip;
4
	import flash.events.MouseEvent;
5
6
	import caurina.transitions.*;
7
	import caurina.transitions.properties.*;
8
	import flash.display.Bitmap;
9
	import flash.events.Event;
10
11
	public class Polaroid extends MovieClip {
12
13
		//the scale at which the Polaroid is zoomed in

14
		private static const ZOOMED_IN_SCALE:Number = 1;
15
		//the scale at which the Polaroid is zoomed out

16
		private static const ZOOMED_OUT_SCALE:Number = .6;
17
18
		//the max height an image can be

19
		private var IMAGE_MAX_HEIGHT:Number;
20
		//the max width an image can be

21
		private var IMAGE_MAX_WIDTH:Number;
22
23
		//we must scale the loaded images, remember the scale

24
		private var scale:Number;
25
26
		//when we zoom out, we zoom out back to the Polaroid's original position and rotation

27
		//the Polaroid's original x coordinate.

28
		private var origX:Number;
29
		//we also need the original y coordinate

30
		private var origY:Number;
31
		//store the Polaroid's original rotation

32
		private var origRotation:Number;
33
		//when this Boolean becomes true, the Polaroid will not respond to mouse events

34
		private var muted:Boolean;
35
		//we need to know if the Polaroid has been clicked

36
		public var clicked:Boolean;
37
38
		public function Polaroid(image:Bitmap) {
39
			//make the Polaroid a button

40
			buttonMode = true;
41
42
			//the maximum dimensions for an image are limited by the dimensions of imageHolder

43
			IMAGE_MAX_HEIGHT = imageHolder.height;
44
			IMAGE_MAX_WIDTH = imageHolder.width;
45
46
			//scale the Polaroid down

47
			scaleX = ZOOMED_OUT_SCALE;
48
			scaleY = ZOOMED_OUT_SCALE;
49
50
51
			//we'll need to resize the image to make it fit

52
			//first check if it is a portrait or landscape

53
			if (image.width > image.height) {
54
				//it is a landscape

55
				//the scale is limited by the image's height

56
				scale = IMAGE_MAX_HEIGHT / image.height;
57
			} else {
58
				//it is a portrait

59
				//the scale is limited by the image's width

60
				scale = IMAGE_MAX_WIDTH / image.width;
61
			}
62
63
			//scale the image

64
			image.scaleX = scale;
65
			image.scaleY = scale;
66
67
			//add the scaled image 

68
			imageHolder.addChild(image);
69
70
			//add event listeners to the Polaroid;

71
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
72
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
73
			addEventListener(MouseEvent.CLICK, onClick);
74
			addEventListener(Event.ADDED, whenAdded);
75
		}
76
77
		public function mute():void {
78
			//calling this function will make the Polaroid not respond to events

79
			buttonMode = false;
80
			muted = true;
81
		}
82
83
		public function unmute():void {
84
			//calling this function will make the Polaroid respond to events

85
			buttonMode = true;
86
			muted = false;
87
		}
88
89
		private function whenAdded(event:Event):void {
90
			//store the Polaroid's original coordinates and rotation

91
			origX = x;
92
			origY = y;
93
			origRotation = rotation;
94
95
			//we won't need this event listener anymore, remove it

96
			removeEventListener(Event.ADDED, whenAdded);
97
		}
98
99
		private function onMouseOver(event:MouseEvent):void {
100
			//only respond if the Polaroid has not been muted

101
			if (! muted) {
102
				//only respond if the Polaroid has not been clicked, clicked Polaroids should not respond to mouse overs.

103
				if (! clicked) {
104
					//move the polaroid to the front

105
					parent.setChildIndex(this, parent.numChildren - 1);
106
					//move and turn the polaroid in a random direction

107
					Tweener.addTween(this, {x: origX + Math.random() * 30 - 15, y: origY + Math.random() * 30 -15, rotation:origRotation + Math.random() * 20 - 10, time:1});
108
				}
109
			}
110
		}
111
112
		private function onMouseOut(event:MouseEvent):void {
113
			//only respond if the Polaroid has not been muted

114
			if (! muted) {
115
				//only respond if the Polaroid has not been clicked

116
				if (! clicked) {
117
					//move the polaroid back to it's original position and rotation

118
					Tweener.addTween(this, {x: origX, y:origY, rotation:origRotation, time:1});
119
				}
120
			}
121
		}
122
123
		private function onClick(event:MouseEvent):void {
124
			//only respond if the Polaroid has not been muted

125
			if (! muted) {
126
				//if it hasn't been clicked before

127
				if (! clicked) {
128
					//we've been clicked, let's make that true

129
					clicked = true;
130
131
					//we're zooming in, let's dispatch an event (Main will pick this up)

132
					dispatchEvent(new Event('zoomIn'));
133
134
					//zoom the Polaroid in to the center of the stage

135
					Tweener.addTween(this, {x:stage.stageWidth/2 - width/2, y:stage.stageHeight/2 - height/2, rotation:0, scaleX:ZOOMED_IN_SCALE, scaleY:ZOOMED_IN_SCALE, time: 1});
136
				} else {
137
					//we've already been clicked, so make clicked false

138
					clicked = false;
139
140
					//we're zooming out, let's dispatch an event (Main will pick this up)

141
					dispatchEvent(new Event('zoomOut'));
142
143
					//zoom the polaroid out, back to its original position

144
					Tweener.addTween(this, {x: origX, y:origY, rotation:origRotation, scaleX: ZOOMED_OUT_SCALE, scaleY:ZOOMED_OUT_SCALE, time:1});
145
				}
146
			}
147
		}
148
	}
149
}

Step 26: Adding Our PolaroidHolder to Our Main Class

Make the changes as seen in the code below.

1
2
package {
3
4
	import flash.display.MovieClip;
5
6
	public class Main extends MovieClip {
7
8
		//a custom class we've made which will load all images from an xml file as Bitmaps

9
		private var imageLoader:ImageLoader;
10
		
11
		private var polaroidHolder:PolaroidHolder;
12
13
		public function Main():void {
14
			//load our imagges

15
			imageLoader = new ImageLoader();
16
			imageLoader.addEventListener('imagesLoaded', displayImages, false, 0, true);
17
			imageLoader.load('pictures.xml');
18
		}
19
20
		private function displayImages(event:Event):void {
21
			//we've loaded all our images

22
			polaroidHolder = new PolaroidHolder(imageLoader.polaroids);
23
			addChild(polaroidHolder);
24
25
			//also clean up the imageLoader

26
			imageLoader.cleanUp();
27
			imageLoader = null;
28
		}
29
	}
30
}

Step 27: The Result So Far

It seems to work, but it's not very interesting. When zooming in, the user is not "pulled in", our product is not immersive enough. What we need is a background! By chance we created one in Step 4. Time to add our background!


Step 28: Adding Our Background

We had exported our background with the class Background.

Not only will we add Background as a child of Main, we'll also make Background darken when we're zoomed in, and lighten when we're zoomed out. How do we do this? Whenever a Polaroid is clicked, it dispatches a custom event, zoomIn or zoomOut. We can trace this event, just add event listeners to our Main class, they'll pick up the zoomIn and zoomOut events, and whenever they're picked up, we can tell our Background to lighten or darken:

1
2
package {
3
4
	import flash.display.MovieClip;
5
	import flash.events.Event;
6
	
7
	import caurina.transitions.*;
8
	import caurina.transitions.properties.*;
9
10
	public class Main extends MovieClip {
11
12
		//a custom class we've made which will load all images from an xml file as Bitmaps

13
		private var imageLoader:ImageLoader;
14
		
15
		//this class will display all our Polaroids, by receiving a Vector of Bitmaps

16
		private var polaroidHolder:PolaroidHolder;
17
		
18
		//the wooden background, bg is an abbrivation for background

19
		private var bg:Background
20
21
		public function Main():void {
22
			//load our images

23
			imageLoader = new ImageLoader('Polaroid.swf');
24
			imageLoader.addEventListener('imagesLoaded', displayImages, false, 0, true);
25
			addChild(imageLoader);
26
			imageLoader.load('pictures.xml');
27
			
28
			//add our bg as a child

29
			bg = new Background();
30
			addChild(bg);
31
			
32
			//add event listener, so we know when we're zoomed in or out

33
			//3rd parameter of addEventListener is true, so we can capture the event, when it "bubbles" up from the Polaroid

34
			addEventListener('zoomIn', onZoomIn, true);
35
			addEventListener('zoomOut', onZoomOut, true);
36
		}
37
38
		private function displayImages(event:Event):void {
39
			//we've loaded all our images

40
			polaroidHolder = new PolaroidHolder(imageLoader.images);
41
			addChild(polaroidHolder);
42
43
			//also clean up the imageLoader

44
			imageLoader.cleanUp();
45
			imageLoader = null;
46
		}
47
		
48
		private function onZoomIn(event:Event):void {
49
			//upon zooming in, we'll darken the bg

50
			Tweener.addTween(bg, {alpha:.6, time:1});
51
		}
52
		
53
		private function onZoomOut(event:Event):void {
54
			//upon zooming out, we'll lighten the bg

55
			Tweener.addTween(bg, {alpha:1, time:1});
56
		}
57
	}
58
}

Step 29: Looking Back

Tada! We're done, this was quite a tutorial! I hope you guys had fun following this tutorial, and also learnt something from it. Why not play around with the code? You could try adding text fields and somehow display the title of the image. Or use several types of Polaroids. Thanks, and feel free to comment if you have any questions or comments. Keep an eye out for my next tutorial!