无法理解我的 spritekit 游戏中的延迟?
Can't understand the lag in my spritekit game?
制作一个简单的 spritekit 游戏。我有 AVFoundation
声音库发出的按钮触摸声音等声音。但是有麻烦了。当按下任何按钮时,我的帧率总是下降,听起来。但是,如果我将所有声音都静音,就不会出现延迟。这是我的代码:
import AVFoundation
class GameAudio {
static let shared = GameAudio()
private var buttonClickSound = AVAudioPlayer()
private var completionSound = AVAudioPlayer()
private var swishSound = AVAudioPlayer()
private var gridSwishSound = AVAudioPlayer()
private let Volume: Float = 0.8
func setupSounds() {
print("GameAudio setupSounds()")
if let path = Bundle.main.path(forResource: SoundNames.ButtonClickSoundName, ofType: "mp3") {
let url = URL(fileURLWithPath: path )
do {
buttonClickSound = try AVAudioPlayer(contentsOf: url)
} catch {
print(error)
}
}
if let path = Bundle.main.path(forResource: SoundNames.CompletionSoundName, ofType: "mp3") {
let url = URL(fileURLWithPath: path)
do {
completionSound = try AVAudioPlayer(contentsOf: url)
} catch {
print(error)
}
}
if let path = Bundle.main.path(forResource: SoundNames.SwishSoundName, ofType: "mp3") {
let url = URL(fileURLWithPath: path )
do {
swishSound = try AVAudioPlayer(contentsOf: url)
} catch {
print(error)
}
}
if let path = Bundle.main.path(forResource: SoundNames.GridSwishSoundName, ofType: "mp3") {
let url = URL(fileURLWithPath: path)
do {
gridSwishSound = try AVAudioPlayer(contentsOf: url)
} catch {
print(error)
}
}
buttonClickSound.volume = Volume
completionSound.volume = Volume
swishSound.volume = Volume
gridSwishSound.volume = Volume
}
func playButtonClickSound() {
buttonClickSound.play()
}
func playCompletionSound() {
completionSound.play()
}
func playSwishSound() {
swishSound.play()
}
func playGridSwishSound() {
gridSwishSound.play()
}
}
在我的 GameViewController 中,我调用 GameAudio.shared.setupSounds()
预加载我所有的声音。
public struct Actions {
static func buttonIsTouched(_ button: SKNode, sound: Bool, completion: @escaping () -> ()) {
if button.hasActions() { return }
if sound {
GameAudio.shared.playButtonClickSound()
}
button.run(touchedButtonAction(scaleFactor: button.returnScaleFactor()), completion: completion)
}
}
class MenuNode: SKNode {
private let settings: Settings
var delegate: MenuNodeDelegate?
private let playButton = SKSpriteNode(imageNamed: SpriteNames.PlayButtonName)
private let MenuNodeTreshold: CGFloat = 12.0
init(settings: Settings) {
self.settings = settings
super.init()
setupButtons()
isUserInteractionEnabled = false
}
private func setupButtons() {
playButton.position = CGPoint(x: 0.0 - playButton.frame.size.width / 2.0 - MenuNodeTreshold / 2.0, y: 0.0)
addChild(playButton)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if playButton.contains(location) {
Actions.buttonIsTouched(playButton as SKNode, sound: settings.sound, completion: {
self.delegate?.playButtonTouched()
})
}
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我在游戏中的所有动作都使用 Actions
结构。我有 buttonTouchedAction()
,我在不同的节点中使用它。我用一个 "play" 按钮从我的菜单中输入了一些代码。所以,例如,我 运行 我的游戏在设备上,它显示菜单节点,等待几秒钟然后点击按钮,在我的帧速率下降后,我有一秒钟的延迟,然后场景变为游戏场景。
我遇到过类似的问题。我最终使用 SKAction
来播放短音。
SKAction.playSoundFileNamed("Sounds/Click.wav", waitForCompletion: false)
如果你想使用 AVAudioPlayer
(而不是 SpriteKit's
声音动作),你应该让声音在后台线程中播放,这样它的操作就不会干扰主线程你所有的视觉材料都变成了:
let soundQueue = OperationQueue()
soundQueue.qualityOfService = QualityOfService.background
soundQueue.addOperation{self.buttonClickSound.play()}
制作一个简单的 spritekit 游戏。我有 AVFoundation
声音库发出的按钮触摸声音等声音。但是有麻烦了。当按下任何按钮时,我的帧率总是下降,听起来。但是,如果我将所有声音都静音,就不会出现延迟。这是我的代码:
import AVFoundation
class GameAudio {
static let shared = GameAudio()
private var buttonClickSound = AVAudioPlayer()
private var completionSound = AVAudioPlayer()
private var swishSound = AVAudioPlayer()
private var gridSwishSound = AVAudioPlayer()
private let Volume: Float = 0.8
func setupSounds() {
print("GameAudio setupSounds()")
if let path = Bundle.main.path(forResource: SoundNames.ButtonClickSoundName, ofType: "mp3") {
let url = URL(fileURLWithPath: path )
do {
buttonClickSound = try AVAudioPlayer(contentsOf: url)
} catch {
print(error)
}
}
if let path = Bundle.main.path(forResource: SoundNames.CompletionSoundName, ofType: "mp3") {
let url = URL(fileURLWithPath: path)
do {
completionSound = try AVAudioPlayer(contentsOf: url)
} catch {
print(error)
}
}
if let path = Bundle.main.path(forResource: SoundNames.SwishSoundName, ofType: "mp3") {
let url = URL(fileURLWithPath: path )
do {
swishSound = try AVAudioPlayer(contentsOf: url)
} catch {
print(error)
}
}
if let path = Bundle.main.path(forResource: SoundNames.GridSwishSoundName, ofType: "mp3") {
let url = URL(fileURLWithPath: path)
do {
gridSwishSound = try AVAudioPlayer(contentsOf: url)
} catch {
print(error)
}
}
buttonClickSound.volume = Volume
completionSound.volume = Volume
swishSound.volume = Volume
gridSwishSound.volume = Volume
}
func playButtonClickSound() {
buttonClickSound.play()
}
func playCompletionSound() {
completionSound.play()
}
func playSwishSound() {
swishSound.play()
}
func playGridSwishSound() {
gridSwishSound.play()
}
}
在我的 GameViewController 中,我调用 GameAudio.shared.setupSounds()
预加载我所有的声音。
public struct Actions {
static func buttonIsTouched(_ button: SKNode, sound: Bool, completion: @escaping () -> ()) {
if button.hasActions() { return }
if sound {
GameAudio.shared.playButtonClickSound()
}
button.run(touchedButtonAction(scaleFactor: button.returnScaleFactor()), completion: completion)
}
}
class MenuNode: SKNode {
private let settings: Settings
var delegate: MenuNodeDelegate?
private let playButton = SKSpriteNode(imageNamed: SpriteNames.PlayButtonName)
private let MenuNodeTreshold: CGFloat = 12.0
init(settings: Settings) {
self.settings = settings
super.init()
setupButtons()
isUserInteractionEnabled = false
}
private func setupButtons() {
playButton.position = CGPoint(x: 0.0 - playButton.frame.size.width / 2.0 - MenuNodeTreshold / 2.0, y: 0.0)
addChild(playButton)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if playButton.contains(location) {
Actions.buttonIsTouched(playButton as SKNode, sound: settings.sound, completion: {
self.delegate?.playButtonTouched()
})
}
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我在游戏中的所有动作都使用 Actions
结构。我有 buttonTouchedAction()
,我在不同的节点中使用它。我用一个 "play" 按钮从我的菜单中输入了一些代码。所以,例如,我 运行 我的游戏在设备上,它显示菜单节点,等待几秒钟然后点击按钮,在我的帧速率下降后,我有一秒钟的延迟,然后场景变为游戏场景。
我遇到过类似的问题。我最终使用 SKAction
来播放短音。
SKAction.playSoundFileNamed("Sounds/Click.wav", waitForCompletion: false)
如果你想使用 AVAudioPlayer
(而不是 SpriteKit's
声音动作),你应该让声音在后台线程中播放,这样它的操作就不会干扰主线程你所有的视觉材料都变成了:
let soundQueue = OperationQueue()
soundQueue.qualityOfService = QualityOfService.background
soundQueue.addOperation{self.buttonClickSound.play()}