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,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,200);
  8. this.addChild(this.layer2,1);

运行结果:

改变layer2的位置:

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

运行结果:

认情况下,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);

运行结果:

改变sprite的位置:

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

运行结果:

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...