转换SpriteKit坐标

时间:2015-08-06 17:36:21

标签: ios objective-c sprite-kit

我试图让我的游戏在iOS 9上运行,但我遇到了问题。 我正在使用此代码计算新节点的位置,它在iOS 7和8中工作正常,但现在在iOS 9中它不起作用:

CGPoint position = [self convertPointFromView:CGPointMake([self convertPointToView:CGPointMake(node.position.x, node.position.y)].x - 30, [self convertPointToView:CGPointMake(node.position.x, node.position.y)].y - 30)];

我尝试了另一种方法,但存在同样的问题:

CGPoint position = [self.view convertPoint:CGPointMake(roundf([self.view convertPoint:CGPointMake(node.position.x, node.position.y) fromScene:self.scene].x + 30), roundf([self.view convertPoint:CGPointMake(node.position.x, node.position.y) fromScene:self.scene].y + 30)) toScene:self.scene];

然后我决定用它来测试它:

NSLog(@"%f", node.position.y);
CGPoint test = [self.view convertPoint:CGPointMake(node.position.x, node.position.y) fromScene:self];
NSLog(@"%f", test.y);
CGPoint test1 = [self.view convertPoint:test toScene:self];
NSLog(@"%f", test1.y);

并收到此作为输出:

2015-08-06 20:25:57.215 Rendzu[30011:226250] 59.076904
2015-08-06 20:25:57.216 Rendzu[30011:226250] -29.999998
2015-08-06 20:25:57.216 Rendzu[30011:226250] 827.076904

现在我认为这是一个错误,或者我不了解它是如何工作的。任何人都可以向我解释一下吗?谢谢你的帮助,对不起我的英语。

1 个答案:

答案 0 :(得分:1)

是的,我认为这是iOS 9中的一个错误,尽管存在一种解决方法。似乎convertPoint:fromScene:

不考虑坐标的不同原点

试试这个:

NSLog(@"%f", node.position.y);
CGPoint test = [self.view convertPoint:CGPointMake(node.position.x,node.position.y) fromScene:self];
NSLog(@"%f", test.y);
CGPoint test1 = [self.view convertPoint:CGPointMake(test.x,test.y + self.view.bounds.size.height) toScene:self];
NSLog(@"%f", test1.y);

这实际上是对the problem

的描述

希望这有帮助