我有一个游戏场景,其中包含与歌曲内容同步发生的动画。我试图设置的其中一个动作与使用SKLabelNode显示的歌曲的歌词有关。我想要做的是根据LabelNode中是否有字符来切换两个动画(歌手停止“唱歌”并等待)。我在更新方法中有这个:
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时,无效状态无法触发。我将“塞子”布尔放在那里,以确保不会像更新方法那样连续调用SKAction。我是Swift和SpriteKit的新手,所以任何帮助都会非常感激。
答案 0 :(得分:1)
你的塞子代码打破了一切,你想做这样的事情:
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时调用它