上下文:
- 有一个光标(就像你的鼠标一样)
SKSpriteNode
- cam是 a
SKCameraNode
并且是光标的子项(即光标所在的位置,因此跟随相机)。 - cam故意不以光标为中心;相反,它是偏移的,所以光标出现在视图的顶部,下面还有空白空间
目标:目标是在相机视图的左下角和右下角添加两个精灵。精灵将是相机的孩子,因此它们始终保持在视野中。
问题
如何将精灵定位在相机的角落,特别是考虑到SKSpriteNode
它没有anchorPoint
属性(通常有SKSpriteNode
,这让我将相机作为一个孩子偏移到光标)?
注意:可以将SKSpriteNode
s定位在GameScene
然后调用.move(toParent: SKNode)
,这会让你更接近,但也会弄乱SKSpriteNode
s的位置和比例
var cam: SKCameraNode!
let cursor = SKSpriteNode(imageNamed: "cursor")
override func didMove(to view: SKView) {
// Set up the cursor
cursor.setScale(spriteScale)
cursor.position = CGPoint(x: self.frame.midX, y: raisedPositioning)
cursor.anchorPoint = CGPoint(x:0.5, y:0.5)
cursor.zPosition = CGFloat(10)
addChild(cursor)
// Set up the camera
cam = SKCameraNode()
self.camera = cam
cam.setScale(15.0)
// Camera is child of Cursor so that the camera follows the cursor
cam.position = CGPoint(x: cursor.size.width/2, y: -(cursor.size.height * 4))
cursor.addChild(cam)
// Add another sprite here and make it child to cursor
...