Layabox 常用操作
文本类
新建一个文本内容
var wb =newLaya.Text();wb.text ="hello";wb.color ="#ffffff";// 文本颜色默认为黑色wb.align ="right";// align水平对齐方式wb.valign ="middle"//valign竖直对齐(top,bottom)wb.bgColor ="ff0000";wb.bold =true;// 加粗与否wb.borderColor ="#FF1493";//文本边框颜色wb.font ="Times New Roman";//字体wb.fontSize =10;//文字大小wb.height =20;//文字高度,像素单位wb.italic =false;//斜体与否wb.leading =5;//垂直行间距wb.overflow =Laya.Text.HIDDEN;//HIDDEN:不显示超出文本域的字符VISIBLE:不进行任何裁切//SCORLL:不显示文本域外的字符像素,并且支持scroll接口可以滚动显示Laya.stage.addChild(wb);
textInput 输入
classLayaSample{constructor(){//初始化引擎,不支持WebGL时自动切换到CanvasLaya.init(640,800,Laya.WebGL);//设置画布的背景颜色Laya.stage.bgColor ="#efefef";this.Text_InputSingleline();this.Text_InputMultiline();}privateText_InputSingleline():void{var textInput:Laya.TextInput=newLaya.TextInput("单行输入");//创建一个 TextInput 类的实例对象 textInput 。textInput.wordWrap =true;//设置 textInput 的文本自动换行。textInput.fontSize =30;//设置 textInput 的字体大小。textInput.x =0;//设置 textInput 对象的属性 x 的值,用于控制 textInput 对象的显示位置。textInput.y =0;//设置 textInput 对象的属性 y 的值,用于控制 textInput 对象的显示位置。textInput.width =300;//设置 textInput 的宽度。textInput.height =200;//设置 textInput 的高度。textInput.bgColor ="#c30c30";Laya.stage.addChild(textInput);//将 textInput 添加到显示列表。}privateText_InputMultiline():void{var textInput:Laya.TextInput=newLaya.TextInput("多行输入");//创建一个 TextInput 类的实例对象 textInput 。textInput.fontSize =30;//设置 textInput 的字体大小。textInput.wordWrap =true;//设置 textInput 的文本自动换行。textInput.multiline =true;//设置textInput的多行输入textInput.x =0;//设置 textInput 对象的属性 x 的值,用于控制 textInput 对象的显示位置。textInput.y =300//设置 textInput 对象的属性 y 的值,用于控制 textInput 对象的显示位置。textInput.width =300;//设置 textInput 的宽度。textInput.height =200;//设置 textInput 的高度。textInput.bgColor ="#c30c30";Laya.stage.addChild(textInput);//将 textInput 添加到显示列表。}}newLayaSample();
图片类
显示图片
var img: Laya.Sprite = new Laya.Sprite();定义一个新图片
img.loadImage("path",x,y,width,height,Handler=null)加载它
注意载入位置的时候,默认的路径为bin目录
classMain{constructor(){//初始化引擎Laya.init(1334,750);//设置舞台背景色Laya.stage.bgColor ="#ffffff";var img:Laya.Sprite=newLaya.Sprite();//加载显示图片,坐标位于100,50img.loadImage("res/img/monkey1.png",100,50);//添加到舞台Laya.stage.addChild(img);}}newMain();
切换图片
在显示图片的基础上,清空绘制,然后获得新的图片资源重新绘制
// 程序入口classMain{//需要切换的图片资源路径private monkey1:string="res/img/monkey1.png";private monkey2:string="res/img/monkey2.png";//切换状态private flag:boolean=false;private img:Laya.Sprite;constructor(){//初始化引擎Laya.init(1334,750);//设置舞台背景色Laya.stage.bgColor ="#ffffff";this.img =newLaya.Sprite();//显示绘制的图片this.switchImg();//侦听switchImg中图片区域的点击事件,触发后执行switchImg切换图片this.img.on(Laya.Event.CLICK,this,this.switchImg);//将图片添加到舞台Laya.stage.addChild(this.img);}private switchImg():void{//清空图片this.img.graphics.clear();//获得要切换的图片资源路径var imgUrl:string=(this.flag =!this.flag)?this.monkey1:this.monkey2;//加载显示图片,坐标位于100,50this.img.loadImage(imgUrl,100,50);}}newMain();

浙公网安备 33010602011771号