Skip to content

Instantly share code, notes, and snippets.

@trekdemo
Created June 27, 2012 08:30
Show Gist options
  • Select an option

  • Save trekdemo/3002441 to your computer and use it in GitHub Desktop.

Select an option

Save trekdemo/3002441 to your computer and use it in GitHub Desktop.
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.external.ExternalInterface;
public class Main extends Sprite {
private var tf:TextField;
private var button:Sprite;
public function Main(){
this.addEventListener( Event.ADDED_TO_STAGE, onAddedToStage )
}
private function onAddedToStage( e:Event ):void {
this.removeEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
initLabel()
initButton();
// NOTICE: This call is necessary to get user info
initSession();
}
/**
* This method is responsible for initializing the communication between
* site <=> flash communication
*/
private function initSession():void {
// This line exposes the hansleLoginStatus function to javascript
flash.external.ExternalInterface.addCallback( 'handleLoginStatus', handleLoginStatus );
// This line calls the initializer js function
// this will notify us about the flash load status
flash.external.ExternalInterface.call( 'SofaChorus.getLoginStatus' );
}
/**
* This method starts the user login process
*/
private function loginThroughJS(e:Event = null):void {
// ExternalInterface.call('SofaChorus.login');
flash.external.ExternalInterface.call('window.SofaChorus.login');
}
/**
* After the site has done with the login process this method will be
* called
* @param data this object contains information about the logged in user
* loggedIn (Boolean)
* uid (String)
* name (String)
* image_url (String)
*/
private function handleLoginStatus(data:Object):void {
tf.appendText('!');
if( data['loggedIn'] ) {
tf.text = 'User logged in ' + data['uid'];
button.visible = false;
} else {
tf.text = 'User is not logged in';
button.visible = true;
button.addEventListener( MouseEvent.CLICK, loginThroughJS );
}
}
private function initLabel():void {
tf = new TextField();
tf.width = 400;
tf.height = 30;
tf.background = true;
tf.border = true;
tf.text = 'Indul a mandula';
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0x000000;
format.size = 10;
format.underline = false;
tf.defaultTextFormat = format;
addChild(tf);
}
private function initButton():void {
button = new Sprite();
button.graphics.beginFill( 0xaa0000 );
button.graphics.drawRect( 0, 0, 100, 40 );
button.graphics.endFill();
button.y = 30;
button.buttonMode = true;
addChild( button );
}
}
}
// vim: ai et st=2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment