Reputation: 551
Here is my code:
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()
}
}
}
When I touch node(bug), it will be removed from the scene. Here I'm trying to stop the playing audio associated with the bug. But audio continues playing even after node is removed from the scene. How do I stop the audio as soon as the node removed from the scene?
Upvotes: 3
Views: 789
Reputation: 551
Here is how I coded to get the result.
I added below code in 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()
}
Upvotes: 1
Reputation: 13665
+ playSoundFileNamed:waitForCompletion: just plays a sound. You don't have control over the sound you are playing (can't pause it, unpause it, stop it etc). There are some people saying you can assign a key to the sound action and stop an action by removing the key. That never worked for me so far (tried it on many iOS versions).
Also, the last iOS version I've tried this trick with action key was 9.1...So, there is a chance and you may try it on 9.3...
Anyways, this is from the docs:
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.
So, it simply plays the short sound and that's it.
As an alternative, you can use SKAudioNode but it is available only on iOS9 and above.
Upvotes: 1