【发布时间】:2014-12-08 19:17:37
【问题描述】:
我是一位经验丰富的程序员,但对 iOS 和 Swift 不熟悉。我已经建立了一个带有声音的俄罗斯方块游戏教程。我正在尝试重构代码并将声音例程移至指定的类。正在调用新类中的函数,但不再播放声音。
原始代码(作品):
GameScene.swift
import SpriteKit
...
class GameScene: SKScene {
...
runAction(SKAction.repeatActionForever(SKAction.playSoundFileNamed("theme.mp3", waitForCompletion: true)))
}
新代码(没有声音播放):
声音.swift
import SpriteKit
class Sounds: SKNode {
required init(coder aDecoder: NSCoder) { fatalError("NSCoder not supported") }
override init() { super.init() }
func playSoundtrack() {
println( "Sounds: playSoundtrack: ")
runAction(SKAction.repeatActionForever(SKAction.playSoundFileNamed("theme.mp3", waitForCompletion: true)))
}
}
将上面的GameScene代码修改如下:
class GameScene: SKScene {
var soundEngine: Sounds = Sounds()
...
soundEngine.playSoundtrack()
}
我确认 println 已执行,但没有声音播放。有什么想法吗?
【问题讨论】:
-
您在哪个对象/节点上调用
runAction?您在控制台中遇到任何错误吗? -
这是问题所在,不是在节点上运行它。控制台没有错误。将类添加到场景中并且它可以工作。谢谢!
标签: ios swift sprite-kit