Ios Can';我不明白我的spritekit游戏的滞后?
制作一个简单的spritekit游戏。在那里,我听到的声音像是按钮触摸的声音等等,是由Ios Can';我不明白我的spritekit游戏的滞后?,ios,swift,sprite-kit,avfoundation,lag,Ios,Swift,Sprite Kit,Avfoundation,Lag,制作一个简单的spritekit游戏。在那里,我听到的声音像是按钮触摸的声音等等,是由AVFoundation声音库发出的。但我有这个问题。我的帧速率总是下降时,点击任何按钮,这听起来。但如果我把所有声音都静音,就不会有延迟。这是我的密码: import AVFoundation class GameAudio { static let shared = GameAudio() private var buttonClickSound = AVAudioPlayer()
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")
}
}
公共结构操作{
静态函数按钮触摸(u按钮:SKNode,声音:Bool,完成:@escaping()->()){
if button.hasActions(){return}
如果声音{
GameAudio.shared.playButtonClickSound()
}
运行(touchedButtonAction(scaleFactor:button.returnScaleFactor()),完成:完成)
}
}
类MenuNode:SKNode{
私人出租设置:设置
变量代理:MenuNodeDelegate?
private let playButton=SKSpriteNode(图像名:SpriteNames.PlayButtonName)
私有let MenuNodeTreshold:CGFloat=12.0
初始化(设置:设置){
self.settings=设置
super.init()
设置按钮()
isUserInteractionEnabled=false
}
专用功能设置按钮(){
playButton.position=CGPoint(x:0.0-playButton.frame.size.width/2.0-MenuNodeTreshold/2.0,y:0.0)
addChild(播放按钮)
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
如果playButton.contains(位置){
动作。按钮触摸(按SKNode播放按钮,声音:设置。声音,完成:{
self.delegate?.playButtonTouched()
})
}
}
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
}
我在游戏中的所有动作都使用
Actions
struct。我有按钮touchdaction()
,我在不同的节点中使用它。我用一个“播放”按钮从菜单中输入了一些代码。例如,我在设备上运行我的游戏,它显示菜单节点,等待几秒钟,然后点击按钮,在帧速率下降后,我有一秒的延迟,然后场景切换到游戏场景 我也有类似的问题。最后我用了SKAction
来播放短音
SKAction.playSoundFileNamed("Sounds/Click.wav", waitForCompletion: false)
有关详细信息,请参见。如果要使用
AVAudioPlayer
(而不是SpriteKit的
声音操作),则应在后台线程中播放声音,以便其操作不会干扰主线程,而主线程中会播放所有视觉内容:
let soundQueue = OperationQueue()
soundQueue.qualityOfService = QualityOfService.background
soundQueue.addOperation{self.buttonClickSound.play()}
你能发布当你的按钮被“点击”时执行的代码吗。使用一些附加代码进行编辑。
GameAudio
中是否有错误的代码实现,比如对我的声音使用四个AVAudioPlayer()
?或者这没问题?你的游戏音频很好。尽可能多地使用播放器-这只会影响内存使用。我认为你的FPS下降是因为切换了场景,而不是因为播放了真实的声音。为了验证,只需删除切换场景的代码,并在声音播放时观察FPS。切换场景时FPS下降是正常的,我还没有找到解决方法。@斯托扬,但我只有一个场景,在两个节点之间切换-MenuNode
和GameNode
,最后一个是我所有游戏内容所在的位置。我甚至在我的游戏节点
中也遇到了这个问题,我点击形状,这个延迟就在他的位置上,没有切换任何节点。我已经尝试过了,但我遇到了问题。但是如果游戏变成背景,我不想播放声音。@NurassylNuridin,他指的是背景线程。这与后台的应用程序不同。你的应用程序通常在几个线程上运行,你甚至都没有注意到。例如,大多数SKActions在单独的线程中运行。使用后台线程有助于将非时间关键型代码与FPS依赖的代码分开。然而,正如我已经指出的,我认为问题与切换场景有关,而不是与播放声音有关。例如,我认为播放声音与设置AVAudioPlayers相比并不太昂贵。当应用程序不再处于活动状态时,游戏是否播放声音由AVSession控制。在我用后台线程(主要是在旧版iPad上)玩AVAudioPlayer之前,我在自己的游戏中有过一些短时间的渲染,所以你应该试试,以防你和我有同样的问题。我会试试这个!谢谢@AlainT。这对我有帮助!