AS3初探

2023-01-31 02:01:33 初探 AS3
开发工具:FLEX BUILDER3 PRO

 

语言:AS 3.0

 

操作系统windows XP SP2

 

1.       在flex3 里新建一个AS工程取名为login2
2.       导入所需要的包(如果无法使用fl包,请参照我的前一篇文章:flex3 里如何导入FLASH CS3的包)
   import fl.controls.Button;

    import fl.controls.Label;

    import fl.controls.TextInput;

   

    import flash.display.Sprite;

    import flash.events.MouseEvent;

3.       定义变量(将来在舞台上出现的对象)
       //用户名标签和输入框

       private var username_label:Label;

       private var username_txt:TextInput;

      

       //密码标签和输入框

       private var pwd_label:Label;

       private var pwd_txt:TextInput;

      

       //登录按钮

       private var login_btn:Button;

      

       //提示消息

       private var showmsg_label:Label;

4.    定义函数让以上的对象并添加到在舞台上

   //定义用户名标签函数

       private function setusernamelabel():void

       {

           username_label = new Label();

           username_label.text = "UserName" ;

           username_label.move(10,10);

           this.addChild(username_label);

       }

      

       private function setusernametxt():void

       {

           username_txt = new TextInput();

           username_txt.move(90,10);

           this.addChild(username_txt);

       }

      

       //定义密码标签函数

       private function setpwdlabel():void

       {

           pwd_label = new Label();

           pwd_label.text = "PassWord" ;

           pwd_label.move(10,40);

           this.addChild(pwd_label);

       }

      

       private function setpwdtxt():void

       {

           pwd_txt = new TextInput();

           pwd_txt.displayAsPassword = true ;

           pwd_txt.move(90,40);

           this.addChild(pwd_txt);

       }

      

       //初始化状态信息标签

       private function setshowmsg():void

       {

           showmsg_label = new Label();

           showmsg_label.move(90,100);

           showmsg_label.text = "";

           this.addChild(showmsg_label);

       }

      

       //定义login按钮函数

       private function setButton():void

       {

           login_btn = new Button();

           login_btn.label = "Login" ;

           login_btn.move(90,70);

           login_btn.addEventListener(MouseEvent.CLICK,login_click);

           this.addChild(login_btn);

       }

5.    定义事件函数,这里定义的是鼠标事件

    private function login_click(e:MouseEvent):void

       {

          

           if (username_txt.text != "admin" || pwd_txt.text != "admin")

           {

              showmsg_label.text = "UserName or Password is Error!";

              disableText();

           }

           else

           {

              showmsg_label.text = "Login Success!";

           }

          

       }

6.    在构造函数里初始化这些函数,否则舞台上是不会显示任何东西的,注意在这个函数过程中任何对象的引用都要先初始化,否则会提示无法引用空对象!

 

    private function setshowmsg():void

       {

           showmsg_label = new Label();

           showmsg_label.move(90,100);

           showmsg_label.text = "";

           this.addChild(showmsg_label);

       }

7.    测试

8.    完整代码:

package {

 

    import fl.controls.Button;

    import fl.controls.Label;

    import fl.controls.TextInput;

   

    import flash.display.Sprite;

    import flash.events.MouseEvent;

 

    public class login2 extends Sprite

    {

       //用户名标签和输入框

       private var username_label:Label;

       private var username_txt:TextInput;

      

       //密码标签和输入框

       private var pwd_label:Label;

       private var pwd_txt:TextInput;

      

       //登录按钮

       private var login_btn:Button;

      

       //提示消息

       private var showmsg_label:Label;

      

       public function login2()

       {

           setusernamelabel();

           setusernametxt();

           setpwdlabel();

           setpwdtxt();

           setButton();

           setshowmsg();

          

          

       }

       //定义用户名标签函数

       private function setusernamelabel():void

       {

           username_label = new Label();

           username_label.text = "UserName" ;

           username_label.move(10,10);

           this.addChild(username_label);

       }

      

       private function setusernametxt():void

       {

           username_txt = new TextInput();

           username_txt.move(90,10);

           this.addChild(username_txt);

       }

      

       //定义密码标签函数

       private function setpwdlabel():void

       {

           pwd_label = new Label();

           pwd_label.text = "PassWord" ;

           pwd_label.move(10,40);

           this.addChild(pwd_label);

       }

      

       private function setpwdtxt():void

       {

           pwd_txt = new TextInput();

           pwd_txt.displayAsPassword = true ;

           pwd_txt.move(90,40);

           this.addChild(pwd_txt);

       }

      

       //初始化状态信息标签

       private function setshowmsg():void

       {

           showmsg_label = new Label();

           showmsg_label.move(90,100);

           showmsg_label.text = "";

           this.addChild(showmsg_label);

       }

      

       //定义login按钮函数

       private function setButton():void

       {

           login_btn = new Button();

           login_btn.label = "Login" ;

           login_btn.move(90,70);

           login_btn.addEventListener(MouseEvent.CLICK,login_click);

           this.addChild(login_btn);

       }

       private function login_click(e:MouseEvent):void

       {

          

           if (username_txt.text != "admin" || pwd_txt.text != "admin")

           {

              showmsg_label.text = "UserName or Password is Error!";

              disableText();

           }

           else

           {

              showmsg_label.text = "Login Success!";

           }

          

       }

       private function disableText():void

       {

           username_txt.enabled = false ;

           pwd_txt.enabled = false ;

       }

      

    }

}

 

 

 

 

 

相关文章