Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Audio Playback speed

I am developing a game where the main sprite is a spinning wheel. I would like to associate audio to the spinning wheel that changes pitch as the wheel slows down. When the wheel stops spinning I would like the sound effect to stop. Can someone point me in the right direction?

In my game scene I have the following code:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch in touches {
        let location = touch.location(in: self)
        //Start spinning the wheel
        if atPoint(location).name == "play_button" {
            player?.physicsBody?.angularVelocity = 0
            player?.rotatePlayer()

            //Click back button
        } else if atPoint(location).name == "back_button" {
            let play_scene = CharacterSelectScene(fileNamed: "CharacterSelect")
            play_scene?.scaleMode = .aspectFill
            self.view?.presentScene(play_scene!, transition: SKTransition.doorsOpenVertical(withDuration: 1))
        } else if atPoint(location).name == "settings_button" {
            let play_scene = SettingsScene(fileNamed: "SettingsScene")
            play_scene?.scaleMode = .aspectFill
            self.view?.presentScene(play_scene!, transition: SKTransition.doorsOpenVertical(withDuration: 1))
        }
    }
}

override func didSimulatePhysics() {
    let speed = player?.physicsBody?.angularVelocity
    if (speed != CGFloat(0.0)) {
        if (speed! <= CGFloat(0.1)){
            finishedRotation = true
        }
    }
}
override func didFinishUpdate() {
    if (finishedRotation == true) {
        let play_scene = QuestionScene(fileNamed: "QuestionScene")
        play_scene?.scaleMode = .aspectFill
        self.view?.presentScene(play_scene!, transition: SKTransition.doorsOpenVertical(withDuration: 1))
    }
}
func didBegin(_ contact: SKPhysicsContact) {
    let defaults = UserDefaults.standard
    var firstBody = SKPhysicsBody()
    var secondBody = SKPhysicsBody()
    if (contact.bodyA.node?.name == "pin") {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }
    if (firstBody.node?.name == "pin" && secondBody.node?.name == "geography") {
        let set_topic = secondBody.node?.name
        defaults.set(set_topic, forKey: "Topic")
    } else if (firstBody.node?.name == "pin" && secondBody.node?.name == "maths") {
        let set_topic = secondBody.node?.name
        defaults.set(set_topic, forKey: "Topic")
    } else if (firstBody.node?.name == "pin" && secondBody.node?.name == "history") {
        let set_topic = secondBody.node?.name
        defaults.set(set_topic, forKey: "Topic")
    } else if (firstBody.node?.name == "pin" && secondBody.node?.name == "science") {
        let set_topic = secondBody.node?.name
        defaults.set(set_topic, forKey: "Topic")
    }
}

The rotatePlayer method from the Player class is defined as below:

func rotatePlayer() {
    let random = GKRandomDistribution(lowestValue: 20, highestValue: 120)
    let r = random.nextInt()
    self.physicsBody?.angularVelocity = 0
    self.physicsBody?.angularVelocity = CGFloat(r)
    self.physicsBody?.angularDamping = 1.02

}
like image 709
Nick Avatar asked Oct 30 '22 07:10

Nick


1 Answers

I have resolved the issue and the code is listed below:

func playSound() {
    let url = Bundle.main.url(forResource: "clicker", withExtension: "m4a")!

    do {
        SoundEffect = try AVAudioPlayer(contentsOf: url)
        guard let players = SoundEffect else { return }

        players.prepareToPlay()
        players.play()
    } catch let error as NSError {
        print(error.description)
    }
}
func didEnd(_ contact: SKPhysicsContact) {

    let defaults = UserDefaults.standard
    var firstBody = SKPhysicsBody()
    var secondBody = SKPhysicsBody()
    if (contact.bodyA.node?.name == "pin") {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }
    if (firstBody.node?.name == "pin" && secondBody.node?.name == "geography") {
        let set_topic = secondBody.node?.name
        defaults.set(set_topic, forKey: "Topic")
        playSound()
    } else if (firstBody.node?.name == "pin" && secondBody.node?.name == "maths") {
        let set_topic = secondBody.node?.name
        defaults.set(set_topic, forKey: "Topic")
        playSound()
    } else if (firstBody.node?.name == "pin" && secondBody.node?.name == "history") {
        let set_topic = secondBody.node?.name
        defaults.set(set_topic, forKey: "Topic")
        playSound()
    } else if (firstBody.node?.name == "pin" && secondBody.node?.name == "science") {
        let set_topic = secondBody.node?.name
        defaults.set(set_topic, forKey: "Topic")
        playSound()
    }
}
like image 78
Nick Avatar answered Nov 15 '22 05:11

Nick