【发布时间】:2020-10-26 21:25:07
【问题描述】:
我目前正在使用 SpriteKit 和 Swift 制作一个非常简单的 2D 足球游戏。在这个游戏中,可以出现各种能量提升,如果被球击中,这些能量就会消失并以不同的方式影响游戏。当这样的加电时,例如“超级跳跃”被击中,标签应该是“超级跳跃”,等待一秒钟,然后在 0.5 秒内淡出。我这样做的方法如下(上电和球碰撞时调用didInteract函数):
func didInteract_superJump() {
notifications.alpha = 1
notifications.text = "Super jump"
updateNotificationLabel()
// code to actually affect the game goes here ...
}
现在notifications 标签将显示“超级跳跃”。然后调用函数updateNotificationLabel:
func updateNotificationLabel() {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
self.notifications.run(SKAction.fadeOut(withDuration: 0.5))
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
self.notifications.text = ""
})
}
}
这可能是一种不好的做法,因为它只在有时起作用。我真的不知道该怎么做。我已经多次阅读了我的代码,似乎应该可以工作——如前所述,文本有时会消失。其他时候,它只是坐在那里,永远不会消失。
这个奇怪的错误消息可能相关也可能不相关,但它出现在我的控制台中:Sokar Hedz[1936:57105] [general] __CFRunLoopModeFindSourceForMachPort returned NULL for mode 'kCFRunLoopDefaultMode' livePort: 6271.
感谢您的宝贵时间。
【问题讨论】:
标签: swift sprite-kit label skaction