Tov*_*vi7 6 iphone objective-c cocos2d-iphone
我一直在寻找一段时间,但由于某些原因我无法找到答案.这看起来很简单,但也许我在库中找不到合适的功能.
我有一个场景,其中包含一堆CCNode,其中每个CCNode都有一个CCSprite.
在应用程序中,我在主层的位置移动,以便在某种程度上围绕摄像机"平移".(即我翻译整个图层以使视口发生变化).
现在我想确定屏幕坐标中CCNode的绝对位置.position属性返回相对于父节点的位置,但我真的希望将其转换为屏幕上的实际位置.
另外,作为一个额外的奖励,如果我可以将这个位置表示为坐标系,其中0,0映射到屏幕的左上方,并且1,1映射到屏幕的右下方,那将是非常棒的.(所以我保持与所有设备兼容)
编辑:请注意,解决方案最好适用于任何CCNode层次结构.
nas*_*ash 18
每个CCNode及其后代都有一个名为convertToWorldSpace:(CGPoint)p的方法
这将返回相对于场景的坐标.
当你有这个坐标时,翻转你的Y轴,你想要0,0在左上角.
CCNode * myNode = [[CCNode alloc] init];
myNode.position = CGPointMake(150.0f, 100.0f);
CCSprite * mySprite = [[CCSprite alloc] init]; // CCSprite is a descendant of CCNode
[myNode addChild: mySprite];
mySprite.position = CGPointMake(-50.0f, 50.0f);
// we now have a node with a sprite in in. On this sprite (well, maybe
// it you should attack the node to a scene) you can call convertToWorldSpace:
CGPoint worldCoord = [mySprite convertToWorldSpace: mySprite.position];
// to convert it to Y0 = top we should take the Y of the screen, and subtract the worldCoord Y
worldCoord = CGPointMake(worldCoord.x, ((CGSize)[[CCDirector sharedDirector] displaySizeInPixels]).height - worldCoord.y);
// This is dry coded so may contain an error here or there.
// Let me know if this doesn't work.
[mySprite release];
[myNode release];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14170 次 |
| 最近记录: |