Creator与Laya的Ts和JsTS的版属性声明详解,属性详解,Enum枚举,dataSource组件综合赋值,box容器复杂赋值[子对象的属性名的geChildByName],clip切片动画

本文详细讲解了LayaAlir中TypeScript(TS)和JavaScript(JS)的属性声明,包括类型声明、默认值、获取属性的值等方面。同时介绍了Creator中如何使用属性声明、枚举类型,以及外部调用属性的方法。通过代码示例展示了如何在TS和JS中声明和使用属性,包括枚举类型在属性面板的展示和使用。
摘要由CSDN通过智能技术生成

//************************ LayaAlir *******************************//

@property(cc.Node) 是装饰器类,详情请看本人 装饰器的章节 https://editor.csdn.net/md/?articleId=108006200

TypeScrip官网 API https://www.tslang.cn/docs/home.html

//************************//1LayaAlir Ts获取属性的值***************************//

constructor() {
super();
/** @prop {name:intType, tips:“整数类型示例”, type:Int, default:1000}/
public intType: number = 1000;
/** @prop {name:numType, tips:“数字类型示例”, type:Number, default:1000}
/
public numType: number = 1000;
/** @prop {name:strType, tips:“字符串类型示例”, type:String, default:“hello laya”}/
public strType: string = “hello laya”;
/** @prop {name:boolType, tips:“布尔类型示例”, type:Bool, default:true}
/
public boolType: boolean = true; //比 Js多了个 ":boolean " 和 “= true,赋值”,众所周知,Ts声明东西,必须要指定类型和赋值

}

//************************//1LayaAlir Js获取属性的值 ***************************//

/** @prop {name:intType, tips:“整数类型示例”, type:Int, default:1000}/
let intType = 1000;
/** @prop {name:numType, tips:“数字类型示例”, type:Number, default:1000}
/
let numType = 1000;
/** @prop {name:strType, tips:“字符串类型示例”, type:String, default:“hello laya”}/
let strType = “hello laya”;
/** @prop {name:boolType, tips:“布尔类型示例”, type:Bool, default:true}
/
let boolType = true;
// 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0
/** @prop {name:txt_timer, tips:“倒计时”, type:Node, default:null}/
this.txt_timer = null; //声明成全局的,以便在以下的其他方法中直接使用访问 /比 Ts少了个 ":boolean " ”,众所周知,Ts声明东西,必须要指定类型和赋值
/** @prop {name:txt_Score, tips:“分数”, type:Node, default:null}
/
this.txt_Score = null;
/** @prop {name:GameOverPanel, tips:“游戏结束按钮”, type:Node, default:null}*/
this.GameOver = null;

//********************************** 1)LayaAlir 的Js 【文件模式】 直接获取属性property来使用场景中的属性 ********//
//
1)_1 【文件模式】LayaAlir 的Js 直接获取属性property来使用场景中的属性 ***************************//
startGame():void{
if(!GameMain.gameView){ //文件模式没有layaMAxUI.ts类,只有这一种加载方式
GameMain.gameView = new GameView(); //这里New的时候Ts会自动导入 import GameView from "./…/ " 这个类,没有的话,就自己手动加上
}
onAwake(){
GameMain.gameView.startBtn.on(Laya.Event.MOUSE_DOWN, this, () => {
AudioMgr.Ins.PlayAudio(‘click’);
})
}
}
}

//********************************** 1)LayaAlir 的Js 【分离模式】 直接获取属性property来使用场景中的属性 ********//
//
1)_1 【分离模式】LayaAlir 的Js 直接获取属性property来使用场景中的属性 *****//
startGame():void{
if(!GameMain.gameView){
GameMain.gameView = new GameView(); 这里New的时候Ts会自动导入 import GameView from "./…/ " 这个类,没有的话,就自己手动加上
}
onAwake(){
GameMain.gameView.startBtn.on(Laya.Event.MOUSE_DOWN, this, () => {
AudioMgr.Ins.PlayAudio(‘click’);
})
}
}
}
//
1)_2 【分离模式】LayaAlir 的Js 直接获取属性property来使用场景中的属性 ************************//
把脚本 作为组件,拖到场景上,不要拖拽到runtime里面
然后 /
@prop {name:txt_timer, tips:“倒计时”, type:Node, default:null}
/
this.txt_timer = null;
//声明成全局的,以便在以下的其他方法中直接使用访问 /比 Ts少了个 ":boolean " ”
//把相应的场景中的组件,推拽到象形的声明的属性的中

