egret 摇杆代码

egret 摇杆代码

        //  实现原理就是 注意 定时器更新的坐标速度一定要保证一致所以我用了相等距离计算的
        //要拖拽的对象
        var draggedObject: egret.Shape;
        var offsetX: number;
        var offsetY: number;
        var chushiX;
        var chushiY;
        var jiaoduX;
        var jiaoduY;
        var pianyijiaoduX;
        var pianyijiaoduY;

        //画一个白色的正方形
        let stageW = this.stage.stageWidth;

        let stageH = this.stage.stageHeight;
        var bg1: egret.Shape = new egret.Shape();
        bg1.graphics.beginFill(0xffffff);
        bg1.graphics.drawRect(0, 0, stageW, stageH);
        bg1.graphics.endFill();
        bg1.x = 0;
        bg1.y = 0;
        bg1.touchEnabled = true;
        this.addChild(bg1);

        //画一个红色的圆
        var circle: egret.Shape = new egret.Shape();
        circle.graphics.beginFill(0xff0000);
        circle.graphics.drawCircle(0, 0, 15);
        circle.graphics.endFill();
        circle.x = 163;
        circle.y = 466;
        this.addChild(circle);


        //黑色半透明圆 园的achor就是中心
        var circle1: egret.Shape = new egret.Shape();
        circle1.graphics.beginFill(0x000000);
        circle1.graphics.drawCircle(0, 0, 65);
        circle1.x = circle.x - 10;
        circle1.y = circle.y - 10;
        circle1.graphics.endFill();
        circle1.alpha = 0.2;
        this.addChild(circle1);

        //画一个蓝色的正方形
        var square: egret.Shape = new egret.Shape();
        square.graphics.beginFill(0x0000ff);
        square.graphics.drawRect(0, 0, 100, 100);
        square.graphics.endFill();
        square.x = 33;
        square.y = 66;
        this.addChild(square);
        var kaishimove = 0;

        //增加圆形的触摸监听
        circle.touchEnabled = true;




        this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, startMove, this);


        function startMove(e: egret.TouchEvent): void {
            chushiX = e.stageX;
            chushiY = e.stageY;
            circle.x = e.stageX;
            circle.y = e.stageY;
            circle1.x = circle.x - 10;
            circle1.y = circle.y - 10;

            //把触摸的对象放在显示列表的顶层
            this.addChild(circle);
            //手指在屏幕上移动,会触发 onMove 方法
            this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, onMove, this);



        }

        this.stage.addEventListener(egret.TouchEvent.TOUCH_END, stopMove, this);

        this.addEventListener(egret.Event.ENTER_FRAME, onEnterFrame, this);

        function onEnterFrame(e: egret.Event) {

            if (kaishimove == 1) {
                console.log(111);
                square.x += pianyijiaoduX;
                square.y += pianyijiaoduY;
            }


        }

        function stopMove(e: egret.TouchEvent) {
            kaishimove = 0;
            console.log(22);
            //手指离开屏幕,移除手指移动的监听
            this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, onMove, this);
        }
        function onMove(e: egret.TouchEvent): void {
            kaishimove = 1;

            circle.x = e.stageX;
            circle.y = e.stageY;
            jiaoduX = e.stageX - chushiX;
            jiaoduY = e.stageY - chushiY;

            var point2: egret.Point = new egret.Point(jiaoduX, jiaoduY);
            point2.normalize(1);
            pianyijiaoduX = point2.x;
            pianyijiaoduY = point2.y;


            console.log('pianyijiaoduX=' + pianyijiaoduX);

            console.log('pianyijiaoduY=' + pianyijiaoduY);
        }

ps: 触摸事件不会向下传递 所以我统一给舞台加了个事件

原创文章如转载,请注明本文链接: http://newmiracle.cn/?p=3137

发表评论

电子邮件地址不会被公开。 必填项已用*标注