Swift: Spritekit 中的多个场景,touchesBegan 从不同的 Sprite 节点类型在第二个和第一个场景中被调用

Swift: Multiple scenes in Sprite Kit, touchesBegan from different SpriteNodes types on 2nd and 1st scene are called

我在 Sprite Kit 中使用多个场景时遇到问题 (Swift)。我设置了 3 个场景(游戏场景、首选项和帮助场景)。我定义了两个不同的 SpriteNode classes,称为 CardItem 和 MenuItem。虽然我只在游戏场景中使用 CardItem,但我在所有三个场景中都使用了 MenuItem。因此,当我在游戏场景(标准场景)中触摸 CardItem SpriteNode 时,只会调用 class(在游戏场景中)的 touchesBegan 函数。但是当我跳入首选项或帮助场景(从游戏场景)并触摸 SpriteNode MenuItem 时,不仅会调用它们的 touchesBegan 函数,还会调用游戏场景中 CardItem 类型的 touchesBegan! (在后面仍然可用)被调用。

我现在的问题是,这是我的应用程序中的功能还是错误?

这里是我的GameScene Class 场景初始化

class GameScene: SKScene, PreferencesSceneDelegate, HelpSceneDelegate {
var prefView: SKView?
var helpView: SKView?
var activeGame: Game?
var gamePrefs =  gamePreferences()
var prefManager: PreferencesManager!

func prefSceneDidFinish(myScene: PreferencesScene, command: String) {

    // Remove PreferencesScene from Screen

    if (command == "Abbruch") {
        myScene.view!.removeFromSuperview()
        setButtonTexture(command)
    } else if command == "Done" {
        myScene.view!.removeFromSuperview()
        activeGame!.setupNewGame(self, gamePrefs: self.gamePrefs, prefManager: self.prefManager, command: 1)
    } else {
        println("prefSceneDidFinish: command \(command) not specified")
    }
}


func helpSceneDidFinish(myScene: HelpScene) {

    // Remove HelpScene from Screen
    myScene.view!.removeFromSuperview()
}


override func didMoveToView(view: SKView) {
    let background = Card(cardType: .background)
    background.cardID = CardName.background.rawValue
    background.anchorPoint = CGPoint(x: 0.5, y: 1.0)
    background.position = CGPoint(x: size.width/2, y: size.height)
    background.zPosition = -2
    background.userInteractionEnabled = false
    self.addChild(background)

    //
    // Preferences Initialization
    //
    self.prefView = SKView(frame: CGRectMake(0, 0, self.frame.width, self.frame.height))

    // Create and configure PreferencesScene
    prefscene = PreferencesScene(size: view.bounds.size)
    prefscene.scaleMode = .AspectFill
    prefManager = PreferencesManager(prefs: gamePrefs)
    tmpSeriesSelection = gamePrefs.inScope // Initialize after data load fromn file

    self.prefView!.presentScene(prefscene)
    prefscene!.thisDelegate = self

    // 
    // Help Initialization
    //
    self.helpView = SKView(frame: CGRectMake(0, 0, self.frame.width, self.frame.height))

    // Create and configure HelpScene
    helpscene = HelpScene(size: view.bounds.size)
    helpscene.scaleMode = .AspectFill

    self.helpView!.presentScene(helpscene)
    helpscene!.thisDelegate = self
    var helpSceneBackground = SKSpriteNode(imageNamed: "HelpSceneBackground.png")
    helpSceneBackground.anchorPoint = CGPoint(x: 0.5, y: 1.0)
    helpSceneBackground.position = CGPoint(x: size.width/2, y: size.height)
    helpSceneBackground.zPosition = 0
    helpSceneBackground.userInteractionEnabled = false
    helpscene.addChild(helpSceneBackground)


    activeGame = Game(scene: self, gamePrefs: gamePrefs, prefManager: prefManager)
}

这是我调用场景的代码片段 (MenuItem touchesBegan)

    case .help :
        println("Pressed Help")
        var transition = SKTransition.fadeWithDuration(Double(0.0))
        self.scene!.view?.addSubview(gamescene.helpView!)
    case .preferences :
        …
       var transition = SKTransition.fadeWithDuration(Double(0.0))
        self.scene!.view?.addSubview(gamescene.prefView!)
    case .ok :
        println("OK Selektion")
        …
       gamescene.prefManager.save(gamescene.gamePrefs)
        prefscene.thisDelegate!.prefSceneDidFinish(prefscene, command: "Done")

在此先感谢您的帮助

当你的场景创建自己的 SKView 实例时,你一定会 运行 遇到问题,这些实例会呈现场景,创建视图,创建场景等。所以,跳过你的 prefViewhelpView属性,使用场景的view属性一个接一个呈现你的场景。

view?.presentScene(scoreScene)

如果(出于某种原因)您需要保留 (1) 不同的场景实例,而是在您的 GameViewController 中处理此问题。

(1) 在大多数情况下你可能不应该...