Swift音频播放速度

时间:2017-06-04 03:54:41

标签: swift sprite-kit avaudioplayer skscene

我正在开发一款主要精灵是旋转轮的游戏。我想将音频与旋转车轮相关联,当车轮减速时,它会改变音高。当车轮停止旋转时,我希望声音效果停止。有人能指出我正确的方向吗?

在我的游戏场景中,我有以下代码:

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")
    }
}

Player类中的rotatePlayer方法定义如下:

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

}

1 个答案:

答案 0 :(得分:2)

我已经解决了这个问题,代码如下:

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()
    }
}