Best Design Tutorials

Design Tutorials provides the best and educational tutorials for your skills improvement

Wikipedia

Search results

Create a 3D Flickr Photo Gallery with Flex and Away3D

Step 1: Reviewing the Flickr API

This application will retrieve photos from Flickr to be displayed within a 3D space. Briefly, the process of downloading photos from Flickr is as follows:

  • A Flickr API key is obtained.
  • The AS3Flickr and AS3CoreLib ActionScript libraries are downloaded and referenced from Flex Builder.
  • The Flickr cross domain policy files are loaded to allow Flex to access images from remote domains.
  • A search of Flickr is performed.
  • The results are used to find the URL of the Flickr images.
  • The images themselves are downloaded and saved/displayed.

This process is covered in depth by a previous tutorial which you can find here. I highly recommend you read that tutorial if you are not familiar with the AS3Flickr library, as these points will only be briefly mentioned in this tutorial.

Step 2: An Introduction to Away3D Lite

The Flash runtime has made some impressive progress over the last few years. The widespread deployment of the Flash runtime, both with Adobe AIR and browser plugins, along with the performance increase in later versions, means Flash has a very diverse ecosystem of libraries and applications. It has gone from being an animation tool to a general purpose development platform for games, business applications, 3D visualizations, video players and more.

3D engines have evolved alongside Flash for some time, going from a curiosity to now being fully featured and even commercial libraries. Away3D is one of the more popular free Flash 3D engines, and recently a smaller, faster version of Away3D was released called Away3D Lite. While Away3D Lite lacks some of the eye candy present in the more complicated 3D engines, its focus on simplicity means that it is perfect for a simple application like a 3D photo album.

Step 3: Get Away3D Lite

You can download a copy of Away3D Lite 1.0 for free from this web page here. Download and extract the engine source code to a convenient location.

Step 4: Get the Greensock TweenMax Library

The movement of the camera within the 3D photo gallery will be performed using the Greensock TweenMax library, which you can get from this page here. Again, download and extract the library to a convenient location.

Step 5: Get AS3Flickr and AS3CoreLib

The AS3Flickr and AS3CoreLib libraries are used to contact the Flickr web service. You can find details about these libraries from the article Build a Photo Viewer Using Flex and the Flickr API.

Step 6: Create a New Project

Create a new Flex project, and add the Away3D Lite, TweenMax, AS3Flickr and AS3CoreLib libraries to the Flex Build Path.

Step 7: Define the Application Attributes

applicationComplete="appComplete()"

This sets the appComplete function to be called when the application has initialized, and will serve as the entry point for our code.

frameRate="100"

This sets the frame rate cap for the application at 100 frames per second. The default is 24, and increasing this will make the application seem more fluid.

width="600" height="400" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#000000, #374040]"

These attributes define the size and background color of the Flex application.

Your Application tag should look something like the code below:

view plaincopy to clipboardprint?
  1. <mx:Application
  2. xmlns:mx="http://www.adobe.com/2006/mxml"
  3. layout="absolute"
  4. applicationComplete="appComplete()"
  5. width="600"
  6. height="400"
  7. backgroundGradientAlphas="[1.0, 1.0]"
  8. backgroundGradientColors="[#000000, #374040]"
  9. frameRate="100">
  10. mx:Application>

Step 8: Add Some Controls

Add the following tags to the Application tag.

view plaincopy to clipboardprint?
  1. <mx:TextInput x="10" y="10" id="txtSearch"/>
  2. <mx:Button x="178" y="10" label="Search" fillAlphas="[0.9, 0.9, 0.9, 0.9]" click="{applicationManager.flickrSearch(this.txtSearch.text)}"/>

We'll give the user the ability to search Flickr and display the results within our 3D photo gallery at run time. These two tags add a text box, where the search phrase can be entered, and a button to initiate the search.

The click event for the button calls applicationManager.flickrSearch(this.txtSearch.text). The ApplicationManager will be created in later steps, and the flickrSearch function will contain the code to contact Flickr and download the photos.

Step 9: Add Some Code

Add a Script tag to the Application tag. This is where the ActionScript code will be written.

view plaincopy to clipboardprint?
  1. <mx:Script>
  2. // code goes here
  3. ]]>
  4. mx:Script>

Step 10: Add the Variables

Add the following variable inside the Script tag.

view plaincopy to clipboardprint?
  1. private var applicationManager:ApplicationManager = null;

The ApplicationManager class will be responsible for initializing the Away3D engine, loading the Flickr images and moving the camera. We hold a reference to it in the applicationManager variable so the button created in step 8 can call the flickrSearch function.

Step 11: Loading the Cross Domain Policy Files

view plaincopy to clipboardprint?
  1. Security.loadPolicyFile("http://api.flickr.com/crossdomain.xml");
  2. Security.loadPolicyFile("http://farm1.static.flickr.com/crossdomain.xml");
  3. Security.loadPolicyFile("http://farm2.static.flickr.com/crossdomain.xml");
  4. Security.loadPolicyFile("http://farm3.static.flickr.com/crossdomain.xml");
  5. Security.loadPolicyFile("http://farm4.static.flickr.com/crossdomain.xml");

In order to access the images on the Flickr servers the cross domain policy files need to be loaded. This is covered in more detail in the article Build a Photo Viewer Using Flex and the Flickr API.

Step 12: The appComplete Function

Add a new function called appComplete.

view plaincopy to clipboardprint?
  1. private function appComplete():void
  2. {
  3. applicationManager = new ApplicationManager();
  4. this.addChildAt(new SpriteUIComponent(applicationManager), 0);
  5. }

