升级到XCode7.2之后SKAudioNode只播放1秒 - 可能是SKScene的问题?

时间:2015-12-13 10:26:03

标签: ios xcode swift sprite-kit tvos

升级到XCode 7.2之后我遇到一个问题,即SKAudioNode只播放一秒然后停止播放。我在代码中没有改变任何内容。

在我的GameViewController中,我将MenuScene称为:

jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(
    3000, 
    0, 
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

//Adding the request to queue
BaseController.getInstance().addToRequestQueue(jsObjRequest);

在我的MenuScene中,我将我的GameScene称为:

class GameViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let scene = MenuScene(size: view.bounds.size)
        let skView = view as! SKView

        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true

        scene.scaleMode = .ResizeFill
        skView.presentScene(scene)

    }...

然后在我的GameScene中添加一个SKAudioNode:

func launchScene() {
        let gameView = view! as SKView
        let gameScene = GameScene(size: self.size)

        gameView.ignoresSiblingOrder = true

        let reveal = SKTransition.fadeWithDuration(0.5)

        gameView.presentScene(gameScene, transition:reveal)
    }

所以,问题是当我点击调用launchScene()函数的Button时,背景音乐开始播放但在大约1秒后停止播放。

编辑:似乎backgroundMusic之前开始播放!转换到另一个场景开始,当另一个场景(gameScene)“终于在那里”(不知道如何描述它)时,音乐停止播放。我不知道为什么,因为我在gameScene中的“didMoveToView”函数中添加了backgroundMusic。

我在这里做错了什么,因为它在XCode 7.1中完美运行?

1 个答案:

答案 0 :(得分:3)

在我的带有场景转换的项目中,为声音添加甚至0.1秒的延迟似乎已经解决了问题。如

internal static class DictionaryHelper
{
    /// <summary>
    /// Returns a string of key value pairs in the format k1=v1,k2=v2,...
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    /// <param name="dictionary"></param>
    /// <returns></returns>
    public static string ToKeyValueString<TKey, TValue>(this Dictionary<TKey, TValue> dictionary)
    {
        return ToKeyValueString(dictionary, "{0}={1}", ",");
    }

    /// <summary>
    /// Returns a string of key value paris in the format k1=v1{separator}kk2=v2...
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    /// <param name="dictionary"></param>
    /// <param name="seperator">The string separator for the pairs</param>
    /// <returns></returns>
    public static string ToKeyValueString<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, string seperator)
    {
        return ToKeyValueString(dictionary, "{0}={1}", seperator);
    }

    /// <summary>
    /// Returns a string of key value pairs in the specified format with the specified separator
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    /// <param name="dictionary"></param>
    /// <param name="format">The format string for the key value pairs</param>
    /// <param name="separator">The string separator for the pairs</param>
    /// <returns></returns>
    public static string ToKeyValueString<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, string format, string separator)
    {
        var pairs = dictionary.Select(c => string.Format(format, c.Key, c.Value));
        return string.Join(separator, pairs);
    }
}