在 swift 后 x 秒后删除标签
Remove label after x seconds in swift
在我的简单游戏(乒乓球)中,我想在得分时在屏幕上显示一个 SKLabel 节点。此标签必须停留 1 秒,然后它必须消失。
我该怎么做?
PS: 我不能使用 "sleep" 函数,因为我希望其他代码保留 运行.
您可以使用 SKSequence
添加和删除标签。一个序列确实在前一个动作完成后开始延迟的动作。
var timeToWait:NSTimeInterval = 2.0
//Wait a given amount of time. Here 2 seconds
var waitAction = SKAction.waitForDuration(timeToWait)
//Block to add your Label
var addLabelBlock = SKAction.runBlock({
addChild(yourLabel)
})
//Action to remove your label from the parent.
var removeNodeAction = SKAction.removeFromParent()
//Everything put in a sequence
var addAndRemoveSequence = SKAction.sequence([addLabelBlock, waitAction, removeNodeAction])
yourLabel.runAction(addAndRemoveSequence)
在我的简单游戏(乒乓球)中,我想在得分时在屏幕上显示一个 SKLabel 节点。此标签必须停留 1 秒,然后它必须消失。
我该怎么做?
PS: 我不能使用 "sleep" 函数,因为我希望其他代码保留 运行.
您可以使用 SKSequence
添加和删除标签。一个序列确实在前一个动作完成后开始延迟的动作。
var timeToWait:NSTimeInterval = 2.0
//Wait a given amount of time. Here 2 seconds
var waitAction = SKAction.waitForDuration(timeToWait)
//Block to add your Label
var addLabelBlock = SKAction.runBlock({
addChild(yourLabel)
})
//Action to remove your label from the parent.
var removeNodeAction = SKAction.removeFromParent()
//Everything put in a sequence
var addAndRemoveSequence = SKAction.sequence([addLabelBlock, waitAction, removeNodeAction])
yourLabel.runAction(addAndRemoveSequence)