第一步往需要添加脚本的对象属性中的runtime属性中填写需要添加的包名加类名,并保存发布(保存Ctrl+s,发布Ctrl+F12)
第二步创建脚本,目录如下图
第三步填写代码
/**
* name
*/
module game{
export class imageRunTime extends Laya.Image{
public scaleTime:number = 100;
constructor(){
super();
this.anchorX= this.anchorY = 0.5;
this.on(Laya.Event.MOUSE_DOWN,this,this.scaleSmal);
this.on(Laya.Event.MOUSE_UP,this,this.scaleBig);
//this.on(Laya.Event.MOUSE_OUT,this,this.scaleSmal);
}
private scaleSmal():void{
Laya.Tween.to(this,{scaleX:0.8,scaleY:0.8},this.scaleTime);
}
private scaleBig():void{
Laya.Tween.to(this,{scaleX:1,scaleY:1},this.scaleTime);
}
}
}
第四部创建UI视图
import View=laya.ui.View;
import Dialog=laya.ui.Dialog;
module ui.Scenes.test {
export class TestSceneUI extends View {
public static uiView:any ={"type":"View","props":{"y":0,"x":0,"width":1920,"height":640},"child":[{"type":"Image","props":{"y":0,"x":0,"skin":"image/BG.jpg","cacheAs":"bitmap","alpha":1}},{"type":"Image","props":{"y":285,"x":1144,"skin":"image/hero2.png","runtime":"game.imageRunTime"}},{"type":"Image","props":{"y":285,"x":910,"visible":true,"skin":"image/hero1.png","runtime":"game.imageRunTime"}},{"type":"Image","props":{"y":283,"x":726,"skin":"image/hero0.png","runtime":"game.imageRunTime"}},{"type":"Image","props":{"y":289,"x":1031,"skin":"image/hero3.png","runtime":"game.imageRunTime"}}]};
constructor(){ super()}
createChildren():void {
View.regComponent("game.imageRunTime",game.imageRunTime);
super.createChildren();
this.createView(ui.Scenes.test.TestSceneUI.uiView);
}
}
}
第五步程序入口处预加载资源
import WebGL = Laya.WebGL;
// 程序入口
class GameMain{
constructor()
{
Laya.init(1920,640, WebGL);//image.atlas
Laya.loader.load("res/atlas/image.atlas",Laya.Handler.create(this,this.onload));
}
private onload():void{
var bgUipage:ui.Scenes.test.TestSceneUI = new ui.Scenes.test.TestSceneUI();
//bgUipage.graphics.drawRect(0,0,300,300,"#ff00ff");
Laya.stage.addChild(bgUipage);
}
}
new GameMain();