New code
class SceneTwo: SKScene, SKPhysicsContactDelegate { let flap = SKAction.playSoundFileNamed("flap.caf", waitForCompletion: false) let whack = SKAction.playSoundFileNamed("whack.caf", waitForCompletion: false) let tap = SKAction.playSoundFileNamed("tap.caf", waitForCompletion: false)
Then I just put
Run (tap) run (flap), etc. where is it necessary ..
Hi, itβs just interesting if I use the correct coding to play sounds in my game. In some context, my game is like a Fluffy bird. One sound is played every time you touch the screen (when the bird has an impulse up), the second sound - when the bird collects a coin between each wall.
I noticed that both of these sounds make my game lag.
Below is my relative sound code for the game.
import AVFoundation var flap: AVAudioPlayer? var tap: AVAudioPlayer? override func didMove(to view: SKView) { tap?.prepareToPlay() flap?.prepareToPlay() func playFlap() { let url = Bundle.main.url(forResource: "flap", withExtension: "caf")! do { flap = try AVAudioPlayer(contentsOf: url) guard let flap = flap else { return } flap.play() } catch let error { print(error.localizedDescription) } } func playTap() { let url = Bundle.main.url(forResource: "tap", withExtension: "caf")! do { tap = try AVAudioPlayer(contentsOf: url) guard let tap = tap else { return } tap.play() } catch let error { print(error.localizedDescription) } }
After that i just
playTap() playFlap()
to where they are needed.
The sound is clear, it seems that my spawning walls are slightly accelerated when the sound is made. Is there something I am doing is wrong?
source share