首页 > 解决方案 > 震动不会停止

问题描述

当我“停止 - 完成”游戏场景时,如果发生振动,它不会停止。我不知道为什么。

当玩家与左右障碍物发生碰撞时,手机会振动。但是如果我关闭游戏,而正在振动,它会一直振动,直到控制器被取消初始化。为什么?

我什至尝试重新定位播放器,但它仍然振动。

关闭函数

视图控制器

func closeGame() {
    if isUserNeedAuthenticating {
        Vibrations.longVibrate()
        viewModel.uploadPoints()
        scene?.stopGame()
        showNextController()
}

游戏场景

func stopGame() {
    gameTimer?.invalidate()
    stopObjectTimers()
    stopMotionManager()
    self.removeAllActions()
    self.removeAllChildren()
}

振动功能

func playerDidCollideWithLeftBarrier() {
    Vibrations.longVibrate()
    let spark = SKEmitterNode(fileNamed: "Sparkling")!
    spark.setScale(0.3)
    player.addChild(spark)
    spark.position = CGPoint(x: -(player.size.width/4), y: 0)
    self.run(SKAction.wait(forDuration: 0.2)) {
        spark.removeFromParent()
    }
}

func playerDidCollideWithRightBarrier() {
    Vibrations.longVibrate()
    let spark = SKEmitterNode(fileNamed: "Sparkling")!
    spark.setScale(0.3)
    player.addChild(spark)
    spark.position = CGPoint(x: (player.size.width/4), y: 0)
    self.run(SKAction.wait(forDuration: 0.2)) {
        spark.removeFromParent()
    }
}

运动功能

override func didSimulatePhysics() {
        let mirrorWidth: CGFloat = 15
        let min = (player.size.width / 2) + mirrorWidth
        let max = frame.width - (player.size.width / 2) - mirrorWidth
        let newPosition = player.position.x + xAcceleration * 30
        if (newPosition < min) {
            playerDidCollideWithLeftBarrier()
            player.position.x = min
            return
        } else if (newPosition > max) {
            player.position.x = max
            playerDidCollideWithRightBarrier()
            return
        } else {
            player.position.x = newPosition
        }
    }


/// A class used to play vibrations
final class Vibrations {

    /// Play a short single vibration, like a tac
    static func tacVibrate() {
        AudioServicesPlaySystemSound(1519) // one tack
    }

    /// Play three shorts tac vibration, like a tac tac tac
    static func threeTacVibrate() {
        AudioServicesPlaySystemSound(1521)
    }

    /// Play a strong boom vibration
    static func boomVibrate() {
        AudioServicesPlaySystemSound(1520)
    }

    /// Play a long vibrations trr trr, it sounds like an error
    static func longVibrate() {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) // heavy tack
    }
}

标签: iosswiftsprite-kit

解决方案


在您的 appdelegate 中,在将辞职通知期间,添加以下行:

AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate)

您还应该为需要取消的任何其他 ID 添加它。

您也可以将其添加到您的课程中以使其更容易。

final class Vibrations {

    /// Play a short single vibration, like a tac
    static func tacVibrate() {
        AudioServicesPlaySystemSound(1519) // one tack
    }

    /// Play three shorts tac vibration, like a tac tac tac
    static func threeTacVibrate() {
        AudioServicesPlaySystemSound(1521)
    }

    /// Play a strong boom vibration
    static func boomVibrate() {
        AudioServicesPlaySystemSound(1520)
    }

    /// Play a long vibrations trr trr, it sounds like an error
    static func longVibrate() {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) // heavy tack
    }

    /// Stops the short single vibration, like a tac
    static func stopTacVibrate() {
        AudioServicesDisposeSystemSoundID(1519) // one tack
    }

    /// Stops the three shorts tac vibration, like a tac tac tac
    static func stopThreeTacVibrate() {
        AudioServicesDisposeSystemSoundID(1521)
    }

    /// Stops the strong boom vibration
    static func stopBoomVibrate() {
        AudioServicesDisposeSystemSoundID(1520)
    }

    /// Stops the long vibrations 
    static func stopLongVibrate() {
        AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate) // heavy tack
    }

}

推荐阅读