Cocos2d-html5中的坐标系统

在cocos2d-html5中,Scene和Layer的默认锚点是(left,bottom),而其它节点(Node)的默认锚点是(center,center)。无论是Layer还是其它节点,其坐标原点都是父节点的左下角,即(left,bottom)。只是有一个例外,cc.MenuItem是以cc.Menu的锚点为坐标原点。

首先测试Layer:

  1. // 获取尺寸
  2. this.winSize = cc.Director.getInstance().getWinSize();
  3. // 灰色层
  4. this.layer1 = cc.LayerColor.create(cc.c4(200, 200, 200, 255), this.winSize.width, this.winSize.height);
  5. this.addChild(this.layer1, 0);
  6. // 绿色层
  7. this.layer2 = cc.LayerColor.create(cc.c4(0, 255, 0, 255), 200, 200);
  8. this.addChild(this.layer2, 1);

运行结果:

Cocos2d-html5中的坐标系统 - 鬼眼邪神 - 鬼眼邪神

改变layer2的位置:

  1. this.layer2.setPosition(200, 200);

运行结果:

Cocos2d-html5中的坐标系统 - 鬼眼邪神 - 鬼眼邪神

默认情况下,Scene和Layer是不能设置锚点的,要想设置锚点可以使用方法ignoreAnchorPointForPosition(newValue)。

参数newValue
false:锚点可设置
true:锚点被强制设置为(0, 0),无法改变

测试Sprite:

  1. // 添加精灵
  2. this.sprite = cc.Sprite.create("res/bear.png");
  3. this.addChild(this.sprite);

运行结果:

Cocos2d-html5中的坐标系统 - 鬼眼邪神 - 鬼眼邪神
 

改变sprite的位置:

  1. this.sprite.setPosition(cc.p(100, 100));

运行结果:

Cocos2d-html5中的坐标系统 - 鬼眼邪神 - 鬼眼邪神
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值