Cocos2dx_世界坐标和模型坐标

前端之家收集整理的这篇文章主要介绍了Cocos2dx_世界坐标和模型坐标前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、世界坐标转成模型坐标

	--Node1
	local node1 = cc.Sprite:create("res/common/node1.png")
	node1:setAnchorPoint(cc.p(1.0,1.0))
	node1:setPosition(cc.p(400,500))
	self.panel.layer:addChild(node1)

	--Node2
	local node2 = cc.Sprite:create("res/common/node2.png")
	node2:setAnchorPoint(cc.p(0.5,0.5))
	node2:setPosition(cc.p(200,300))
	self.panel.layer:addChild(node2)

	local x,y = node2:getPosition()
	local pt1 = node1:convertToNodeSpace(cc.p(x,y))
	local pt2 = node1:convertToNodeSpaceAR(cc.p(x,y))

	cclog("Node2 NodeSpace = (%f,%f)",pt1.x,pt1.y)
	cclog("Node2 NodeSpaceAR = (%f,pt2.x,pt2.y)

例图:


打印:


2、模型坐标转成世界坐标

	--Node1
	local node1 = cc.Sprite:create("res/common/node3.png")
	node1:setAnchorPoint(cc.p(0.5,0.5))
	node1:setPosition(cc.p(400,500))
	self.panel.layer:addChild(node1)

	--Node2
	local node2 = cc.Sprite:create("res/common/node4.png")
	node2:setAnchorPoint(cc.p(0.0,0.0))
	node2:setPosition(cc.p(0,0))
	node1:addChild(node2)

	local x,y = node2:getPosition()
	local pt1 = node1:convertToWorldSpace(cc.p(x,y))
	local pt2 = node1:convertToWorldSpaceAR(cc.p(x,y))

	cclog("Node2 WorldSpace = (%f,pt1.y)
	cclog("Node2 WorldSpaceAR = (%f,pt2.y)
例图:

打印:

原文链接:https://www.f2er.com/cocos2dx/342039.html

猜你在找的Cocos2d-x相关文章