Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Actionscript 3, Facebook and Flash.

This post contains a script made combining Official Facebook Actionscript API released! with Understanding Flash button component.

package {
	import flash.display.Sprite;
	import fl.controls.Button;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import com.facebook.data.users.GetInfoData;
	import com.facebook.utils.FacebookSessionUtil;
	import com.facebook.data.users.FacebookUser;
	import com.facebook.data.users.GetInfoFieldValues;
	import com.facebook.commands.users.GetInfo;
	import com.facebook.net.FacebookCall;
	import com.facebook.events.FacebookEvent;
	import com.facebook.Facebook;
	public class as3facebook extends Sprite {
		var my_text:TextField = new TextField();
		var fbook:Facebook;
		var session:FacebookSessionUtil;
		var user:FacebookUser;
		public var button_labels:Array = ["Click here to login into Facebook","Click here once you logged in into Facebook"];
		var my_button:Button;
		public function as3facebook() {
			addChild(my_text);
			my_text.width=460;
			my_text.x=20;
			my_text.y=100;
			my_text.text = "Facebook API test";
			for (var i:int=0; i<2; i++) {
				my_button=new Button();
				addChild(my_button);
				my_button.label=button_labels[i];
				my_button.setSize(250,30);
				my_button.move(125, i*40+10);
				my_button.addEventListener(MouseEvent.CLICK, clickHandler);
			}
			session=new FacebookSessionUtil("83ccead57ece4086338470a75e13c5b1","1fff6abf608e90e70e1aad07798aa8e4",loaderInfo);
			session.addEventListener(FacebookEvent.CONNECT,onConnect);
			fbook=session.facebook;
		}
		function clickHandler(event:MouseEvent):void {
			switch (event.currentTarget.label) {
				case "Click here to login into Facebook" :
					onLogin();
					break;
				case "Click here once you logged in into Facebook" :
					onConfirmLogin();
					break;
			}
		}
		private function onLogin():void {
			session.login();
		}
		private function onConfirmLogin():void {
			session.validateLogin();
		}
		private function onConnect(e:FacebookEvent):void {
			var call:FacebookCall=fbook.post(new GetInfo([fbook.uid],[GetInfoFieldValues.ALL_VALUES]));
			call.addEventListener(FacebookEvent.COMPLETE,onGetInfo);
		}
		private function onGetInfo(e:FacebookEvent):void {
			user=(e.data as GetInfoData).userCollection.getItemAt(0) as FacebookUser;
			my_text.text="Hello "+user.first_name+" "+user.last_name+" born on "+user.birthday;
		}

	}
}

And this is the result:

As in the Flex example, you should click the 2nd button (the one saying “Click here once…”) only when you get “You may now close this window and return to the application.” message on your FB page.

I am really excited for this new API and I can't wait to include it in a game.

Download the source code, with all required libraries

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.