我想实现一个带有“彩色突出显示”的 Sprite 节点。 “突出显示可以有多种颜色。这是在沙盒应用程序中完成的,以进行尝试。
我考虑过使用SKTextures来表示节点的“高光”。然而,使用这种方法,我将必须上传许多“突出显示”文件,这些文件必须“预先附加”到主机 spritenode png 文件。因此,如果我有 5 个 spritenode“主机”png 文件和 5 个“高亮”png 文件,那么我总共将拥有 5x5=25 个 png 文件,代表所有可能的组合。需要准备和维护的文件太多了。
所以,我宁愿让 spritenode“主机”有一个子 spritenode,这是亮点。
主机和高亮 Sprite 节点都需要一起移动,并且高亮 Sprite 节点位于“主机” Sprite 节点前面的 1 个 zPosition 处。所以,2个 Sprite 节点的cgposition应该是相同的。
我为主机 spritenode 创建一个类文件。
class Host : SKSpriteNode {
var highlight = SKSpriteNode()
init(strPieceName : String) {
let texture = SKTexture(imageNamed: strPieceName)
super.init(texture: texture, color: UIColor.clear, size: texture.size())
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addHighlight(withColour strColour : String) {
highlight = SKSpriteNode(imageNamed: strColour)
addChild(highlight)
}
}
在GameScene类中,我调用touchesBegan中的addHighlight函数
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let cgPointTouched = t.location(in: self)
let skNodeTouched : SKNode = self.atPoint(cgPointTouched)
switch skNodeTouched.name {
case "Highlight":
host.highlight.position = skNodeTouched.position
host.highlight.anchorPoint = CGPoint(x: 0.5, y: 0.5)
host.highlight.size = CGSize(width: 0.2, height: 0.2)
host.highlight.alpha = 1
host.highlight.zPosition = 3
addChild(host.highlight)
}
}
}
这段代码的问题在于,虽然高亮 Sprite 节点“跟随”主机 Sprite 节点,但高亮 Sprite 节点位置的屏幕外观并不是宿主 Sprite 节点“之上”。但奇怪的是,2个 Sprite 节点的cgposition是相同的。
我认为 GameScene 和 Highlight 类的坐标系统不一样,但我不知道如何解决这个问题以及为什么会发生这种情况。
最佳答案
不知道为什么你要做所有这些复杂的事情,但你不应该碰这个位置。将其保留为 0,0,并且永远不要将其添加到场景中。相反,将其留在 Sprite 上,它将始终跟随您的 Sprite 。
当然,您始终可以使用colorBlendFactor
将 Sprite 颜色与纹理混合以创建高光
你的类应该是这样的:
class Host : SKSpriteNode {
var highlight : SKSpriteNode!
func addHighlight(withColour strColour : String) {
highlight.removeFromParent()
highlight = SKSpriteNode(imageNamed: strColour)
//highlight.anchorPoint = CGPoint(x: 0.5, y: 0.5)
highlight.size = CGSize(width: 0.2, height: 0.2)
//highlight.alpha = 1
highlight.zPosition = 3
highlight.moveToParent(self)
}
}
无需添加额外的 init 来执行与其他 init 完全相同的操作
您的触摸代码应如下所示:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let cgPointTouched = t.location(in: self)
let skNodeTouched : SKNode = self.atPoint(cgPointTouched)
switch skNodeTouched.name {
case "Highlight":
host.addHighlight(withColour:"whatevermycoloris")
}
}
}
当然,您始终可以通过执行以下操作来避免额外的节点:
class Host : SKSpriteNode {
func addHighlight(withColour : UIColor) {
color = withColour
colorBlendFactor = 0.75 //Change this intensity to 1 to add more color
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let cgPointTouched = t.location(in: self)
let skNodeTouched : SKNode = self.atPoint(cgPointTouched)
switch skNodeTouched.name {
case "Highlight":
host.addHighlight(withColour:UIColor.yellow)
}
}
}
关于iOS Swift SpriteKit : How to make a child spritenode's position and movements to be the same as its parent spritenode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51934599/