iOS Swift SpriteKit : How to make a child spritenode's position and movements to be the same as its parent spritenode?

标签 ios swift sprite-kit skspritenode coordinate-systems

我想实现一个带有“彩色突出显示”的 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/

相关文章:

iphone - NSURL 到 NSString

ios - 使用 ARKit 的面部和相机之间的距离

ios - CocoaPods iOS 框架链接 i386 文件

ios - SpriteKit - 更改纹理后 FPS 下降

swift - 将 ARFaceAnchor 与 SpriteKit 叠加层对齐

ios - 访问和使用应用程序沙箱中的文件

ios - NSPlaceHolderString 泄漏,Instruments 没有告诉我在哪里?

ios - 如何通过 Swift 扩展实现委托(delegate)方法

swift - 单击事件 NSTextField OSX

ios - Sprite 套件游戏中的计分系统