编程是一种美德,是促使一个人不断向上发展的一种原动力 关于坐标系的用法

 
  
var HelloWorldLayer = cc.Layer.extend({
    sprite:null,
    ctor:function () {
        //////////////////////////////
        // 1. super init first
        this._super();
        this.coordinateSystem();

        /////////////////////////////
        // 2. add a menu item with "X" image, which is clicked to quit the program
        //    you may modify it.
        // ask the window size
        var size = cc.winSize;

        /////////////////////////////
        // 3. add your codes below...
        // add a label shows "Hello World"
        // create and initialize a label
        var helloLabel = new cc.LabelTTF("Hello World", "Arial", 38);
        // position the label on the center of the screen
        helloLabel.x = size.width / 2;
        helloLabel.y = size.height / 2 + 200;
        // add the label as a child to this layer
        this.addChild(helloLabel, 5);

        // add "HelloWorld" splash screen"
        this.sprite = new cc.Sprite(res.HelloWorld_png);
        this.sprite.attr({
            x: size.width / 2,
            y: size.height / 2
        });
        this.addChild(this.sprite, 0);

        return true;
    },
    coordinateSystem:function()
    {
        var spriteA = new cc.Sprite(res.a1);
        spriteA.attr({
            anchorX:0,
            anchorY:0,
            x:100,
            y:0
        })
        this.addChild(spriteA,3);

        var spriteB = new cc.Sprite(res.b1);
        spriteB.attr({
            anchorX:0,
            anchorY:0,
            x:47,
            y:0
        })
        spriteA.addChild(spriteB,3);

        var spriteC = new cc.Sprite(res.c1);
        spriteC.attr({
            anchorX:0,
            anchorY:0,
            x:47,
            y:20
        })
        spriteB.addChild(spriteC,3);

        //将spriteB转换为世界坐标系的坐标
       var worldB = spriteB.convertToWorldSpace(cc.p(0,0))
        cc.log(worldB.x+"/////:"+worldB.y);//输出是  147 ,  0
        //将spriteC转换为世界坐标系的坐标
        //方法1
        var worldC = spriteC.convertToWorldSpace(cc.p(0,0))
        cc.log(worldC.x+"/////////"+worldC.y);

        //方法2
        var temp = spriteB.convertToWorldSpace(spriteC.getPosition())
        cc.log("temp:x"+temp.x+"y"+temp.y);
        //将spriteC在世界坐标系中的坐标转换为在spriteA的坐标。
        var CPositon = spriteA.convertToNodeSpace(temp);
        cc.log("in the spriteA node:spriteC's position:"+
             CPositon.x+"///"+CPositon.y);
        //打印的结果如下:
        /**
         * 147/////:0
         CCDebugger.js:331 194/////////20
         CCDebugger.js:331 temp:x194y20
         CCDebugger.js:331 in the spriteA node:spriteC's position:94///20
         * 
         * **/
    },
});

	var HelloWorldScene = cc.Scene.extend({
          onEnter:function () {
          this._super();
          var layer = new HelloWorldLayer();
          this.addChild(layer);
    }
});

你可能感兴趣的:(Cocos2d-js,Web前端)