Setera Setera - 6 months ago 37
Swift Question

SKAudioNode else/if off/on Case

I'm new to Swift coding and trying to figure out how to make looped audio with on/off function while touching a Node.
I figured that the best way for me to implement it is through SKAudioNode, but I'm not sure what I'm doing wrong in the following code.
When pressed on the Node - the SKAudioNode Starts to play, and then, after pressing to switch the sound off - the sound doesn't stop. When pressed another time - the sound overleaps on the previous one.
Putting the whole essential piece here. Thank you in advance.

import SpriteKit
import AVFoundation


I'm creating a boolean here to trigger if sound is on or off:

var firesoundon = false


Setting up a scene next.

class GameScene2: SKScene {

override func didMoveToView(view: SKView) {
setUpScenery()
}


private func setUpScenery() {


//BACKGROUND
let background = SKSpriteNode(imageNamed: backgroundImage, normalMapped: true)
background.anchorPoint = CGPointMake(0, 0)
background.position = CGPointMake(0, 0)
background.zPosition = Layer.ZBackground
background.size = CGSize(width: self.view!.bounds.size.width, height: self.view!.bounds.size.height)
background.lightingBitMask = 1;
addChild(background)


//FIRE

let fireLayerTexture = SKTexture(imageNamed: fireImage)
let fireLayer = SKSpriteNode(texture: fireLayerTexture)

fireLayer.anchorPoint = CGPointMake(1, 0)
fireLayer.position = CGPointMake(size.width, 0)
fireLayer.zPosition = Layer.Z4st
addChild(fireLayer)

let firePath = UIBezierPath()
firePath.moveToPoint(CGPointMake(0, 0))
firePath.addCurveToPoint(CGPointMake(115, 215), controlPoint1: CGPointMake(5, 170), controlPoint2: CGPointMake(90, 190))
firePath.addCurveToPoint(CGPointMake(150, 260), controlPoint1: CGPointMake(115, 215), controlPoint2: CGPointMake(120, 295)) //curve1
firePath.addCurveToPoint(CGPointMake(180, 280), controlPoint1: CGPointMake(150, 260), controlPoint2: CGPointMake(155, 350)) //curve2
firePath.addCurveToPoint(CGPointMake(225, 280), controlPoint1: CGPointMake(180, 280), controlPoint2: CGPointMake(210, 395)) //curve3
firePath.addCurveToPoint(CGPointMake(250, 260), controlPoint1: CGPointMake(230, 280), controlPoint2: CGPointMake(245, 290)) //curve4
firePath.addCurveToPoint(CGPointMake(290, 220), controlPoint1: CGPointMake(250, 260), controlPoint2: CGPointMake(280, 270)) //curve5
firePath.addLineToPoint(CGPointMake(380, 200))
firePath.addLineToPoint(CGPointMake(380, 0))
firePath.addLineToPoint(CGPointMake(0, 0))
let fireNode = SKShapeNode(path: firePath.CGPath, centered: false)
fireNode.alpha = 0.2
fireNode.zPosition = Layer.Z4st+1
fireNode.lineWidth = 2
fireNode.strokeColor = SKColor.whiteColor()
fireNode.fillColor = SKColor.blueColor()
fireNode.position = CGPointMake(size.width/2, 0)
fireNode.name = "fireNode"

self.addChild(fireNode)


Creating a Fire on/off function:

func fireTurnsOnOff () {

let playguitar: SKAudioNode
playguitar = SKAudioNode(fileNamed: "guitar.wav")

if firesoundon == false {

addChild(playguitar)

firesoundon = true
print("firesoundon is == \(firesoundon)")
}

else if firesoundon == true {

let stopguitar = SKAction.stop()
playguitar.runAction(stopguitar)
print("GuiterStopped")

firesoundon = false
print("firesoundon is == \(firesoundon)")
}
}



override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {

let touchLocation = touch.locationInNode(self)
let node: SKNode = nodeAtPoint(touchLocation)

if node.name == "fireNode" {
fireTurnsOnOff()
}

}
}

Answer

Every time you are pressing the sound button you are creating a new instance of SKAudioNode which probably causes the sound issues.

Take theses two lines

let playguitar: SKAudioNode
playguitar = SKAudioNode(fileNamed: "guitar.wav")   

out of

 func fireTurnsOnOff() {...

Than put

  let playguitar: SKAudioNode

above DidMoveToView as a class/global property and

  playguitar = SKAudioNode(fileNamed: "guitar.wav")  

inside didMoveToView.