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

查看:180
本文介绍了在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天全站免登陆