kelvinluck.com

a stroke of luck

Flashr code example

Update 3:

This is a very old page imported from my previous blog. If there is missing content below or anything that doesn’t make sense then please check the page on my old blog.

Update 2:

For the latest up to date information about Flashr please check out the new Flashr microsite

Update:

This example will no longer work as-is – you will need to update your Flashr files as described here.

I’ve put together a very simple example which demonstrates how easy it is to talk to the flickr API using Flashr.

First up, here is the finished product. It basically connects to flickr.com and gets a list of the 16 most recent photos for a given user (me in this case) and displays them all in a nice little grid.

The code to create this simple movie is extremely simple… Here it is in it’s entirety:

/**
* Class: UserRecentPhotos
*
* Author:
* Kelvin Luck
*
* See Also:
* http ://www.kelvinluck.com/projects/flashr-a-flickr-api-wrapper-for-flash
*/


import com.kelvinluck.flickr.Flickr;
import com.kelvinluck.flickr.FlickrResponseListener;
import com.kelvinluck.flickr.Person;
import com.kelvinluck.flickr.Photo;
import com.kelvinluck.util.LogWrapper;
import com.dynamicflash.utils.Delegate;

class com.kelvinluck.flickr.example.UserRecentPhotos
{
   
   /**
   * Variable: apiKey
   * Your API Key.
   *
   * See Also:
   * http ://www.flickr.com/services/api/misc.api_keys.html
   **/

   var apiKey:String = "REPLACE_ME";
   /**
   * Variable: userNsid
   * The NSID of the user whose photos you want to get
   **/

   var userNsid:String = "51035610516@N01";
   /**
   * Variable: _target
   * The MovieClip to attach the photo thumbnails to
   **/

   var _target:MovieClip;
   
   /**
   * Function: UserRecentPhotos
   * Constructor
   **/

   function UserRecentPhotos(target:MovieClip)
   {
      Stage.scaleMode = "noScale";
     
      // initalise logging to Flash's output window
      LogWrapper.getInstance().init();
      LogWrapper.getInstance().addTracePublisher();
     
      _target= target;
      var _flickr:Flickr = Flickr.getFlickr();
      _flickr.apiKey = apiKey;
      var _flickrResponseListener:FlickrResponseListener = new FlickrResponseListener();
      _flickrResponseListener.setSuppressOutput(true);
      _flickrResponseListener.onPeopleGetPublicPhotos = Delegate.create(this, onPeopleGetPublicPhotos);
      _flickr.peopleGetPublicPhotos(userNsid, null, 16,1);
   }
   /**
   * Function: onPeopleGetPublicPhotos
   * Called when the _flickrResponseListener hears a response to flikr.people.getPublicPhotos
   *
   * Parameters:
   * p               -  The person whose photos you just got.
   **/

   function onPeopleGetPublicPhotos(p:Person):Void
   {
      var photosArray:Array = p.getPhotos();
      var userNsid:String = p.nsid;
      for (var i:Number=0; i< photosarray .length; i++) {
         var thisPhoto:Photo = Photo(photosArray[i]);
         
         // create a movieclip to load the photo into
         var mc:MovieClip = _target.createEmptyMovieClip("photo"+i, i);
         
         // position the clips so they form a nice grid
         mc._x = 1 + (i%4) * 76;
         mc._y = 1 + Math.floor(i/4) * 76;
         // create a nested movieclip so that our onPress isn't overwritten by the jpg as it loads
         mc.createEmptyMovieClip("jpgHolder", 1);
         // and load the jpeg into it
         mc.jpgHolder.loadMovie(thisPhoto.thumbnailUrl);
         
         mc.photo = thisPhoto;
         
         // add the onPress to this movieclip to link to the relevant photo on flickr.com
         mc.onPress = function()
         {
            getURL(this["photo"].photoPageUrl, "_blank");
         };
      }
   }
   /**
   * Function: main
   * Entrypoint to the application.
   **/

   public static function main():Void
   {
      var u:UserRecentPhotos = new UserRecentPhotos(_root);
   }
   /**
   * Function: toString
   **/

   public function toString():String
   {
      return "[com.kelvinluck.flickr.example.UserRecentPhotos]";
   }
}

As you can see, an instance of Flickr and an instance of FlickrResponseListener are created. Then _flickr.peopleGetPublicPhotos is called. When a response is returned by flickr.com, _flickrResponseListener.onPeopleGetPublicPhotos is triggered. This function is passed a Person object. We can get this person’s photos with a simple call to Person.getPhotos. We then loop over those photos loading them into movieclips.

And that’s how easy it is! You can download the files used in this demo from here – there is instructions for building with MTASC or the Flash IDE. The file that you will be interested in looking at is com.kelvinluck.flickr.example.UserRecentPhotos.as.

Update 2:

Petit as written a step by step tutorial showing how to use this code as a basis for a copy of the flickr badge.

No Comments, Comment or Ping

Reply to “Flashr code example”