理解使用Swift的坐标系统SpriteKit的问题

时间:2023-01-22 23:17:40

I am having trouble fully grasping the coordinate system with SpriteKit using Swift. I have looked at many resources including apples dev docs but for some reason I am very confused (i'm sure this must be simple).

我在使用Swift完全掌握SpriteKit的坐标系时遇到了麻烦。我查看了很多资源,包括苹果开发文档,但出于某种原因,我感到非常困惑(我确信这一定很简单)。

I have a GameScene class inheriting from SKScene. I am just trying to add a SKSpriteNode to the screen using various coordinates to get used to the screen layout. I believe that when you create any SKNode and give it a position by doing the following

我有一个从SKScene继承的GameScene类。我只是想在屏幕上添加一个SKSpriteNode,使用各种坐标来适应屏幕布局。我相信,当您创建任何SKNode并通过以下操作给它一个位置时

var node = SKSpriteNode(color: UIColor.brownColor(), size: CGSizeMake(100, 100))
node.position = CGPointMake(0, 0)

and then place any SKNode using

然后放置任何SKNode

self.addChild(node)

The coordinates that have been supplied to the node are coordinates in the parent node. Does this mean that the brown square that has been created should be placed at the bottom left corner?

提供给节点的坐标是父节点中的坐标。这是否意味着已经创建的棕色方块应该放在左下角?

This is obviously not the case and is what is confusing me. I am using the iPhone 5s simulator for testing in portrait.

显然不是这样的,这让我很困惑。我正在使用iPhone 5s模拟器进行人像测试。

The sprite node only appears in the bottom left when I set the position to (350, 50).

当我将位置设置为(350,50)时,sprite节点只出现在左下角。

2 个解决方案

#1


9  

Select the GameScene.sks in the Project Navigator, select the SKScene (it should have a yellow border round it) and set its size property to 320 points x 569 points (this is assuming iPhone5 / 5S) either way this is the problem. Also make sure you double check it as I am sure it changed back to 1024x768 on me, but seems to be working now.

选择GameScene。在项目导航器中,sks选择SKScene(它应该有一个黄色边框围绕它),并将其大小属性设置为320点x 569点(这是假设iPhone5 / 5S)。还要确保你仔细检查过它,因为我确信它在我身上变成了1024x768,但现在似乎还在工作。

#2


4  

In the GameViewController class at the function viewDidLoad(), add this line of code:

在函数viewDidLoad()的GameViewController类中,添加这一行代码:

scene.size = skView.bounds.size

#1


9  

Select the GameScene.sks in the Project Navigator, select the SKScene (it should have a yellow border round it) and set its size property to 320 points x 569 points (this is assuming iPhone5 / 5S) either way this is the problem. Also make sure you double check it as I am sure it changed back to 1024x768 on me, but seems to be working now.

选择GameScene。在项目导航器中,sks选择SKScene(它应该有一个黄色边框围绕它),并将其大小属性设置为320点x 569点(这是假设iPhone5 / 5S)。还要确保你仔细检查过它,因为我确信它在我身上变成了1024x768,但现在似乎还在工作。

#2


4  

In the GameViewController class at the function viewDidLoad(), add this line of code:

在函数viewDidLoad()的GameViewController类中,添加这一行代码:

scene.size = skView.bounds.size