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

猜你喜欢

转载自www.cnblogs.com/WentingC/p/9268698.html