In spritekit, is there a quick way to position a node at the center of the screen without having to calculate the coordinate manually?
在spritekit中,有一种快速的方法可以将节点定位在屏幕的中心而无需手动计算坐标吗?
2 个解决方案
#1
3
if your scene has an anchorPoint of CGPoint(x: 0.5, y: 0.5)
then any object you add to the scene will automatically be placed in the center of the screen.
如果您的场景有一个CGPoint的锚点(x:0.5,y:0.5),那么您添加到场景中的任何对象都将自动放置在屏幕的中心。
If your scene has an anchorPoint of CPoint.zero (lower left corner) you can set the position of the object by setting object.position = CGPoint(x: self.size.width / 2, y: self.size.height/ 2)
where self is the scene
如果您的场景有一个CPPoint.zero的anchorPoint(左下角),您可以通过设置object.position = CGPoint(x:self.size.width / 2,y:self.size.height / 2)来设置对象的位置。 )自我是场景
#2
3
Apart from Ron Myschuk answer you can also use the scene frame property. I think this way is the most flexible as it will work regardless of anchor point.
除了Ron Myschuk之外,您还可以使用场景框架属性。我认为这种方式最灵活,因为无论锚点如何都可以。
e.g
// Center
yourNode.position = CGPoint(x: frame.midX, y: frame.midY)
// Slightly off centre
yourNode.position = CGPoint(x: frame.midX - 100, y: frame.midY - 100)
Hope this helps
希望这可以帮助
#1
3
if your scene has an anchorPoint of CGPoint(x: 0.5, y: 0.5)
then any object you add to the scene will automatically be placed in the center of the screen.
如果您的场景有一个CGPoint的锚点(x:0.5,y:0.5),那么您添加到场景中的任何对象都将自动放置在屏幕的中心。
If your scene has an anchorPoint of CPoint.zero (lower left corner) you can set the position of the object by setting object.position = CGPoint(x: self.size.width / 2, y: self.size.height/ 2)
where self is the scene
如果您的场景有一个CPPoint.zero的anchorPoint(左下角),您可以通过设置object.position = CGPoint(x:self.size.width / 2,y:self.size.height / 2)来设置对象的位置。 )自我是场景
#2
3
Apart from Ron Myschuk answer you can also use the scene frame property. I think this way is the most flexible as it will work regardless of anchor point.
除了Ron Myschuk之外,您还可以使用场景框架属性。我认为这种方式最灵活,因为无论锚点如何都可以。
e.g
// Center
yourNode.position = CGPoint(x: frame.midX, y: frame.midY)
// Slightly off centre
yourNode.position = CGPoint(x: frame.midX - 100, y: frame.midY - 100)
Hope this helps
希望这可以帮助