Creating a Flash Facebook application with the Facebook Actionscript API

Emanuele Feronato Actionscript 3, Facebook, Flash, Php

It’s time to see how can we build a Flash Facebook application.

What we are going to do is a Flash movie to be embedded in a Facebook application, able to interact with the user by publishing notes and stories on the wall.

The first thing we need is the official Facebook Actionscript API. This library contains all we need to create a complete Facebook Flash application.

You can download the source code at this link, but before messing with AS3, let’s create the PHP part.

I suggest you to read the Developing a Facebook Application for absolute beginners posts from 1 to 5 if you don’t know how to create a basic Facebook application.

Then, take a look at the PHP code:

Notice at lines 1-3 the way you can import CSS style sheets. Don’t forget to use htmlentities on the imported file.

Then, at lines 5-9 I created a simple javascript function called grant that just redirects the browser to the application page. That is, basically it refreshes the page, and it will be called once the user will grant (or won’t grant) the permission to publish contents on his wall by submitting the permission form as you can see at line 30.

This is possible thanks to next_fbjs="grant()".

Another important thing in this script you should arleady know is the way I ask for publishing permission.

You can ask for permission inside the Flash movie itself but I found it easier to ask directly from Php. This way, you can even hide the Flash movie if the user does not grant permissions.

But the core of the script, the think you did not see in previous tutorials is the way I include the Flash movie at line 34 with the fb:swf tag.

You can find the official documentation at this page, and the most interesting thing is Facebook is passing some interesting parameters to the movie.

Let’s see the Actionscript now… I used the button component to create the buttons but this is not important… here it is the commented script:

The interesting thing of this script is the way you can send notifications (line 44) and publish posts (line 49)… I won’t talk about notifications because Facebook will deprecate this method in late November/early December 2009 , but I want you to look at the way I publish the post following the format explained at the official attachment docs.

You can test the application at this page and download the full source code, library included.

Next time, we’ll see how to add more functions.

