0

我刚刚更新到 xCode beta 6。在 beta5 中一切正常,但在 beta 6 中必须改变一些东西。我加了很多“!” 在我的代码中。无论如何,我的项目是小游戏。完成关卡(赢或输)后,我想调用 skscene。

代码在 mainviewController 中:

if(flag==true){//  IF we WON
            /* That below lines should call "Won.swift" file But it doesn't */
            var scene = Won(fileNamed: "GameScene")
            let skView = self.view as SKView
            skView.showsFPS = true
            skView.showsNodeCount = true
            skView.ignoresSiblingOrder = true
            scene.scaleMode = SKSceneScaleMode.AspectFill
            scene.position = CGPointMake(0, 568)
            skView.presentScene(scene)
        }

Won.swift 文件是:

class Won: SKScene {

    var audioPlayer = AVAudioPlayer()

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Fireworks", ofType: "mp3")!)

        // Removed deprecated use of AVAudioSessionDelegate protocol
        AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
        AVAudioSession.sharedInstance().setActive(true, error: nil)

        var error:NSError?
        audioPlayer = AVAudioPlayer(contentsOfURL: alertSound2, error: &error)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
        let myLabel = SKLabelNode(fontNamed:"Chalkduster")
        myLabel.text = "You Win";
        myLabel.fontSize = 65;
        myLabel.fontColor = UIColor.redColor()
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
        self.addChild(myLabel)
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }

}

此代码在 beta 5 中有效,但在 beta6 中无效。此外,我将 uikit、avfoundation、spritekit 基础重新添加到项目中。并检查媒体文件“mp3”或图像是否存在于捆绑资源中......该项目运行完美,没有任何错误或警告。但是 Skscene 部分不起作用。我还尝试设置断点来跟踪它的运行方式。该过程继续进行,但从未进入“Won.swift”文件。

有什么建议吗?谢谢你。

4

1 回答 1

0

听起来您的变量“标志”没有正确设置为真。您是否尝试在该代码中放入一些 println() 以查看该代码是否实际被调用?

另外,你为什么把它放在 mainViewController 中?把它放在游戏场景中,这样当你移动到不同的场景时,场景就会受到控制。您的主视图控制器应该(可能)只启动第一个场景,并让场景逻辑处理过渡到其他场景。

实际上,这可能是您的问题 - 如果该“标志”设置为 true,并且该函数被重复调用,例如,每一帧,那么该文件将被重复调用。或者,如果您在其中将 flag 设置回 false,并且该代码在其他地方被执行,它可能正在调用您的另一个场景文件,并立即切换回它,这样您就永远不会看到“Won”场景。

可能是很多东西。开始洒一些 println() 并设置断点以弄清楚到底发生了什么。

于 2014-08-22T16:32:16.897 回答