//************************* 加载 【 U3D 】 场景类 .ls 具体方法查找我的【LayaU3D场景的 .ls场景】这篇博文查看 *************************//
Laya.Scene3D.load(“res/scene/LayaScene_Main/Conventional/Main.ls”,Laya.Handler.create(this,this.onLoadSceneComplete))
//Unity导出的 .ls场景的加载,并传递给相应的节点以组件类,并给这个组件类的Init()方法传递参数
onLoadSceneComplete(loadScene){
loadScene.zOrder = -1;
loadScene.getChildByName(“Hummer”).addComponent(HummerCtrl).Init(camera,loadScene,effectPrefab);
}

//****************************************** 2) 【内嵌模式】 2中获取场景 和场景中属性的方法 ***************//
//
2)_1 【内嵌模式】 2中获取场景 和场景中属性的方法 *************************************//
export default class MainView extends ui.MainViewUI {
constructor(t) {
super(t);
this.startBtn.on(Laya.Event.MOUSE_DOWN, this, () => { //继承自父类ui.MainViewUI以后,直接可以使用父类中的节点属性
AudioMgr.Ins.PlayAudio(‘click’);
})
}

//******************************* 2)_2 【内嵌模式】 2中获取场景 和场景中属性的方法 *************************************//
export default class BaseView

}
export default class MainView extends BaseView<ui.MainViewUI> {
constructor(t) {
super(t);
}
isTest: boolean = false;
private startClickTime: number = 0;
init() {
this.scene = new ui.MainViewUI; //new 这个 layaMaxUI.ts 中生成的 ui.MainViewUI 的 REG注册的场景
this.scene.startBtn.on(Laya.Event.MOUSE_DOWN, this, () => { //this.scene.
AudioMgr.Ins.PlayAudio(‘click’);
})
//在GameMain类里面,把所有的场景都放里面,同一随时调用

class GameMain(){
public static gameStart:GameSatrt; //把场景做成全局变量
public static gameView:GameView;
public static gameOver:GameOver;

public static isShow:boolean;  //全局变量
constructor(){
	var resArray:Array<any> = [
			{url:"res/atlas/ui.json",type:Laya.Loader.ATLAS}, //只有大图,不到出的时候,才这样加载
			{url:"res/back.png",type:Laya.Loader.IMAGE},
			{url:"res/help.png",type:Laya.Loader.IMAGE},
	   	    {url : "skeleton/loadsk.sk", type : Laya.Loader.BUFFER},    
	 	  {url : "image/start/txt-notice.png", type : Laya.Loader.IMAGE},
		  {url : "sound/bgm.mp3", type : Laya.Loader.SOUND}  
	];,
	Laya.loader.load(resArray,Laya.Handler.creator(this,this.onLoaded));
}
onLoaded();Void{
	
}

}

//实例化moles的时候,可以传递constructor(参数)
class Moles{
private isShow : boolean;
private normalState : Laya.Image;
private hitState : Laya.Image;
private downY : number;
constructor( //接收到了GameView在实例化Mole的时候new mols(参数)传递过来的参数
normalState:Laya.Image,
hitState:Laya.Image,
scoreImg:Laya.Image,
downY:number,
hitCallBackHd:Laya.Handler)
{
this.normalState = normalState;
this.hitState = hitState ;
this.downY = downY ; //赋值哥全局的this.downY,就可以在这个类里面随便哪一个地方使用了
}
}

//GameView 实例化moles 并 传递给 constructor(参数)
class GameView{
private moles: Array;
private moleNum: number = 9;
private score: number ;

constructor(normalState:Laya.Image,hitState:Laya.Image,downY:number){
	var hitCallBackHd:Laya.Handler = Laya.Handler.create(this,this.setScore,null,false);
	  						//声明事件传递过去,也可以用
	  						Laya.stage.on("addScore",this.AddScore)   
	  						 Laya.stage.event("addScore")   
	  						 this.owner.on("addScore",this.AddScore),   
	  						 this.owner.event("addScore")替代
	for(var i:number=0;i<this.moleNum;i++){
			var box:Laya.Box = this.getChildByName("item"+i) as Laya.Box;
			var mole = new Mole(
				box.getChildByName("normal") as Laya.Image,
				box.getChildByName("hit") as 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值