シンプルなスプライトキットゲームを作成する。そこには、ボタンライブラリに触れたような音などがあります。AVFoundation
サウンドライブラリ。しかし、これに問題があります。いずれのボタンを押すとフレームレートが常に下がります。しかし、私がすべての音を消しても、遅れはありません。私はプリロードすべての私のサウンドにGameAudio.shared.setupSounds()
を呼び出す私のGameViewControllerで私のspritekitゲームの遅れを理解できませんか?
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()
}
}
:ここに私のコードです。
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")
}
}
私はゲーム内のすべての行動に対してstruct Actions
を使用します。私はbuttonTouchedAction()
を持っています、私は別のノードでそれを使用します。私は1つの "再生"ボタンで私のメニューからいくつかのコードを入れました。たとえば、私はゲームでゲームを実行し、メニューノードを表示し、数秒待ってからボタンをタップすると、フレームレートが低下した後、1秒後にラグがあり、シーンがゲームシーンに変化します。
ボタンが「ヒット」したときに実行されるコードを投稿できますか? – Stoyan
ストイヤンを終えました。追加コードで編集。 –
私のサウンドに4つの 'AVAudioPlayer()'を使うのではなく、 'GameAudio'に悪いコードの実装がありますか?これは大丈夫ですか? –