cocos2d html5 互相推挤的精灵

前端之家收集整理的这篇文章主要介绍了cocos2d html5 互相推挤的精灵前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 update:function ()
    {
        if(1)
        {
            for (var j = 0; j < this._paddles.length; j++) {
                var _obj1 = this._paddles[j];
                if (!_obj1)
                    break;

                for (var i = j+1; i < this._paddles.length; i++) {
                    var _obj2 = this._paddles[i];
                    if (!_obj2)
                        break;

                    var _lenth = this.calculateLenth(_obj1.getPosition(),_obj2.getPosition());

                    var _minLenth = 40*2;
                    if(_minLenth>_lenth)
                    {
                        var _centerPoint = cc.p((_obj1.x + _obj2.x)/2,(_obj1.y + _obj2.y)/2);
                        var _moveLenth = (_lenth - _minLenth) /2;

                        var _randian = this.calculateRadian(_obj1.getPosition(),_obj2.getPosition());
                        cc.log(_randian);
                        var _offset = cc.p(Math.sin(_randian)*_moveLenth,Math.cos(_randian)*_moveLenth);

                        _obj1.runAction( cc.moveBy(0.1,cc.p(_offset.x,_offset.y)));
                        _obj2.runAction( cc.moveBy(0.1,cc.p(-_offset.x,-_offset.y)));
                    }
                }
            }
        }

    },calculateLenth:function(p1,p2)
    {

        var _offsetX = p1.x - p2.x;
        var _offsetY = p1.y - p2.y;
        var _lenth = Math.sqrt(_offsetX*_offsetX + _offsetY*_offsetY);

        return _lenth;
    },calculateRadian:function(p1,p2)
    {
        var _offsetX = p1.x - p2.x;
        var _offsetY = p1.y - p2.y;
        var _radian = Math.atan(_offsetX/ _offsetY);
        if(p1.y > p2.y)
        {
            _radian +=Math.PI;
        }

        return _radian;
    }

猜你在找的Cocos2d-x相关文章