使用Sprite Kit和swift 2按下按钮时如何更改场景

时间:2016-01-25 03:16:06

标签: ios sprite-kit scene

我是swift的新手,在阅读本教程http://www.raywenderlich.com/66877/how-to-make-a-game-like-candy-crush-part-1后尝试添加一些额外的功能。我想做的是结合数字宠物的元素,就像iOS游戏“最好的朋友”。首先,我添加一个主场景(picture of main scene)和几个按钮,当按下按钮时,场景会改变。以下是我用来实现它的代码。 MainSceneController.swift:

import UIKit
import SpriteKit

class MainSceneController: UIViewController {

    @IBOutlet weak var settingButton: UIButton!
    @IBOutlet weak var storeButton: UIButton!
    @IBOutlet weak var petsButton: UIButton!
    @IBOutlet weak var levelButton: UIButton!

    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    override func shouldAutorotate() -> Bool {
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let mainScene = MainScene(size:CGSize(width: 320, height: 568))
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true
        mainScene.scaleMode = .AspectFill
        skView.presentScene(mainScene)
    }

    @IBAction func changeScene(sender: UIButton) {
        let labelText = sender.titleLabel?.text!
        print(labelText)
        switch (labelText!) {
            case "LevelButton":
                let scene = LevelScene(size:CGSize(width: 320, height: 568))
                let reveal = SKTransition.fadeWithDuration(1)
                scene.scaleMode = SKSceneScaleMode.AspectFill
                scene.view?.presentScene(scene, transition: reveal)
            case "PetsButton":
                let scene = PetsScene(size:CGSize(width: 320, height: 568))
                let reveal = SKTransition.fadeWithDuration(1)
                scene.scaleMode = SKSceneScaleMode.AspectFill
                scene.view?.presentScene(scene, transition: reveal)
            default:
                break
        }

    }

}

import SpriteKit

class PetsScene: SKScene {

    override init(size: CGSize) {
    super.init(size: size)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func didMoveToView(view: SKView) {
        backgroundColor = SKColor.redColor()
        let mylabel = SKLabelNode(fontNamed: "Chalkduster")
        mylabel.text = "PetsScene"
        mylabel.fontSize = 70
        mylabel.position = CGPoint(x: self.frame.size.width / 2, 
                                   y: self.frame.size.height / 2)
        self.addChild(mylabel)
    }

}

当我按下按钮时,场景不会改变。我也用segue来实现它。但似乎不是我想要的。我已经找了一些项目源代码,但我找不到类似的。如果提前感谢,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我不确定这是否也是你要找的,但它是我用来在场景之间转换的方法。我不使用Segues。我不知道为什么,除此之外,我从来没有费心去学习如何使用它们以便默认它们。我甚至很少使用故事板。因此下面的代码是如何在代码中从场景转换到另一个场景的示例。我通常有一个GlobalFunctions.swift文件,它包含许多类共享的函数,以下文件将在这样的文件中:

func transitionToScene(node: String, sendingScene: SKScene) {

    //println("Entering transition to scene method with \(node) and \(sendingScene.name)")

    let transDur = 1.5
    let transition = SKTransition.fadeWithColor(sendingScene.backgroundColor, duration: transDur)

    var scene = SKScene()
    switch node {
        case splashSceneString:
            scene = SplashScene(size: sendingScene.size)
        case gameSceneString:
            scene = GameScene(size: sendingScene.size)
        case optionsSceneString:
            scene = OptionsScene(size: sendingScene.size)
        case menuSceneString:
            scene = MenuScene(size: sendingScene.size)
    default:
        if general_debug == true { print("Default should not be reached. No Scene?") }
    }

    scene.scaleMode = SKSceneScaleMode.AspectFill
    sendingScene.view!.presentScene(scene, transition: transition)

}

所以这个func会被某个事件调用。例如,如果您触摸游戏/应用程序中的按钮,touchesEnded:函数将执行以下操作:

transitionToScene("optionsSceneString", sendingScene: self.scene)

希望这会有所帮助。祝你好运!