SKSpriteNode在x和y轴上的定位错误

问题描述 投票:0回答:2

我宣布了一个SKSpriteNode,有一个支柱图像。图像是3000 x 50,我已经声明它是这样的:

var pairs = SKNode()

let pillar = SKSpriteNode(imageNamed: "pillar")
pillar.setScale(0.5)
pillar.physicsBody = SKPhysicsBody(rectangleOf: pillar.size)
pillar.physicsBody?.isDynamic = false
pillar.physicsBody?.affectedByGravity = false
pillar.zPosition = 1

pairs.zPosition = 1
pairs.addChild(pillar)

当我为我的支柱设置位置时,我这样做:

bottomWall.position = CGPoint(x: Int(self.frame.width) + 100, y: 0)

我已经将y设置为0,但是柱状图像大约在y轴的屏幕中间开始,而对于x轴,这个Int(self.frame.width) + 100将柱子从中间定位100px。

所以我的问题是:

为什么这个行为以及我怎样才能让y: 0开始在y: 0开始t x: 0x: 0

swift sprite-kit skspritenode
2个回答
0
投票

默认情况下,您的SKScene的anchorPoint为0.5,0.5。这表示对象的默认位置位于场景的中心。

如果希望从左下角开始放置对象,则需要将场景anchorPoint设置为零。在func didMove(to view: SKView)里面设置这个

self.anchorPoint = CGPoint(x: 0, y: 0)

要么

self.anchorPoint = CGPoint.zero

enter image description here


0
投票

物体的position是关于物体的中心。为了使xy代表bottomWall的左下角你应该设置

bottomWall.anchorPoint = CGPoint(x:0,y:0)

在线之前你定位bottomWall

这将使用对象的左下角作为xy相对于屏幕。

© www.soinside.com 2019 - 2024. All rights reserved.