Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

Creating a Flash Facebook application with the Facebook Actionscript API

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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (41 votes, average: 4.20 out of 5)
Loading ... Loading ...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 37 comments

  1. Paul

    on November 4, 2009 at 11:43 am

    Is’nt this a security risk when you put the secret key in the swf?

  2. Yarden Refaeli

    on November 4, 2009 at 12:50 pm

    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 ;)

  3. FlashGameMaker

    on November 4, 2009 at 4:18 pm

    Did you use Flex or the Flash IDE?

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

    on November 4, 2009 at 6:07 pm

    [...] more here: Creating a Flash Facebook application with the Facebook … No Comments Read [...]

  5. Down the Foxhole – News Flash

    on November 4, 2009 at 10:39 pm

    [...] Self-professed Italian geek and programmer Emanuele Feronato provides a useful tutorial on how to create a Flash applicaiton for Facebook using the Facebook ActionScript API. Read more [...]

  6. Paul

    on November 5, 2009 at 2:50 pm

    @Yarden Refaeli: Yeah thats what I thought and why I’am asking. :)
    How do you Auth the app in facebook? WIth php or with JavaScript

  7. brew

    on November 6, 2009 at 8:02 am

    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!

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

    on November 10, 2009 at 7:14 am

    [...] Creating a Flash Facebook application with the Facebook Actionscript API [...]

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

    on November 11, 2009 at 7:16 pm

    [...] time to make some considerations on the Facebook application made with the Facebook Actionscript API I blogged some days [...]

  10. ??Facebook??????????? | God is not home today

    on November 15, 2009 at 9:08 am

    [...] Emanuele Feronato? http://www.emanueleferonato.com/2009/11/04/creating-a-flash-facebook-application-with-the-facebook-a… [...]

  11. Emanuele Feronato

    on November 17, 2009 at 3:30 am

    [...] Some days ago I showed you how to create a Flash Facebook application using the AS3 API. [...]

  12. Muse

    on November 24, 2009 at 2:27 pm

    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..

  13. robi2216

    on November 24, 2009 at 7:01 pm

    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

  14. Manish

    on December 14, 2009 at 1:13 pm

    Hi nice tutorial,

    i am trying to use share element to share swf from Publish data.
    can you help me to that.

    thanks

  15. Shawn Keim

    on December 14, 2009 at 11:40 pm

    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

  16. Markham Butler

    on December 16, 2009 at 5:38 pm

    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?

  17. Fahim Akhter

    on December 19, 2009 at 1:36 pm

    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

  18. MoMo-Studio Development Blog » Blog Archive » Zetta Monster Plus….Facebook?

    on January 22, 2010 at 9:36 am

    [...] http://www.emanueleferonato.com/2009/11/04/creating-a-flash-facebook-application-with-the-facebook-a… [...]

  19. kanyal

    on January 24, 2010 at 12:08 pm

    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

  20. gujarathi

    on March 10, 2010 at 6:53 am

    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();
    }

    }

  21. EZ

    on March 13, 2010 at 12:38 am

    Encountered a problem:
    My Flash movie is not loaded from my server (“Movie not loaded…”).

    Trying to debug the problem I’ve added Emanuele’s SWF to mine:

    The result:
    The SWF from http://www.gamemummy.com loads just find
    The SWF from http://www.microsheep.com does not load

    Permission problems?
    Any ideas?

    P.S
    Trying to reach this swf from the browser works just fine:
    http://www.microsheep.com/facebook_test/facebook_demo.swf

  22. xoxo

    on May 26, 2010 at 10:00 am

    hi could you please create a tutorial about loading friends that uses the application in facebook, take a look at this link: http://www.qweas.com/downloads/games/other/scr-farmville-tools.JPG

    below the game you could see those people who is using the application. then it is limit to 8 friends to be viewed

    Been trying to google around about this, and basically i really don’t have any idea how to do this one. :D

  23. sankar

    on May 29, 2010 at 12:45 pm

    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.

  24. sankar

    on May 29, 2010 at 12:48 pm

    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

  25. dodi

    on June 23, 2010 at 7:19 am

    nice tutorial, this very helpful.. i want to integrete my game to facebook

  26. karunakar

    on June 25, 2010 at 8:30 am

    nice tutorial. Helped me a lot……..Thank u very much

  27. pissed of

    on September 27, 2010 at 11:04 pm

    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

  28. sankar

    on December 9, 2010 at 3:30 pm

    Hi friend i need code.Can u send me please.
    i am willing to create app for facebook
    IT WOULD BE GREAT HELP FOR ME
    sankar.niit@yahoo.co.in is my id

  29. Seng Keat

    on February 18, 2011 at 6:26 am

    I am newbies to Flash and I used CS4
    I want to build flash app for facebook game
    i searched it and just found out in ur side

    A great help -
    Keat

  30. Karthik

    on April 21, 2011 at 1:46 pm

    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

  31. mostafa

    on June 10, 2011 at 10:28 pm

    I want to get Facebook user id in action script ? could you provide me with a sample ?!

    Thanks a lot.

  32. Jaz

    on June 24, 2012 at 6:20 pm

    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 ^__^

  33. Nenoufar

    on July 21, 2012 at 3:27 pm

    The flash demo page does not work anymore. Any other link where we could see it in action?

  34. Technological Specifications | mmmmppp

    on August 8, 2012 at 5:42 pm

    [...] Facebook Posting from Flash Tutorial [...]

  35. Prospectus | mmmmppp

    on August 16, 2012 at 6:01 am

    [...] Facebook Posting from Flash Tutorial [...]

  36. Luzmar

    on October 10, 2012 at 4:06 am

    hello!!! im trying to do a flash website (facebook) i would like to know if any of you can help me with that

  37. Davai

    on August 6, 2013 at 7:52 pm

    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)

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×