生活随笔
收集整理的這篇文章主要介紹了
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,50 img.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();
轉載于:https://www.cnblogs.com/mrfri/p/8727163.html
總結
以上是生活随笔為你收集整理的Layabox 常用操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。