从场景中移除后如何停止 SKSpriteNode 的音频(Swift)?
How to stop an audio of SKSpriteNode after its removed from the scene(Swift)?
这是我的代码:
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let backgroundImage = SKSpriteNode(imageNamed: "Background.jpeg")
backgroundImage.size = self.frame.size
backgroundImage.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
backgroundImage.zPosition = 1
self.addChild(backgroundImage)
let addBugAction = SKAction.sequence([SKAction.runBlock({
self.addBugsToScene()
}), SKAction.waitForDuration(1)])
self.runAction(SKAction.repeatActionForever(addBugAction))
}
func addBugsToScene() {
let bug = SKSpriteNode(imageNamed: "Mos2.gif")
bug.name = "Mosquito"
//giving random position to and assigning to bugs
let randomPoint = gettingRandomPosition(bug.size)
bug.position = CGPoint(x: randomPoint.x, y: randomPoint.y)
bug.zPosition = 12
let blockOFaction = SKAction.runBlock({
let randomMovingPoint = self.gettingRandomPosition(bug.size)
let action = SKAction.moveTo(CGPoint(x: randomMovingPoint.x, y: randomMovingPoint.y), duration: 2)
//action.speed = 3.0
let movePlusSoundAction = SKAction.group([action,SKAction.playSoundFileNamed("MosquitoNoise.wav", waitForCompletion: false)])
bug.runAction(movePlusSoundAction)
})
let waitAction = SKAction.waitForDuration(1)
bug.runAction(SKAction.repeatActionForever(SKAction.sequence([blockOFaction, waitAction])))
self.addChild(bug)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(location)
if touchedNode.name == "Mosquito"{
touchedNode.removeAllActions()
//touchNode.runAction(SKAction.Stop())
//removing the bug which is tapped
touchedNode.removeFromParent()
}
}
}
当我触摸节点(错误)时,它会从场景中移除。在这里,我试图停止播放与错误相关的音频。但即使在节点从场景中移除后,音频仍会继续播放。如何在节点从场景中移除后立即停止音频?
+ playSoundFileNamed:waitForCompletion: 只是播放一个声音。您无法控制正在播放的声音(无法暂停、取消暂停、停止等)。有人说您可以为声音动作指定一个键,然后通过移除该键来停止动作。到目前为止,这对我来说从来没有用过(在许多 iOS 版本上尝试过)。
此外,我用操作键尝试此技巧的最后一个 iOS 版本是 9.1...所以,有机会,您可以在 9.3 上尝试...
无论如何,这是来自文档:
Use SKAction playSoundFileNamed:waitForCompletion: only for short
incidentals. Use AVAudioPlayer for long running background music. This
action is not reversible; the reversed action is identical to the
original action.
所以,它只是播放短音,仅此而已。
作为替代方案,您可以使用 SKAudioNode,但它仅适用于 iOS9 及更高版本。
下面是我如何编码以获得结果。
我在 addBugToScene() 中添加了以下代码
let mosquitoNoise = SKAudioNode(fileNamed: "MosquitoNoise.wav")
mosquitoNoise.autoplayLooped = true
mosquitoNoise.runAction(SKAction.changeVolumeTo(0.3, duration: 60))
bug.addChild(mosquitoNoise)
//changes made here
let movePlusSoundAction = SKAction.group([action,SKAction.runBlock({
mosquitoNoise.runAction(SKAction.play())
})])
bug.runAction(movePlusSoundAction)
})
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(location)
if touchedNode.name == "Mosquito"{
//removes the SKAudioNode so audio will stop
touchedNode.removeAllChildren()
//removing the bug which is tapped
touchedNode.removeFromParent()
}
这是我的代码:
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let backgroundImage = SKSpriteNode(imageNamed: "Background.jpeg")
backgroundImage.size = self.frame.size
backgroundImage.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
backgroundImage.zPosition = 1
self.addChild(backgroundImage)
let addBugAction = SKAction.sequence([SKAction.runBlock({
self.addBugsToScene()
}), SKAction.waitForDuration(1)])
self.runAction(SKAction.repeatActionForever(addBugAction))
}
func addBugsToScene() {
let bug = SKSpriteNode(imageNamed: "Mos2.gif")
bug.name = "Mosquito"
//giving random position to and assigning to bugs
let randomPoint = gettingRandomPosition(bug.size)
bug.position = CGPoint(x: randomPoint.x, y: randomPoint.y)
bug.zPosition = 12
let blockOFaction = SKAction.runBlock({
let randomMovingPoint = self.gettingRandomPosition(bug.size)
let action = SKAction.moveTo(CGPoint(x: randomMovingPoint.x, y: randomMovingPoint.y), duration: 2)
//action.speed = 3.0
let movePlusSoundAction = SKAction.group([action,SKAction.playSoundFileNamed("MosquitoNoise.wav", waitForCompletion: false)])
bug.runAction(movePlusSoundAction)
})
let waitAction = SKAction.waitForDuration(1)
bug.runAction(SKAction.repeatActionForever(SKAction.sequence([blockOFaction, waitAction])))
self.addChild(bug)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(location)
if touchedNode.name == "Mosquito"{
touchedNode.removeAllActions()
//touchNode.runAction(SKAction.Stop())
//removing the bug which is tapped
touchedNode.removeFromParent()
}
}
}
当我触摸节点(错误)时,它会从场景中移除。在这里,我试图停止播放与错误相关的音频。但即使在节点从场景中移除后,音频仍会继续播放。如何在节点从场景中移除后立即停止音频?
+ playSoundFileNamed:waitForCompletion: 只是播放一个声音。您无法控制正在播放的声音(无法暂停、取消暂停、停止等)。有人说您可以为声音动作指定一个键,然后通过移除该键来停止动作。到目前为止,这对我来说从来没有用过(在许多 iOS 版本上尝试过)。
此外,我用操作键尝试此技巧的最后一个 iOS 版本是 9.1...所以,有机会,您可以在 9.3 上尝试...
无论如何,这是来自文档:
Use SKAction playSoundFileNamed:waitForCompletion: only for short incidentals. Use AVAudioPlayer for long running background music. This action is not reversible; the reversed action is identical to the original action.
所以,它只是播放短音,仅此而已。
作为替代方案,您可以使用 SKAudioNode,但它仅适用于 iOS9 及更高版本。
下面是我如何编码以获得结果。
我在 addBugToScene() 中添加了以下代码
let mosquitoNoise = SKAudioNode(fileNamed: "MosquitoNoise.wav")
mosquitoNoise.autoplayLooped = true
mosquitoNoise.runAction(SKAction.changeVolumeTo(0.3, duration: 60))
bug.addChild(mosquitoNoise)
//changes made here
let movePlusSoundAction = SKAction.group([action,SKAction.runBlock({
mosquitoNoise.runAction(SKAction.play())
})])
bug.runAction(movePlusSoundAction)
})
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(location)
if touchedNode.name == "Mosquito"{
//removes the SKAudioNode so audio will stop
touchedNode.removeAllChildren()
//removing the bug which is tapped
touchedNode.removeFromParent()
}