This function is called when the Flex application has loaded and initialized. It is here that we create a new instance of the ApplicationManager class and add it as a child control. Notice that we actually pass an instance of a class called SpriteUIComponent to the addChildAt function.

As you will see in later steps, the ApplicationManager extends one of the template classes from the Away3D Lite API. These template classes themselves extend the Flex Sprite class. Even though the addChildAt function will accept a Sprite, an exception will be raised at runtime. This is because only classes extending the UIComponent class can be added a child controls of a Flex application. To work around this limitation the SpriteUIComponent extends the UIComponent class, and then adds a Sprite as its own child.

Step 13: The SpriteUIComponent Class

view plaincopy to clipboardprint?
  1. package
  2. {
  3. import flash.display.Sprite;
  4. import mx.core.UIComponent;
  5. public class SpriteUIComponent extends UIComponent
  6. {
  7. public function SpriteUIComponent(sprite:Sprite)
  8. {
  9. super ();
  10. explicitHeight = sprite.height;
  11. explicitWidth = sprite.width;
  12. addChild (sprite);
  13. }
  14. }
  15. }

As was mentioned above, the SpriteUIComponent class extends the UIComponent class (meaning it can be added as a child of a Flex application), and then adds the supplied Sprite object as its own child.

Step 14: The ApplicationManager Class

Import the following packages

view plaincopy to clipboardprint?
  1. import away3dlite.core.base.*;
  2. import away3dlite.core.utils.*;
  3. import away3dlite.loaders.*;
  4. import away3dlite.materials.*;
  5. import away3dlite.primitives.*;
  6. import away3dlite.templates.*;
  7. import com.adobe.webapis.flickr.*;
  8. import com.adobe.webapis.flickr.events.*;
  9. import flash.display.*;
  10. import flash.events.*;
  11. import flash.geom.*;
  12. import flash.net.*;
  13. import com.greensock.*;
  14. import mx.collections.*;

Step 15: Extend the BasicTemplate

Define the ApplicationManager class as extending the BasicTemplate class.

view plaincopy to clipboardprint?
  1. public class ApplicationManager extends BasicTemplate

A new feature included with Away3D Lite are the template classes. Almost every Away3D application needs to initialize the same underlying classes like a camera and a view, and also setup event listeners for frame events. These common steps, which used to have to be coded by each developer manually, are now performed by one of several template classes. We will extend one of these template classes, BasicTemplate, with the ApplicationManager.

Step 16: Define the Constants

The ApplicationManager will have a number of constant variables that define the appearance and function of the class.

view plaincopy to clipboardprint?
  1. private static const SEARCH_STRING:String = "landscape";
  2. private static const MAX_RESULTS:int = 50;
  3. private static const API_KEY:String = "YourFlickrAPIKey";

These three constants relate to the Flickr web service.

view plaincopy to clipboardprint?
  1. private static const PHOTO_HEIGHT:Number = 50;
  2. private static const PHOTO_WIDTH:Number = PHOTO_HEIGHT * 1.618;

These two variables define the size of the photos in 3D space. The width is defined relative to the height using the golden rectangle ratio, which produces a rectangle whose shape is generally considered aesthetically pleasing.

view plaincopy to clipboardprint?
  1. private static const PHOTO_CLOUD_WIDTH:Number = 1000;
  2. private static const PHOTO_CLOUD_HEIGHT:Number = 1000;
  3. private static const PHOTO_CLOUD_DEPTH:Number = 3000;

These three variables define the area that all the photos will be contained in.

view plaincopy to clipboardprint?
  1. private static const NUMBER_PHOTOS:int = 300;

This variable defines how many individual photos will be created in the area defined above.

view plaincopy to clipboardprint?
  1. private static const CAMERA_JUMP_TIME:Number = 3;
  2. private static const CAMERA_DIST_FROM_PHOTO:Number = 30;
  3. private static const CAMERA_DIST_JUMP_BACK:Number = 100;

These variables define the movement and speed of the camera as it jumps from one photo to the next. The camera will follow a bezier curve, with the start of the curve being 30 units back from a photo, moving towards a point 100 units back, and then ending at a point 30 units back from the next photo.

Step 17: Embedding the Default Image

view plaincopy to clipboardprint?
  1. [Embed(source="../media/texture.jpg")]
  2. protected static const DefaultTexture:Class;

The images that will be displayed all come from Flickr, which means that there will be an initial delay as the photos are retrieved. In the mean time an image embedded into the SWF file will be displayed, to give the user something to look at other than white rectangles.

Step 18: Adding the Variables

view plaincopy to clipboardprint?
  1. protected var textures:ArrayCollection = new ArrayCollection();
  2. protected var photos:ArrayCollection = new ArrayCollection();
  3. protected var loadedTextures:int = 0;

The images returned from Flickr are stored in the textures collection. The Away3D Lite meshes that will display the images are stored in the photos collection. The loadedTextures variable keeps a track of how many images have been loaded from Flickr, so they can be stored in the appropriate position in the textures collection.

Step 19: Initializing the ApplicationManager

Add a new function called onInit.

view plaincopy to clipboardprint?
  1. override protected function onInit():void

Once the underlying BasicTemplate object has initialized itself, the onInit function will be called. It is here that we initialize the extending class.

view plaincopy to clipboardprint?
  1. for (var i:int = 0; i <>
  2. addTexture(Cast.bitmap(DefaultTexture));

No comments:

Post a Comment