cocos2dx 坐标系统详解

1、在cocos2dx中,支持以下坐标系:

● 屏幕坐标系: 原点在左上角,X轴向右,Y轴向下。(比如处理触摸事件时CCTouch对象中的坐标就是屏幕坐标系)

● OpenGL坐标系: 原点在左下角,X轴向右,Y轴向上。(比如CCNode类的setPosition函数调用就是此坐标系)

● 世界坐标系:指相对于整个屏幕的坐标系,(0,0)就是屏幕的左下角

● 本地坐标系:相对于父对象的坐标。


2、锚点(AnchorPoint)


● 一句话来描述就是:锚点我们可以看成用一根图钉将一张纸或者相片钉在墙上的那个点。


● 锚点的x和y取值范围在[0,1]之间。


● 精灵(Sprite)的锚点默认为(0.5,0.5),默认是不忽略锚点的,所以

virtual bool isIgnoreAnchorPointForPosition();//默认返回false

●  其他节点比如说CCLayer和CCSense,锚点默认为(0,0),默认是忽略锚点

virtual bool isIgnoreAnchorPointForPosition();//默认返回true


所以,如果要在CCLayer和CCSense中设置锚点,要如下设置

	CCLayer* layer1 = CCLayerColor::create(ccc4(255,0,0,255), 300, 200);	
	addChild(layer1);
	layer1->ignoreAnchorPointForPosition(false);//设置不忽略锚点,此时锚点会变为默认的(0.5,0.5)
	layer1->setAnchorPoint(ccp(0,0));//然后再这里设置自定义锚点





3 坐标转换,详细解释如下,应该已经说得很明白了。


CCSprite *sprite1=CCSprite::create("CloseNormal.png");
	CCLOG("sprite1=(%f,%f)",sprite1->getContentSize().width,sprite1->getContentSize().height);
	sprite1->setAnchorPoint(ccp(1,1));

	sprite1->setPosition(ccp(20,40));

	CCSprite *sprite2=CCSprite::create("CloseNormal.png");
	sprite2->setPosition(ccp(-5,-20));
	sprite2->setAnchorPoint(ccp(1,1));

	CCPoint point1=sprite1->convertToNodeSpace(sprite2->getPosition());//以sprite1左下角为参考点(0,0)定位sprite2锚点的坐标,并返回
	CCPoint point2=sprite1->convertToWorldSpace(sprite2->getPosition());//以sprite1左下角为参考点(0,0),定位(-5,20)这个点为sprite2锚点坐标,并返回
	CCPoint point3=sprite1->convertToNodeSpaceAR(sprite2->getPosition());//以sprite1的锚点为参考点(0,0),定位sprite2锚点的位置,并返回
	CCPoint point4=sprite1->convertToWorldSpaceAR(sprite2->getPosition());//以sprite1的锚点为参考点(0,0),定位定位(-5,20)这个点为sprite2锚点坐标,并返回
	
	CCLOG("point1=(%f,%f)",point1.x,point1.y);
	CCLOG("point2=(%f,%f)",point2.x,point2.y);
	CCLOG("point3=(%f,%f)",point3.x,point3.y);
	CCLOG("point4=(%f,%f)",point4.x,point4.y);



输出结果如下:

sprite1=(40.000000,40.000000)    (图片大小)
point1=(15.000000,-20.000000)
point2=(-25.000000,-20.000000)
point3=(-25.000000,-60.000000)
point4=(15.000000,20.000000)




posted @ 2014-03-08 18:09  会做菜的老狼  阅读(263)  评论(0编辑  收藏  举报