如何在swift中点击颜色精灵后返回上一个场景?

时间:2017-05-19 21:36:30

标签: ios swift button sprite-kit scene

因此,对于一个学校项目,我的任务是制作2D游戏。游戏很好,但我正在努力制作一个后退按钮(在页面中间),所以想知道是否有特定的代码使这项工作。我正在使用spriteKit,所以我在点击颜色精灵后试图回到上一个场景。

如果这是一个愚蠢的问题我很抱歉,但我对Swift稍微有些新意。

亲切的问候, 詹姆斯

2 个答案:

答案 0 :(得分:1)

以下是如何使用彩色精灵创建按钮的示例。它显示了如何设置按钮以接收触摸事件以及如何使用这些触摸事件在场景之间导航。

在此示例中,您可以向前导航到新场景并向后导航到之前的场景。

import SpriteKit

class Button: SKSpriteNode {

    var tapped: (() -> Void)?

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        tapped?()
    }

}

class GameScene: SKScene {

    var parentScene: SKScene?
    var sceneCount = 1

    override func didMove(to view: SKView) {
        if parentScene != nil {
            let backButton = addButton(color: .red, position: CGPoint(x: -200, y: 0))
            backButton.tapped = {
                if let previousScene = self.parentScene {
                    view.presentScene(previousScene)
                }
            }
        }

        let nextButton = addButton(color: .blue, position: CGPoint(x: 200, y: 0))
        nextButton.tapped = {
            if let nextScene = SKScene(fileNamed: "GameScene") as? GameScene {
                nextScene.scaleMode = self.scaleMode
                nextScene.parentScene = self
                nextScene.sceneCount = self.sceneCount + 1
                view.presentScene(nextScene)
            }
        }

        let label = SKLabelNode(text: "Scene \(sceneCount)")
        addChild(label)
    }

    func addButton(color: SKColor = .white, position: CGPoint = .zero) -> Button {
        let button = Button(color: color, size: CGSize(width: 200, height: 200))
        button.position = position
        button.isUserInteractionEnabled = true
        addChild(button)
        return button
    }

}

答案 1 :(得分:0)

添加一个按钮,最简单的方法是检测相关SKScene中精灵的触摸。

// Serial port class
#include "GUIApp.h "
class MySerial
{

public:
//..............
//write something on GUI`

对于更可重用的解决方案,理想情况下,您需要创建一个按钮子类。谷歌有很多教程如何做到这一点。

要在SKScenes之间进行转换,您可以在每个场景中创建一个loadScene方法,然后在必要时调用它们。

enum NodeName: String {
   case coloredSprite1
   case coloredSprite2
}

class GameScene: SKScene {

     let coloredSprite = SKSpriteNode(imageNamed: "YourImageName")

     /// Scene setup
     override func didMove(to view: SKView) {
       // set up your colored sprite if necessary
       // Give your sprites unique names to identify them

       coloredSprite.name = NodeName.coloredSprite1.rawValue // always use enums for things like string identifiers so you avoid typos
     }

     /// Touches
     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
         for touch in touches {
             let location = touch.location(in: self)
             let touchedNode = atPoint(location)

             // Way 1 by node (probably less preferable)
             switch touchedNode {
             case coloredSprite:
                 // do something (e.g call loadScene method)
                // see below
             default: 
                 break
             }

             // Way 2 by node name (probably more preferable)
             // name is an optional so we have to unwrap it when using it in the switch statement. 
             // The easiest way is by providing an alternative string, by using the nil coalescing operator (?? "NoNodeNameFound")

             switch touchedNode.name ?? "NoNodeNameFound" {
             case NodeName.coloredSprite1.rawValue:
                // do something (e.g call loadScene method)
                // see below
             default: 
                break
             }
        }
    }

    // Also call touchesEnded, touchesMoved and touchesCancelled and do necessary stuff
}

希望这有帮助