2
votes

I'm trying to implement the "3d" audio Apple has added to SpriteKit, but the sounds really don't seem to be positioned at all, even with headphones on. Here's my relevant code.

Play Sound function:

func playTapSound(node: SKNode) {
    let tapSound = SKAudioNode(fileNamed: "glassNote")
    tapSound.positional = true
    tapSound.autoplayLooped = false
    node.addChild(tapSound)
    tapSound.runAction(SKAction.play())
}

Game Scene:

let listenerNode = SKNode()
...
override func didMoveToView(view: SKView) {
    listenerNode.position = view.center
    listener = listenerNode
}

playTapSound gets called if the user taps a game piece node.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touchedNode = scene.nodeAtPoint(sceneTouchPoint)
    playTapSound(touchedNode)
}

I've checked the positions of all the nodes including the listener node and they're all correct. Listener node is in the center of the screen and the game pieces are basically a checkerboard layout.

When I tap a piece, I really can't tell there's any sort of positioning on the audio. Is it just extremely subtle? Can't find much info out there about it except Apple saying it's part of SpriteKit..

1
I've implemented a simple test with an SKAudioNode. The difference between the audio node being on the left and on the right of the listener was very obvious.0x141E
Yea i should probably do the same - minimum example.Chris Slowik
LMK if you're still not hearing a difference and I'll post my test program.0x141E
Thanks. Yea I did test it recently with just an empty scene and an audio node where I tap. Worked great. In my posted example, its behaving as if the listener is at the lower left, even though the listener's position is the center of the screen.Chris Slowik
Holy cow. Reading the docs for the 1000th time (awake and not half asleep), and it dawned on me. See answer =] Thanks for helping, sometimes you gotta chat it out!Chris Slowik

1 Answers

1
votes

Turns out my mistake was not adding my listenerNode to the scene before setting it to the listener.

From the docs on listener:

If a non-nil value is specified, it must be a node in the scene.

Game Scene updated and verified:

let listenerNode = SKNode()
...
override func didMoveToView(view: SKView) {
    listenerNode.position = view.center
    addChild(listenerNode)
    listener = listenerNode
}