Comments 37

  1. Yarden Refaeli

    NEVER PUT YOUR SECRET KEY IN UR SWF, NEVER. Emenuelle, great post, I made a game for facebook, it called Snaker (http://apps.facebook.com/snaker-game). I would like to write a post in your blog about it, if you’ll like. I used PHP for the whole backend (instead of using the Actionscript library), and used AMFPHP to communicate between the flash and PHP. I’ll send you an email about it. It would be great honor to write a tutorial in your blog ;)

  2. Pingback: Websitemarketingbiz.Net- Hyprobulksms-Rahsia Jana Income mudah » Blog Archive » Creating a Flash Facebook application with the Facebook …

  3. Pingback: Down the Foxhole – News Flash

  4. brew

    excellent!!!

    You make the best tutorials on the web.

    How about a tutorial on making a friend selector to select which friend to send a message to.

    thanks again for a really useful tutorial!

  5. Pingback: Weekly Shared Items – 10. November, 2009 | TOXIN LABS - weblog of a german design student from wuerzburg

  6. Pingback: Creating a Flash Facebook application with the Facebook Actionscript API – part 2 : Emanuele Feronato

  7. Pingback: ??Facebook??????????? | God is not home today

  8. Pingback: Emanuele Feronato

  9. Muse

    Hi all! I’m kind ned help here..I was planning to do a facebook application using flash as my Final Year Project..need help for ideas. I’m looking for small scale,complete and interactive application. Last but not least, is it a ‘must’ to use CSS and php? I’m just beginner in flash and zero knowledge in php and css but I really2 wanted to learn in this field…hope u guys can help me here..

  10. robi2216

    Hi your tutorial is great!!!!

    I used the SendMail but I donĀ“t worked, please if you could help me in this case

    case “Send EMail” :
    fbcall = new SendEmail([uid], ‘Hi’, ‘Facebook is excelent’, ‘Hi‘);
    fbook.post(fbcall);

    thanks

  11. Shawn Keim

    Your tutorial is great, but your sample has the same problem I do with the posting of an item. It requires the user to approve all publications… most facebook users don’t want to do that. Do you know how to make it display the Feed Form for approval on Each post?

    Thanks in advance

  12. Markham Butler

    This is great stuff. In your tut, you say, “You can ask for permission inside the Flash movie itself but I found it easier to ask directly from Php.” Can you point me to info on how to ask permissions from within Flash?

  13. Fahim Akhter

    Finally, a decent tutorial the flash IDE. Thanks a lot for that.

    I have pretty much the same questions as everyone else.

    – First, how to authorize from within facebook.
    – Second, What about the publish this post popup , I don’t want to send messages to my users without asking them :)

    Would really appreciate the help

  14. Pingback: MoMo-Studio Development Blog » Blog Archive » Zetta Monster Plus….Facebook?

  15. kanyal

    Hi
    there is new tab or window open problem with this api app, how you solved it. can we test this app on desktop pc and when finished can be hosted on web. i am not talking for desktop app. i am asking about facebook web app. hope you will get idea what i want to clear
    Thanks

  16. gujarathi

    Hi,
    I am facing a problem in following code. I want my application and login screen should open in same browser window. Using this code It always opens in new window. Will anybody please tell me how to achieve it?
    Again the code in if/else block in the function Init() never gets executed. It directly goes to session.login() and opens new window.

    Please help me.

    public function Init():void
    {
    session = new FacebookSessionUtil(“MY_API_KEY”,”MY_SECRET_KEY”,loaderInfo);
    session.addEventListener(FacebookEvent.CONNECT, handle_Connect);
    fbook=session.facebook;
    trace(“initial login”);
    if(loaderInfo.parameters.fb_sig_added == true)
    {
    session.verifySession();
    }
    else if(loaderInfo.parameters.fb_sig_added == false)
    {
    navigateToURL(new URLRequest(“http://www.facebook.com/login.php?api_key=” +loaderInfo.parameters.fb_sig_api_key), “_top”);
    }
    else{
    session.login();
    onConfirmLogin();
    }
    }

    public function handle_Connect(event:FacebookEvent):void
    {
    trace(“handle_Connect”);
    var call1:FacebookCall = fbook.post(new GetInfo([fbook.uid],[GetInfoFieldValues.ALL_VALUES]));
    call1.addEventListener(FacebookEvent.COMPLETE,handle_getinfoComplete);
    }
    public function onConfirmLogin():void
    {
    trace(“onConfirmLogin”);
    session.validateLogin();
    }
    public function handle_getinfoComplete(event:FacebookEvent):void
    {
    trace(“handle_getinfoComplete”);
    user=(event.data as GetInfoData).userCollection.getItemAt(0) as FacebookUser;
    if( user == null )
    {
    trace(“handle_getinfoComplete,onConfirmLogin”);
    onConfirmLogin();
    }
    else
    {
    trace(“hi”,user.first_name+” “+user.last_name);

    StartGame();
    }

    }

  17. sankar

    I need Line by line explanation for the above sorry for asking this because i don’t get much details in face book documention.it means nuts i need from u guys please help me.

  18. sankar

    i asked explanations for face book API classes not for this tutorial.sorry for my poor way of communication.

    I need to get and post the image to Facebook. is it possible. please i am waiting for all ur valuable replies. thanks in advance

  19. pissed of

    fuck off ur tutorial it doens not work you just take the exemple of the as3 api liarrrrrrrrrrrr, dude put some good code in ur exemple and stop puting shit, make ur variable having type

  20. Karthik

    Hi,
    I’m very new Facebook applications.
    I’m trying to develop a flash game application on facebook.
    Please provide me with some sample code.
    That would be great helpfull for me.
    Thanks in advance.
    karthik655@gmail.com

  21. Jaz

    hello, we are now in our Capstone Project and we would like to create an application for facebook. My friend said that facebook application in flash is great… can you guide me :)) thank you so much ^__^

  22. Pingback: Technological Specifications | mmmmppp

  23. Pingback: Prospectus | mmmmppp

  24. Davai

    I’m trying to figure out how companies that are using flash to build facebook apps are able to Cloak the files from the users.

    eg. Several of the Zygna games are completely Flash, presumably a SWF file sitting on their end – but unlike the typical flash game, we can’t simply pull the link to the SWF and save the entire thing to our computer.

    Any suggestion on where I would begin learning how to do this (cloaking the components, not stealing someone else’s)

Leave a Reply

Your email address will not be published. Required fields are marked *