ios - 震动不会停止
问题描述
当我“停止 - 完成”游戏场景时,如果发生振动,它不会停止。我不知道为什么。
当玩家与左右障碍物发生碰撞时,手机会振动。但是如果我关闭游戏,而正在振动,它会一直振动,直到控制器被取消初始化。为什么?
我什至尝试重新定位播放器,但它仍然振动。
关闭函数
视图控制器
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
}
}
解决方案
在您的 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
}
}
推荐阅读
- mongodb - 使用特定文档的过滤器计算 mongo 数组中的元素数
- javascript - Asp.net C#在插入记录之前用JS确认对话框检查条件不触发为什么?
- javascript - 在不同的选择标签中附加带有值的选项标签
- progressive-web-apps - PWA - 如何指定版本?
- c# - 如何在c#中比较两个数组并计算相等的项目
- python - 在 Python 中,在循环中使用三个函数,其中第三个函数的输出是所有三个函数的新输入
- python-3.x - 将 CSS 与 Bottle 连接起来
- excel - Excel VBA 在特定单元格中添加值
- android - 如何在同一个 Realm 查询中查询两个字段?
- python - 如何将 gizeh 矢量动画叠加到具有透明度的电影视频上?