gpt4 book ai didi

swift - 在 SKNode 子类中实现 anchorPoint

转载 作者:搜寻专家 更新时间:2023-10-31 08:34:06 24 4
gpt4 key购买 nike

我有一个继承自 SKNode 的自定义类,我想在该类上实现 SKSpriteNode 的类似 anchor 的功能。具体来说,我想要实现的是自动将自定义节点置于父节点的中心(anchorPoint 为 0.5、0.5),而不必将其偏移其宽度/高度的一半。

有什么建议吗?

编辑:我最终首先创建了一个 anchorPoint 变量,然后覆盖了 position 变量并设置了由 anchorPoint 修改的 super 位置。

var anchorPoint = CGPoint(x: 0.5, y: 0.5)

override var position: CGPoint {
set {
super.position = CGPoint(x: self.position.x - self.size.width * self.anchorPoint.x, y: self.position.y - self.size.height * self.anchorPoint.y)
}
get {
return super.position
}
}

最佳答案

您可以覆盖继承类的 setPosition 函数。在此函数中,您将针对偏移 anchor 进行调整。

例如,如果您的 Sprite 是 100x100。并且你已经设置了它的anchor为(.5,.5),当有人设置位置时,你应该从中添加或减去sprite尺寸然后设置位置。在这种情况下,如果用户将 Sprite 位置设置为 (0,0),您将添加 (100*.5,100*.5) = (50,50)。加上 (0,0) 就是 (50,50)。

这是您应该设置的新位置。

关于swift - 在 SKNode 子类中实现 anchorPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29507027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com