前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >AS3初探

AS3初探

作者头像
py3study
发布2020-01-09 11:15:40
6030
发布2020-01-09 11:15:40
举报
文章被收录于专栏:python3

开发工具: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 ;

}

}

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档