Thursday 10 September 2009

Create a Shuffle Gallery in Flash Using XML and ActionScript 3.0

Step 1: Overview

Using XML we'll dynamically load and obtain information about the images, give them a random center position, add a frame, add drag functionality, and lastly, we'll use a tween to handle the zoom animation.

Step 2: Let's Get Started

Open Flash and create a new Flash File (ActionScript 3).

Set the stage size to 600x350 and add a gray radial gradient (#EEEEEE, #DDDDDD).

””

Step 3: Adding a Preloader

We're going to add a preloading animation to tell the user when the content is loading. In this case I used the Apple inspired preloader that we created before. Since we're going to use only the animation, there's no need to import the class or use an Export Identifier.

Place the preloader on the stage and center it.

Step 4: Embedding a Font

We're going to embed a font, a super easy task when adding a TextField to the Stage in the Flash IDE, but a little different using ActionScript.

Open the Library Panel and right-click in the items area without selecting one, a contextual menu will appear.

Click on "New Font" to open a dialog window, give a name to your font and select the one you want to use as shown in the following image.

This will create a class of the font you selected, we'll instantiate this in Step 9.

Step 5: XML

Let's create the XML file.

Open your prefered XML or Text editor and write:

  1. xml version="1.0" encoding="UTF-8"?>
  2. <images>
  3. <image src="images/image.jpg" title="This is image 1"/>
  4. <image src="images/image2.jpg" title="This is image 2"/>
  5. <image src="images/image3.jpg" title="This is image 3"/>
  6. <image src="images/image4.jpg" title="This is image 4"/>
  7. <image src="images/image5.jpg" title="This is image 5"/>
  8. images>

When you're done, save it as "images.xml" in your xml folder.

Step 6: ActionScript

The code that we'll use will be written in a single class that will be used as the Document Class in the FLA file.

Create a new ActionScript File (File > New)

Save it as "Main.as".

Step 7: Package

We'll begin with:

The package keyword allows you to organize your code into groups that can be imported by other scripts, it's recommended to name them starting with a lowercase letter and use intercaps for subsequent words for example: galleryClasses.

If you don't want to group your files in a package or you have only one class, you can use it right from your source folder, but the idea is to be organized.

Step 8: Required Classes

  1. import flash.display.Sprite;
  2. import flash.display.MovieClip;
  3. import flash.net.URLLoader;
  4. import flash.net.URLRequest;
  5. import flash.display.Loader;
  6. import flash.events.Event;
  7. import flash.filters.BitmapFilter;
  8. import flash.filters.DropShadowFilter;
  9. import flash.text.TextFormat;
  10. import flash.text.TextField;
  11. import flash.text.AntiAliasType;
  12. import flash.events.MouseEvent;
  13. import fl.transitions.Tween;
  14. import fl.transitions.easing.Strong;
  15. import fl.transitions.TweenEvent;

These are the classes that we'll need to make this gallery. If you need help with a specific class please use the Flash Help (F1).

Step 9: Extending the Class

  1. public class Main extends MovieClip
  2. {

The extends keyword defines a class that is a subclass of another class. The subclass inherits all the methods, properties and functions, that way we can use them in our class.

We're going to use MovieClip specific methods and properties so we extend using the MovieClip Class.

Step 10: Variables

  1. var xml:XML; // The XML Object that will parse the XML File
  2. var images:Array = new Array(); //This array will store the images loaded
  3. var imagesLoaded:int = 0; //A counter, counts the images loaded
  4. var imagesTitle:Array = new Array(); //The title properties of the XML File
  5. var tween:Tween; //Handles the animation
  6. var zoomed:Boolean = false; //Checks if a picture is zoomed, false by default
  7. var canClick:Boolean = true; //Checks if the user can click a picture to zoom it, true by default
  8. var lastX:int; //Stores the x property of the last picture that was clicked
  9. var lastY:int; //Stores the y property of the last picture that was clicked
  10. var textformat:TextFormat = new TextFormat(); //A TextFormat Object
  11. var screen:Sprite = new Sprite(); //A black screen to focus on the active picture
  12. var formatFont:Avenir = new Avenir(); //This is the embedded font

Step 11: Constructor

The constructor is a function that runs when an object is created from a class. This code is the first to execute when you make an instance of an object or when using the Document Class.

In this function we'll set the properties of the TextFormat object that we'll use to display a title or a description of each image. Create the black screen that appears when the user clicks on a picture and call the function which loads the desired XML file.

  1. public function Main():void
  2. {
  3. textformat.color = 0xFFFFFF;
  4. textformat.font = formatFont.fontName;
  5. textformat.size = 17; //Use the same size you used when embedding the font from the Library
  6. screen.graphics.beginFill(0x111111, .75);
  7. screen.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
  8. screen.graphics.endFill();
  9. loadXML("xml/images.xml");
  10. }

Step 12: XML Loader Function

This function loads the XML file provided by the "file" parameter. We also add a listener to handle when the load is complete.

  1. private function loadXML(file:String):void
  2. {
  3. var urlLoader:URLLoader = new URLLoader();
  4. var urlReq:URLRequest = new URLRequest(file);
  5. urlLoader.load(urlReq);
  6. urlLoader.addEventListener(Event.COMPLETE, handleXML);
  7. }

Step 13: Parse XML

Here we convert the loaded XML file to a valid XML object using the parameter "data" of the URLLoader. Then we use a "for" statement to create a Loader for every image in the XML. Additional information is found in the commentary.

  1. private function handleXML(e:Event):void
  2. {
  3. xml = new XML(e.target.data);
  4. for (var i:int = 0; i <>
  5. {
  6. var loader:Loader = new Loader();
  7. loader.load(new URLRequest(String(xml.children()[i].@src)));
  8. images.push(loader); //Adds the Loaders to the images Array to gain access to them outside this function
  9. imagesTitle.push(xml.children()[i].@title); //Adds the title attribute content to the array to use it outside this function
  10. loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded); //A listener to the function that will be executed when an image is loaded
  11. }
  12. }

0 nhận xét:

Post a Comment

 

Blogroll

Site Info

Text

Tut - Designer Copyright © 2009 WoodMag is Designed by Ipietoon for Free Blogger Template