赞
踩
Cocos2d-x坐标系和OpenGL坐标系相同,都是起源于笛卡尔坐标系。
笛卡尔坐标系中规定右手系原点在左下角,x向右,y向上,z向外。如图所示
- auto red = LayerColor::create(Color4B(255, 100, 100, 128), visibleSize.width/2, visibleSize.height/2);
-
- auto green = LayerColor::create(Color4B(100, 255, 100, 128), visibleSize.width/4, visibleSize.height/4);
-
- red->addChild(green);
-
- this->addChild(red, 0);
- auto red = LayerColor::create(Color4B(255, 100, 100, 128), visibleSize.width/2, visibleSize.height/2);
- red->ignoreAnchorPointForPosition(false);
- red->setAnchorPoint(Point(0.5, 0.5));
- red->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
-
- auto green = LayerColor::create(Color4B(100, 255, 100, 128), visibleSize.width/4, visibleSize.height/4);
- green->ignoreAnchorPointForPosition(false);
- green->setAnchorPoint(Point(1, 1));
- red->addChild(green);
-
- this->addChild(red, 0);
- auto red = LayerColor::create(Color4B(255, 100, 100, 128), visibleSize.width/2, visibleSize.height/2);
- red->ignoreAnchorPointForPosition(true);
- red->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
-
- auto green = LayerColor::create(Color4B(100, 255, 100, 128), visibleSize.width/4, visibleSize.height/4);
- green->ignoreAnchorPointForPosition(true);
-
- red->addChild(green);
-
- this->addChild(red, 0);
<span style="font-size:14px;">Sets the 'z' coordinate in the position. It is the OpenGL Z vertex value.</span>
即PositionZ的值即为OpenGL的z值VertextZ。同样节点的PositionZ也是决定了该节点的渲染顺序,值越大,但是与zOrder不同的区别在于,PositionZ是全局渲染顺序,即在根节点的渲染顺序上的。而zOrder则是局部渲染顺序,即在该节点的父节点上的渲染顺序,与Node的层级有关。
- auto red = LayerColor::create(Color4B(255, 100, 100, 255), visibleSize.width/2, visibleSize.height/2);
- red->ignoreAnchorPointForPosition(false);
- red->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
-
- auto green = LayerColor::create(Color4B(100, 255, 100, 255), visibleSize.width/4, visibleSize.height/4);
- green->ignoreAnchorPointForPosition(false);
- green->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2 - 100));
- red->setPositionZ(1);
- green->setPositionZ(0);
- this->addChild(red, 0);
- this->addChild(green, 1);
- virtual bool onTouchBegan(Touch *touch, Event * event);
- virtual void onTouchEnded(Touch *touch, Event * event);
- virtual void onTouchCancelled(Touch *touch, Event * event);
- virtual void onTouchMoved(Touch *touch, Event * event);
- // 把世界坐标转换到当前节点的本地坐标系中
- Point convertToNodeSpace(const Point& worldPoint) const;
-
- // 把基于当前节点的本地坐标系下的坐标转换到世界坐标系中
- Point convertToWorldSpace(const Point& nodePoint) const;
-
- // 基于Anchor Point把基于当前节点的本地坐标系下的坐标转换到世界坐标系中
- Point convertToNodeSpaceAR(const Point& worldPoint) const;
-
- // 基于Anchor Point把世界坐标转换到当前节点的本地坐标系中
- Point convertToWorldSpaceAR(const Point& nodePoint) const;
auto *sprite1 = Sprite::create("HelloWorld.png"); sprite1->setPosition(ccp(20,40)); sprite1->setAnchorPoint(ccp(0,0)); this->addChild(sprite1); //此时添加到的是世界坐标系,也就是OpenGL坐标系 auto *sprite2 = Sprite::create("HelloWorld.png"); sprite2->setPosition(ccp(-5,-20)); sprite2->setAnchorPoint(ccp(1,1)); this->addChild(sprite2); //此时添加到的是世界坐标系,也就是OpenGL坐标系 //将 sprite2 这个节点的坐标ccp(-5,-20) 转换为 sprite1节点 下的本地(节点)坐标系统的 位置坐标 Point point1 = sprite1->convertToNodeSpace(sprite2->getPosition()); //将 sprite2 这个节点的坐标ccp(-5,-20) 转换为 sprite1节点 下的世界坐标系统的 位置坐标 Point point2 = sprite1->convertToWorldSpace(sprite2->getPosition()); log("position = (%f,%f)",point1.x,point1.y); log("position = (%f,%f)",point2.x,point2.y);
- 运行结果:
-
- Cocos2d: position = (-25.000000,-60.000000)
- Cocos2d: position = (15.000000,20.000000)
其中:Point point2 = sprite1->convertToWorldSpace(sprite2->getPosition());
此时的变换是将sprite2
的坐标转换到sprite1
的世界坐标系下,而其中世界坐标系是没有变化的,始终都是和OpenGL等同,只不过sprite2
在变换的时候将sprite1
作为了”参照“而已。所以变换之后sprite2
的坐标为:(15,20)。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。