Flash_and_Flex_03_2009

ActionScript Development

of it and passing it our API Key. Finally, we call the first of our functions – the getUser function. The getUser function will use our FlickrService instance to call the FlickrS ervice.people.findByUsername() method found in the Flickr API, see (Listing 22). We just need to pass this our _username variable. Once we get a result, we will call the getSets function. The findByUsername method simply finds some information out about a user (based on their username) and returns it. All we need here is the NSID. With that, we can access a users photos and photosets. Once we get a response from our FlickrService, we can remove the eventListener and set our _nsid variable, see (Listing 23). Next we'll call the getAlbums function. The getAlbums function will call the Flick rService.photosets.getList() method, see (Listing 24). This requires the NSID of a user (which we've just set) and will return a list of

albums for that user. Once we get that list we will call the createAlbums function. The createAlbums function will loop through the data returned from our FlickrS ervice.photosets.getList method and create a PhotosetItem for each photoset, see (Listing 25). First we remove the eventListener from the FlickrService, then we create an array from the data that the event has returned. As we loop through the array, we create a PhotosetItem and pass it the object stored in that item of the array. Then we position the PhotosetItem (mine are 120 pixels wide, so I am spacing them 121 pixels apart so that there is a 1 pixel gap between them), add an eventListener to handle the mouse click, and finally add the Photoset to the albumHolder MovieClip that sits on the stage of your FLA. The loadPhotoset function is fired when the user clicks on one of the PhotosetItem instances and will be used to populate the ThumbList instance with the photos from

that photoset, see (Listing 26). First of all we remove any existing eventListener listening for the FlickrService.photosets.getPh otos() method. Then we replace it with a fresh eventListener and make the call to the aforementioned method. We need to pass this method the public ID variable that we set up in the PhotosetItem class. This will let the Flickr API know which photoset you are after, and return the photos from that set for you. The final function we need to write to complete the project is the loadThumbs function, see (Listing 27). This function will take the result of the FlickrService.p hotosets.getPhotos() method and create an new ThumbList instance by passing it the results. First we need to remove any existing ThumbLists. Then we simply create a new instance and pass it the photoSet.photo array found in the data returned by the FlickrResultEvent . Then we position the ThumbList (you values may be different depending on the layout of your design) and finally we add the albumList to the stage. That's it! Now run your movie and you should see your Flash portfolio being populated by your Flickr account. Conclusion In this tutorial you have learnt how to create a Flash Portfolio that can be managed using Flickr. There are many different uses this could have, but the methods you will use will be very similar. Once you have created this portfolio you will quickly be able to apply the same code to a completely different design, simply by changing a few values. From here you may want to add a scrollbar to your ThumbList to accommodate a Photoset with a lot of photos, or perhaps use the powerful FlickrService.photos. search() method to allow users to search through your portfolio. There are endless possibilities. If you haven't done so already, I encourage you to download the as3flickrlib and as3corelib source files, and then head over to http:// www.flickr.com/services/api / and start exploring the Flickr API. Enjoy!

Listing 24. Listing the user’s albums

private function getAlbums():void { FS.addEventListener(FlickrResultEvent.PHOTOSETS_GET_LIST, createPhotosets); FS.photosets.getList(_nsid); } Listing 25. Adding the albums private function createAlbums(e:FlickrResultEvent):void { FS.removeEventListener(FlickrResultEvent.PHOTOSETS_GET_LIST, createPhotosets); var photosets = e.data.photoSets as Array; for ( var i:uint = 0; i < photosets.length; i ++) { var photoset:PhotosetItem = new PhotosetItem(photosets[i] as Object); photoset.x = i * 121; photoset.addEventListener(MouseEvent.CLICK, loadPhotoset); photosetHolder.addChild(photoset); } } Listing 26. Loading the photos private function loadPhotoset(e:MouseEvent):void { FS.removeEventListener(FlickrResultEvent.PHOTOSETS_GET_PHOTOS, loadThumbs); FS.addEventListener(FlickrResultEvent.PHOTOSETS_GET_PHOTOS, loadThumbs); FS.photosets.getPhotos(e.currentTarget.ID); } Listing 27. Loading the thumbnails

private function loadThumbs(e:FlickrResultEvent):void { if (albumList) removeChild(albumList); albumList = new ThumbList(e.data.photoSet.photos); albumList.x = 3; albumList.y = 534; addChild(albumList); }

JASSA AMIRLANG Jassa Amir-Lang is a freelance Flash/Flex Developer based in sunny Adelaide, South Australia. He has been playing around with all things Flash for the last 10 years and is always looking for ways to implement ideas using the Flash platform. He is an active member of the Adobe User Group in South Australia. For more information you can check out his blog at www.bangersandflash.net.

03/2009 (5)

44

Made with FlippingBook HTML5