1. 程式人生 > >點選螢幕對顯示物件進行操作:調整位置/旋轉縮放模式/拖動

點選螢幕對顯示物件進行操作:調整位置/旋轉縮放模式/拖動

1.載入資源

2.將資源新增到顯示列表並設定錨點

////////////////////////////////*調整位置關鍵程式碼*/

this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, (e: egret.TouchEvent) => {

顯示物件.x = e.localX;
顯示物件.y = e.localY;
}, this);

////////////////////////////////*旋轉縮放模式關鍵程式碼*/

private static  STEP_ROT:number=3;

private static STEP_SCALE:number=.03

//用於記錄當前模式,模式切換通過觸控舞臺觸發

private _animMode:number;

private _scaleBase:number;

private launchAnimations(0:void{

this._animMode = AnimaModes.ANIM_ROT;
this.stage.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {

this._animMode = (this._animMode + 1) % 3;
}, this);
this._scaleBase = 0;
//根據當前模式調整旋轉度數或縮放正弦基數形成相應動畫
this.addEventListener(egret.Event.ENTER_FRAME, (e: egret.Event) => {
switch (this._animMode) {
case AnimaModes.ANIM_ROT:
this.顯示物件.rotation += Main.STEP_ROT;
break;
case AnimaModes.AnimM_SCALE: //縮放範圍0.5-1
this.顯示物件.scaleX = this._dt.scaleY = 0.5 + 0.5 * Math.abs(Math.sin(this._scaleBase += Main.STEP_SCALE));
break;
}
this._txtInfo.text = "旋轉角度:" + this.顯示物件.rotation
+ "\n縮放比例:" + this.顯示物件.scaleX.toFixed(2)
+ "\n點選進入" + (["縮放", "靜止", "旋轉"][this._animMode]) + "模式";
return false;//startTick 中回撥返回值表示執行結束是否立即重繪
}, this);
}

}
class AnimaModes {
public static ANIM_ROT: number = 0;
public static AnimM_SCALE: number = 1;
}

/////////////////////////////////*拖動關鍵程式碼*/

 private 顯示物件: egret.Bitmap;

private imgLoadHandler(e: egret.Event): void {
this.顯示物件 = new egret.Bitmap(e.currentTarget.data);
var wHalf: number = this.顯示物件.width / 2;
var hHalf: number = this.顯示物件.height / 2;
this.顯示物件t.anchorOffsetY = wHalf;
this.顯示物件.anchorOffsetY = hHalf;
//隨機初始位置
this.顯示物件.x = wHalf + (this.stage.stageWidth - wHalf * 2) * Math.random();
this.顯示物件.x = hHalf + (this.stage.stageHeight - hHalf * 2) * Math.random();
this.addChild(this._dt);
this.launchMove();
}
private launchMove(): void {
this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchHandler, this);
}
private updateDT(stageX: number, stageY: number): void {
//顯示物件同步手指位置
this.顯示物件.x = stageX;
this.顯示物件.y = stageY;
}
private touchHandler(e: egret.TouchEvent) {
switch (e.type) {
case egret.TouchEvent.TOUCH_MOVE:
this.updateDT(e.stageX, e.stageY);
break;
case egret.TouchEvent.TOUCH_BEGIN:
this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchHandler, this);
this.stage.once(egret.TouchEvent.TOUCH_END, this.touchHandler, this);
this.updateDT(e.stageX, e.stageY);
break;
case egret.TouchEvent.TOUCH_END:
this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchHandler, this);
this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchHandler, this);

break;
}
}

http://developer.egret.com/cn/example/egret2d/index.html#010-disp-basic