egret 示例實戰一:輕觸屏幕調整顯示對象位置


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.效果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM