/ Published in: ActionScript 3
You can access Flickr using AS3 the same way as Javascript with JSON. You just have to massage the data a bit. Here's how you would use the class. In your Main.as, create an instance of the FlickrLoader. You will need the JSON.decoder class from http://code.google.com/p/as3corelib/
Expand |
Embed | Plain Text
/************************ Use this in your Flash file ************************/ var photoSetLoader :FlickrLoader = new FlickrLoader(); photoSetLoader.load( "flickrPhotoSetID" ); photoSetLoader.addEventListener( Event.COMPLETE, photoSetCompleteHandler, false, 0, true ); function photoSetCompleteHandler( e:Event ):void { var flickrLoader:FlickrLoader = FlickrLoader( e.currentTarget ); flickrLoader.removeEventListener( Event.COMPLETE, photoSetCompleteHandler ); var totalImages:Number = flickrLoader.getImageData().length; var currentImage:uint = Math.floor( Math.random() * totalImages ); trace( flickrLoader.getImageData() ); } /************************ Place this is a file called FlickrLoader.as ************************/ package { import com.adobe.serialization.json.JSON; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.HTTPStatusEvent; import flash.events.IEventDispatcher; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.events.SecurityErrorEvent; import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.URLRequestMethod; import flash.net.URLVariables; public class FlickrLoader extends EventDispatcher { private var _data:Array; private static const FLICKR_API:String = "FLICKR_API_KEY"; public function FlickrLoader(target:IEventDispatcher=null) { super(target); } public function load( photoSetID:String ):void { var urlVars:URLVariables = new URLVariables(); urlVars.method = "flickr.photosets.getPhotos"; urlVars.api_key = FLICKR_API; urlVars.photoset_id = photoSetID; urlVars.format = "json"; var urlRequest:URLRequest = new URLRequest(); urlRequest.url = "http://api.flickr.com/services/rest/"; urlRequest.method = URLRequestMethod.GET; urlRequest.data = urlVars; var urlLoader:URLLoader = new URLLoader(); urlLoader.addEventListener( Event.COMPLETE, onJSONLoaded ); urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true); urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true); urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true); urlLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true); urlLoader.load( urlRequest ); } private function onJSONLoaded( e:Event ):void { _data = new Array(); var rawData = e.currentTarget.data as String; var data:String = rawData.slice( rawData.indexOf("{"), rawData.lastIndexOf("}") + 1 ); // trace( data ); var json:Object = JSON.decode( data ) as Object; var photos:Object = json.photoset.photo as Object; //Create new image objects for( var i:uint; i < photos.length; i++ ){ _data.push({ "title" : photos[i].title.toString(), "url" : "http://farm" + photos[i].farm + ".static.flickr.com/" + photos[i].server + "/" + photos[i].id + "_" + photos[i].secret + ".jpg" }); } //Fire once the XML has been converted into Image Objects dispatchEvent( new Event( Event.COMPLETE ) ); } public function getImageData():Array { return _data; } private function httpStatusHandler ( e:HTTPStatusEvent ):void { //trace("httpStatusHandler:" + e); } private function securityErrorHandler ( e:SecurityErrorEvent ):void { trace("securityErrorHandler:" + e); } private function ioErrorHandler( e:IOErrorEvent ):void { trace("ioErrorHandler: " + e); } private function progressHandler( e:ProgressEvent ):void { //trace(e.currentTarget.bytesLoaded + " / " + e.currentTarget.bytesTotal); } } }
Comments
Subscribe to comments
You need to login to post a comment.

Getting the following error on Compile:
FlickrLoader.as(63): col: 64 Error: Incorrect number of arguments. Expected no more than 0.
data.push( new Object (photos[i].title.toString(), "http://farm" + photos[i].farm + ".static.flickr.com/" + photos[i].server + "/" + photos[i].id + "" + photos[i].secret + ".jpg"));
Hey Andrew,
Well from what you've provided, one thing I notice right off the bat is that you might be missing an underscore(_) between photos[i].id and photos[i].secret.
You're also missing the underscore( _ ) on the _data array. Maybe when you're submitting the error message through this comments box, it strips the underscores out.
This looks like broken script..Its gives me the same error as Andrew...
1137: Incorrect number of arguments. Expected no more than 0. at
"http://farm" +
[I dont understand If you pass two value to an object how can you call it]
Can someone help me?? Or can someone post the fla thats working J
Hey jinx_dee,
I changed the code a bit to fix your issue. Fingers Crossed.
I see you can pull the title of the image out of the array, is there any way to pull the description of the image as well?