附加脚本的使用

作者:追风剑情 发布于:2019-9-20 17:38 分类:LayaAir

一、新建MyDialog.scene

3333.png

拖一个登录界面

3333.png

给需要在代码中访问的组件取个名字

3333.png

二、新建MyDialog.ts

3333.png

整个工程结构

3333.png

MyDialog.ts

  1. export default class MyDialog extends Laya.Script {
  2. // 这部分为自动生成的,这些字段可显示在属性面板中
  3. /** @prop {name:intType, tips:"整数类型示例", type:Int, default:1000}*/
  4. public intType: number = 1000;
  5. /** @prop {name:numType, tips:"数字类型示例", type:Number, default:1000}*/
  6. public numType: number = 1000;
  7. /** @prop {name:strType, tips:"字符串类型示例", type:String, default:"hello laya"}*/
  8. public strType: string = "hello laya";
  9. /** @prop {name:boolType, tips:"布尔类型示例", type:Bool, default:true}*/
  10. public boolType: boolean = true;
  11. // 更多参数说明请访问(附加脚本的使用): http://ldc.layabox.com/doc/?nav=zh-ts-2-4-2
  12.  
  13. private accountInput: Laya.TextInput;
  14. private passwordInput: Laya.TextInput;
  15. private btnLogin: Laya.Button;
  16. constructor() { super(); }
  17. onEnable(): void {
  18. }
  19.  
  20. onDisable(): void {
  21. }
  22.  
  23. /**
  24. * 此属性在运行时被自动调用
  25. * 设置owner函数,可以直接获取到添加附加脚本的组件实例
  26. **/
  27. public set owner(value: any)
  28. {
  29. var dialog: Dialog = value;
  30. if (dialog == null)
  31. return;
  32.  
  33. //获取组件
  34. this.accountInput = dialog.getChildByName("accountInput") as Laya.TextInput;
  35. this.passwordInput = dialog.getChildByName("passwordInput") as Laya.TextInput;
  36. this.btnLogin = dialog.getChildByName("btnLogin") as Laya.Button;
  37.  
  38. //按钮注册Click事件处理器
  39. this.btnLogin.clickHandler = new Laya.Handler(this, this.onClickLogin);
  40. }
  41.  
  42. private onClickLogin(): void
  43. {
  44. var account: string = this.accountInput.text;
  45. var password: string = this.passwordInput.text;
  46. console.debug("*************** onClickLogin() account=%s, password=%s", account, password);
  47. }
  48. }

三、把MyDialog.ts脚本拖到MyDialog.scene的属性面板上

3333.png

四、在Chrome中运行测试

11111.png

点登录按钮,打印出日志

3333.png

标签: LayaAir

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号