向量的练习

例子1:

//用定时器生成一圈线

var dn = new cc.DrawNode();
this.addChild(dn);
var a = cc.p(cc.winSize.width>>1,cc.winSize.height>>1);    //获取到地图中心点
var b = cc.pAdd(a,cc.p(200,0));                //获取中心点X轴偏移200距离的点
dn.drawSegment(a,b,1,cc.color(255,0,0,255));   //drawSegment 画线段API        画出一条a点到b点颜色为红色的线
var num = 0;
setInterval(function(){
    var c = cc.pRotateByAngle(b, a, cc.degreesToRadians(num++));    //pRotateByAngle:按角度旋转        degreesToRadians:角度转弧度
    dn.drawSegment(a,c,1,cc.color(255,0,0,255))
},50);


例子2:

//点击屏幕,画一条线
    this.dn = new cc.DrawNode();
    this.addChild(this.dn);
    var that = this;
    var listener = cc.EventListener.create({
        event:cc.EventListener.TOUCH_ONE_BY_ONE,
        swallowTouches: true,
        onTouchBegan: function (touch, event) {
            that.drawLine(touch.getLocation());   //把点击屏幕获取的全局坐标传入函数
        }
    });
    cc.eventManager.addListener(listener, this);
},
drawLine:function(p){
    var a = cc.p(cc.winSize.width>>1, cc.winSize.height>>1);
    var b = p;
    //var c = p
    var c = cc.pNormalize(cc.pSub(b, a));     //cc.pNormalize:向量标准化      cc.pSub:减法
    c = cc.pMult(c, 200);                     //cc.pMult:乘法
    c = cc.pAdd(a, c);                        //cc.pAdd:加法
    this.dn.drawSegment(a,c,1,cc.color(255,0,0,255))
}

你可能感兴趣的:(向量的练习)