Adding links to Facebook and Twitter in Flash ActionScript 3

Here’s a shortcut that might help some folks out for creating links for status updates to Facebook and Twitter from within your Flash ActionScript 3 projects. For the simplicity of this blog post, I’m explaining this example in relation to buttons you have created in Flash and are residing on the stage:

  • Create your button objects in Flash, drag them to the stage and give them an instance name (in this example, I’m using “btn_twitter” and “btn_facebook”).
  • Add the following actionscript below to post a status message to Twitter.com:

MC_connect.btn_twitter.addEventListener(MouseEvent.CLICK, loadTwitter);
function loadTwitter(e:MouseEvent):void {
navigateToURL(new URLRequest(“http://twitter.com/home?status=Check out Itanji, L.L.C. for your web and rich Internet Application Development Needs! www.itanjillc.com“));
}

  • Add the following ActionScript below to post a status message to Facebook (note: you need to URL-encode u- and t-parameter):

MC_connect.btn_facebook.addEventListener(MouseEvent.CLICK, loadFacebook);
function loadFacebook(e:MouseEvent):void {
var fbURL:String = “http://www.facebook.com/sharer.php?u=” + encodeURIComponent(“http://www.itanjillc.com“) + “&t=” + encodeURIComponent(“Check out Itanji, L.L.C. for your web and rich Internet Application Development Needs! www.itanjillc.com”);
navigateToURL(new URLRequest(fbURL));
}
// The URL to Facebook should look like this:
// http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.itanjillc.com%2F%3Fp%3D82&t=Check+out+Itanji,+L.L.C.+for+your+web+and+rich+Internet+Application+Development+Needs!+www.itanjillc.com

Hope this helps some folks out there implementing Twitter and Facebook links in their Flash/AS3 applications!

3 Responses to “Adding links to Facebook and Twitter in Flash ActionScript 3”

  • harish:

    Hey
    The code you have suggested doesnt post the message from actionscript instead it get the data from the url of the html and post it any idea how to post dynamic score from flash games to facebook

    • jeremyseda:

      You could embed the dynamic score as a variable within your event listener:
      challengeBtn.addEventListener(MouseEvent.CLICK,openFBFriendList);
      function openFBFriendList(event:MouseEvent):void {
      var url:String = "http://apps.facebook.com/your-app-url/invite.html?s="+MovieClip(root).myScore;
      var request:URLRequest = new URLRequest(url);
      try {
      navigateToURL(request, '_top');
      } catch (e:Error) {
      trace("Error occurred!");
      }
      }

Leave a Reply