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

<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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值