loginHandler method
The loginHandler method is attached to the CLICK event of the loginBtn. This method is called whenever the loginBtn is clicked by the mouse.
private function loginHandler(e:MouseEvent):void {
This method is responsible for checking the username and password fields for valid data. If one of these fields is empty, the function is exited and a response message is displayed below the password field.
loginMC.responseTxt.text = "";
if(loginMC.username.text == "" || loginMC.password.text == "") {
loginMC.responseTxt.text = "username & password required"; return;
If the username and password fields are found to have acceptable values, those values are stored in the data property, which will be read by the calling script in a custom event handler.
username:loginMC.username.text, password:loginMC.password.text
The last part of this method is to fire off a custom event, alerting the calling script that a username and password have been captured and need to be validated.
dispatchEvent(new Event(LOGIN_ATTEMPT));
This component has been developed to separate the display and security logic, which means the component can be reused because nothing is hardcoded into the actual login logic.
The last real method is a setter, which is used to externally set the response string. This would be called from the loginHandler if the validation failed and you want to show the user so that he or she can attempt another password or username.
public function set responseString(s:String):void {
loginMC.responseTxt.text = s;
The dummyHandler method does not have any functionality. It is used to block any mouse events that occur on the blocking MovieClip. This method is required because an event handler needs a valid function, and if you define an anonymous one, it creates a slow memory leak.
private function dummyHandler(e:MouseEvent):void {
At this point you have completed the LoginWindow class and can move to the next step, which is to create some simple ActionScript to test the component. When that is properly tested you can create the PHP service and make the login component interact with the database to actually look for real users attempting to log in.
Here is the complete LoginWindow class.
package {
import flash.display.MovieClip; import flash.geom.Rectangle; import flash.events.Event; import flash.events.MouseEvent; import flash.net.*;
public class LoginWindow extends MovieClip {
public var data:Object;
private var _visible:Boolean = false; private var container:MovieClip; private var blocker:MovieClip = null;
public static var LOGIN_ATTEMPT:String = "onLoginAttempt";
function LoginWindow() {
container = this;
loginMC.resetBtn.addEventListener(MouseEvent.CLICK, resetHandler);
loginMC.loginBtn.addEventListener(MouseEvent.CLICK, loginHandler);
public function redraw():void {
blocker = new MovieClip();
blocker.alpha = 0.5;
blocker.graphics.beginFill(0x00 00 00);
blocker.graphics.drawRect( 0, 0, stage.stageWidth, stage.stageHeight
blocker.addEventListener( MouseEvent.ROLL_OVER, dummyHandler
blocker.addEventListener( MouseEvent.ROLL_OUT, dummyHandler
blocker.addEventListener( MouseEvent.CLICK, dummyHandler
blocker.width = stage.stageWidth;
blocker.height = stage.stageHeight;
// reverse the offset to position blocker blocker.y = y * -1; blocker.x = x * -1;
addChild(blocker);
container.swapChildren(loginMC, blocker);
loginMC.x = (stage.stageWidth / 2) -(loginMC.width / 2);
public function close():void {
container.parent.removeChild(container);
private function resetHandler(e:MouseEvent):void {
loginMC.username.text = ""; loginMC.password.text = ""; loginMC.responseTxt.text = "";
private function loginHandler(e:MouseEvent):void {
loginMC.responseTxt.text = ""; if(
loginMC.username.text == "" || loginMC.password.text == "")
loginMC.responseTxt.text =
"username & password required"; return;
username:loginMC.username.text, password:loginMC.password.text };
dispatchEvent(new Event(LOGIN_ATTEMPT));
public function set responseString(s:String):void {
loginMC.responseTxt.text = s;
private function dummyHandler(e:MouseEvent):void {
Post a comment