Egret 图片移动至鼠标点击位置

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Fanstasic/article/details/102710114

Egret版本号:5.2.29

//基本显示
module game {
    import Event = egret.Event;
    import ImageLoader = egret.ImageLoader;
    import TextField = egret.TextField;
    import Shape = egret.Shape;
    import Bitmap = egret.Bitmap;
    import BitmapData = egret.BitmapData;

    export class showImage extends egret.DisplayObjectContainer {
        private _txInfo: TextField;
        private _bgInfo: Shape;
        public constructor() {
            super();
            this.once(Event.ADDED_TO_STAGE, this.onAddToStage, this);
        }
        private onAddToStage(evt: Event) {
            var imgLoader: ImageLoader = new ImageLoader;
            imgLoader.once(Event.COMPLETE, this.imgLoadHandler, this);
            imgLoader.load("resource/cartoon-egret_00.png");
        }
        private imgLoadHandler(evt: Event): void {
            var bmd: BitmapData = evt.currentTarget.data;
            let _texture = new egret.Texture();
            _texture.bitmapData = bmd;
            var bird: Bitmap = new Bitmap(_texture);
            bird.x = 100;
            bird.y = 100;
            this.addChild(bird);
            bird.anchorOffsetX = bmd.width / 2;
            bird.anchorOffsetY = bmd.width / 2;
            bird.x = this.stage.stageWidth * .5;
            bird.y = this.stage.stageWidth * .5;

            this._txInfo = new egret.TextField;
            this.addChild(this._txInfo);

            this._txInfo.size = 28;
            this._txInfo.x = 50;
            this._txInfo.y = 50;
            this._txInfo.textAlign = egret.HorizontalAlign.LEFT;
            this._txInfo.textColor = 0x000000;
            this._txInfo.type = egret.TextFieldType.DYNAMIC;
            this._txInfo.lineSpacing = 6;
            this._txInfo.multiline = true;
            this._txInfo.text = "轻触屏幕调整显示对象位置";

            this._bgInfo = new Shape;
            this.addChildAt(this._bgInfo, this.numChildren - 1);
            this._bgInfo.x = this._txInfo.x;
            this._bgInfo.y = this._txInfo.y;
            this._bgInfo.graphics.clear();
            this._bgInfo.graphics.beginFill(0xffffff, .5);
            this._bgInfo.graphics.drawRect(0, 0, this._txInfo.width, this._txInfo.height);
            this._bgInfo.graphics.endFill();

            this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, (evt: egret.TouchEvent) => {
                // bird.x = evt.localX ;
                // bird.y = evt.localY ;
                egret.Tween.get(bird).to({ x: evt.localX, y: evt.localY }, 1000, egret.Ease.sineIn);
            }, this);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Fanstasic/article/details/102710114
今日推荐