您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-html5中的坐标系统

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

首先测试Layer:

// 获取尺寸

this.winSize = cc.Director.getInstance().getWinSize();

// 灰色层

this.layer1 = cc.LayerColor.create(cc.c4(200, 200, 200, 255), this.winSize.width, this.winSize.height);

this.addChild(this.layer1, 0);

// 绿色层

this.layer2 = cc.LayerColor.create(cc.c4(0, 255, 0, 255), 200, 200);

this.addChild(this.layer2, 1);

运行结果:



改变layer2的位置:

this.layer2.setPosition(200, 200);

运行结果:



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

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

测试Sprite:

// 添加精灵

this.sprite = cc.Sprite.create("res/bear.png");

this.addChild(this.sprite);

运行结果:



改变sprite的位置:

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

运行结果:

#cyg { font: 14px "微软雅黑"; line-height: 1.6em; } #cyg h2 { margin: 1.33em 0 1em 0; font-size: 16px; } #cyg h3 { margin: 1.33em 0 1em 0; font-size: 14px; } #cyg p { margin: 1em 0; } #cyg_code { max-height: 320px; overflow-y: hidden; margin: 1em 0; padding-left: 40px; border: 2px solid rgb(200, 200, 200); background: rgb(231, 229, 220); font: 12px courier,arial,sans-serif; list-style: decimal; } #cyg_code li { min-height: 20px; line-height: 20px; padding: 0 1em; border-left: 3px solid rgb(108, 226, 108); background: rgb(240, 240, 240); white-space: pre; } #cyg_code li:nth-child(even) { background: rgb(230, 230, 230); } #cyg_code em { font-style: normal; color: rgb(117, 113, 94); } #cyg_code strong { font-weight: normal; color: rgb(153, 129, 255); } #cyg_code b { font-weight: normal; color: rgb(166, 226, 46); } #cyg_code cite { font-style: normal; color: rgb(198, 93, 8); } #cyg_code span { color: rgb(249, 38, 76); } #cyg_code q { color: rgb(102, 217, 239); }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: