在 Swift SpriteKit 项目中播放声音? [英] Play Sound in Swift SpriteKit project?

查看:11
本文介绍了在 Swift SpriteKit 项目中播放声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行这段代码时.

runAction(SKAction.playSoundFileNamed("Click.mp3", waitForCompletion: false))

我的应用崩溃了:

由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:在主包中找不到资源 Click.mp3"

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Resource Click.mp3 cannot be found in the main bundle'

推荐答案

验证文件是否存在于项目中.先实例化文件中的变量,然后再做一个育种方法.

verify that the file exists in the project. instantiates the variables in the file first and then make a method for breeding.

如果项目中存在该文件,您应该执行以下操作:

if the file exists in the project you should do the following:

var sound = SKAction.playSoundFileNamed("sound.mp3", waitForCompletion: false)
...
override func didMoveToView(view: SKView) {
    playSound(sound)
    //call playSound method when you want
}

func playSound(sound : SKAction)
{
    runAction(sound)
}

在这种模式下不会崩溃

这篇关于在 Swift SpriteKit 项目中播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