1. 封裝一個加載圖片的方法備用
1 class CommonFun{ 2 //加載圖像方法 3 public static creatBitmapByName(name:string){ 4 let result = new egret.Bitmap(); 5 let texture:egret.Texture = RES.getRes(name); 6 result.texture = texture; 7 return result; 8 } 9 }
2.將加載好的圖片添加至顯示舞台中
1 let love:egret.Bitmap = CommonFun.creatBitmapByName("love_png"); 2 this.addChild(love);
3.設置圖片在舞台中居中顯示
1 love.anchorOffsetX = love.width/2;//設置圖片錨點居中 2 love.anchorOffsetY = love.height/2; 3 love.x = this.stage.stageWidth * .5;//設置圖片居中 4 love.y = this.stage.stageHeight * .5;
4.添加改圖片的觸摸點擊事件
1 this.stage.addEventListener(egret.TouchEvent.TOUCH_TAP,(e:egret.TouchEvent)=>{ 2 love.x = e.localX; 3 love.y = e.localY; 4 },this);
5.效果: