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

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);
    }
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值