当我"停止 - 完成"游戏场景,如果发生振动,它不会停止。我无法理解为什么。
当玩家与左或右障碍物碰撞时,手机会发出振动。但是,如果我关闭游戏,同时振动,它会保持振动,控制器被取消初始化。 为什么呢?
我甚至尝试重新定位播放器,但它仍然会振动。
关闭功能
查看控制器
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
}
}
答案 0 :(得分:0)
在您的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
}
}