运行 基于 SKLabel 节点文本字符数的 SKAction

run an SKAction based on the character count of an SKLabel node's text

我有一个游戏场景,其中包含与歌曲内容同步出现的动画。我尝试设置的其中一项操作与使用 SKLabelNode 显示的歌曲歌词有关。我想要做的是根据 LabelNode 中是否有字符在两个动画之间切换(歌手停止 "singing" 并等待)。我在更新方法中有这个:

var singerActive = false
var lyricsLabel = SKLabelNode()

override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        if lyricsLabel.text?.characters.count == 0{
            singerActive = false
        }else{
            singerActive = true
        }

        let sings = SKAction.runBlock { () -> Void in
            self.singer.runAction(SKAction.fadeAlphaTo(1, duration: 0.1))
            self.singerIdle.runAction(SKAction.fadeAlphaTo(0, duration: 0.1))
        }
        let singerInactive = SKAction.runBlock { () -> Void in
            self.singer.runAction(SKAction.fadeAlphaTo(0, duration: 0.1))
            self.singerIdle.runAction(SKAction.fadeAlphaTo(1, duration: 0.1))
        }

        var stopper = true
        if singerActive && stopper{
            stopper = false
            singer.runAction(sings)
        }

        if singerActive == false && stopper == false{

            stopper = true
            singer.runAction(singerInactive)
        }

    }

不幸的是,这似乎不起作用。活跃的歌手状态有效,但当字符数 == 0 时,不活跃状态无法触发。我将 "stopper" 布尔值放在那里,以确保 SKAction 在更新时不会被连续调用方法。我是 Swift 和 SpriteKit 的新手,所以非常感谢任何帮助。

你的阻止代码破坏了一切,你想做这样的事情:

    override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    if lyricsLabel.text?.characters.count == 0{
        singerActive = false
    }else{
        singerActive = true
    }

    let sings = SKAction.runBlock { () -> Void in
        self.singer.runAction(SKAction.fadeAlphaTo(1, duration: 0.1))
        self.singerIdle.runAction(SKAction.fadeAlphaTo(0, duration: 0.1))
    }
    let singerInactive = SKAction.runBlock { () -> Void in
        self.singer.runAction(SKAction.fadeAlphaTo(0, duration: 0.1))
        self.singerIdle.runAction(SKAction.fadeAlphaTo(1, duration: 0.1))
    }

    if singerActive{
        singer.removeAllActions()
        singer.runAction(sings)
    }
    else{
        singer.removeAllActions()
        singer.runAction(singerInactive)
    }

}

但这也很糟糕,因为这意味着您总是 运行 对每次更新采取行动。您可能需要重新编写代码,以免发生这种情况。

所以你需要做这样的事情:

    override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    if lyricsLabel.text?.characters.count == 0{
        singerActive = false
    }else{
        singerActive = true
    }

    let sings = SKAction.runBlock { () -> Void in
        self.singer.runAction(SKAction.fadeAlphaTo(1, duration: 0.1))
        self.singerIdle.runAction(SKAction.fadeAlphaTo(0, duration: 0.1))
    }
    let singerInactive = SKAction.runBlock { () -> Void in
        self.singer.runAction(SKAction.fadeAlphaTo(0, duration: 0.1))
        self.singerIdle.runAction(SKAction.fadeAlphaTo(1, duration: 0.1))
    }

    if singerActive && singerIdle.alpha = 1{
        singer.removeAllActions()
        singerIdle.alpha = 0.99;
        singer.runAction(sings)
    }
    else if !singerActive && singer.alpha = 1{
        singer.removeAllActions()
        singer.alpha = 0.99;
        singer.runAction(singerInactive)
    }

}

现在我们只在 alpha 状态为 1 时调用它