Cocos2d-js 实现多触点缩放与旋转

来源:互联网 发布:mac 更改启动项 编辑:程序博客网 时间:2024/06/05 09:53

<span style="font-size:14px;">var listeners = cc.EventListener.create({            event: cc.EventListener.TOUCH_ALL_AT_ONCE,            onTouchesBegan: function (touches, event) {            },            onTouchesMoved: function (touches, event) {                var touch = touches[0];                var target = event.getCurrentTarget();                var delta = touch.getDelta();                if (touches.length == 1) {                    cc.log("move");                    target.x += delta.x;                    target.y += delta.y;                    return;                }                if (touches.length >= 2) {                    cc.log("double");                    var touch1 = touches[1];                    var delta1 = touch1.getDelta();                    var point = touch.getLocation();                    temp = point;                    var point1 = touch1.getLocation();                    //缩放                    var area = (point.x - point1.x);                    var area1 = (point.x - point1.x + delta.x - delta1.x);                      this.sprite.scale = area1/area*this.sprite.scale;                                          //旋转                    var degree = angle(point,point1);                    point.x += delta.x;                    point1.x += delta1.x;                    point.y += delta.y;                    point1.y += delta1.y;                      var degree1 = angle(point,point1);                    var rota = Math.round(degree-degree1)                                         this.sprite.rotation = rota+this.sprite.rotation;                }            }        });          function angle(start,end){            var diff_x = end.x - start.x,                diff_y = end.y - start.y;            //返回角度            return 360*Math.atan(diff_y/diff_x)/(2*Math.PI);        }                  cc.eventManager.addListener(listeners, this.sprite);</span>



源引:http://www.ipastimes.com/post/14.html

0 0