iOS Swift SpriteKit:如何制作儿童spritenode';s的位置和移动是否与其父spritenode相同?

iOS Swift SpriteKit:如何制作儿童spritenode';s的位置和移动是否与其父spritenode相同?,ios,swift,sprite-kit,skspritenode,coordinate-systems,Ios,Swift,Sprite Kit,Skspritenode,Coordinate Systems,我想实现一个带有“彩色高光”的spritenode。“突出显示”可以有多种颜色。这是在沙盒应用程序中完成的 我已经考虑过使用SKTextures来表示节点的“高光”。但是,使用这种方法,我必须上载一些“高光”文件,这些文件必须“预附加”到主机spritenode png文件。因此,如果我有5个spritenode“主机”png文件和5个“高光”“png文件,那么我总共有5x5=25个png文件,代表所有可能的组合。这将是太多的文件准备和维护 因此,我宁愿让spritenode“主机”有一个子sp

我想实现一个带有“彩色高光”的spritenode。“突出显示”可以有多种颜色。这是在沙盒应用程序中完成的

我已经考虑过使用SKTextures来表示节点的“高光”。但是,使用这种方法,我必须上载一些“高光”文件,这些文件必须“预附加”到主机spritenode png文件。因此,如果我有5个spritenode“主机”png文件和5个“高光”“png文件,那么我总共有5x5=25个png文件,代表所有可能的组合。这将是太多的文件准备和维护

因此,我宁愿让spritenode“主机”有一个子spritenode,这是最突出的

主体和突出显示spritenode都需要一起移动,突出显示spritenode位于“主体”spritenode前面的Z1位置。因此,两个spritenodes的位置应该相同

我为主机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)
    }        
}
在GameSecene类中,我在touchesStart中调用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)
    }
    }
 }
override func touchsbegind(touch:Set,带有事件:UIEvent?){
对于t-in-touch{
让cgpointtoucted=t.location(in:self)
让skNodeTouched:SKNode=self.atPoint(cgPointTouched)
开关skNodeTouched.name{
案例“亮点”:
host.highlight.position=skNodeTouched.position
host.highlight.ancorpoint=CGPoint(x:0.5,y:0.5)
host.highlight.size=CGSize(宽度:0.2,高度:0.2)
host.highlight.alpha=1
host.highlight.zPosition=3
addChild(主机突出显示)
}
}
}
此代码的问题在于,尽管高亮显示spritenode“跟随”主机spritenode,但高亮显示spritenode位置的屏幕外观不是主机spritenode的“顶部”。但奇怪的是,两个spritenodes的位置是相同的


我认为游戏场景的坐标系统和Highlight类是不一样的,但我不知道如何解决这个问题,以及为什么会发生这种情况。

不知道为什么要做这些复杂的事情,但你不应该碰这个位置。将其保留为0,0,并且永远不要将其添加到场景中。相反,把它放在雪碧上,它会一直跟着你的雪碧

当然,您可以始终使用
colorBlendFactor
将精灵颜色与纹理混合以创建高光

您的类应该如下所示:

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)
    }        

}
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")
        }
    }
 }
不需要添加与其他init完全相同的额外init

您的触摸代码应如下所示:

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)
    }        

}
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")
        }
    }
 }

override func touchsbegind(touch:Set,带有事件:UIEvent?){
对于t-in-touch{
让cgpointtoucted=t.location(in:self)
让skNodeTouched:SKNode=self.atPoint(cgPointTouched)
开关skNodeTouched.name{
案例“亮点”:
host.addHighlight(带颜色:UIColor.yellow)
}
}
}

colorBlendFactor没有达到我想要的效果,但值得一试。谢谢。
SKSpriteNode
方法有两个问题,一个是突出显示节点的位置仍然与主机节点的位置不对齐。因此,我更改了
addHighlight()
函数以接受位置参数,并传递了
skNodeTouched
的cgposition。这就解决了职位问题。第二个问题是,无论我使用什么位置,高亮显示节点总是显示在主机节点的“顶部”。我希望突出显示节点位于主机节点的“后面”。谢谢,什么,什么?您在问题中没有指定这两个选项。如果位置与主机节点不对齐,则主机节点会被顶起,因为它实际上应该挂在主机节点上,我建议修复主机。如果你想突出显示在你的主机后面(那么你没有突出显示它)使zPosition-1现在我又读了一遍,你把cgPointTouched传递给了突出显示了吗?那就意味着你的亮点出现在现场,而不是像我告诉你的那样出现在主持人身上