触摸按钮无法加载新的SpriteKit SKScene

时间:2018-02-07 08:53:38

标签: ios swift sprite-kit uitouch

我无法通过触摸场景上的按钮加载新场景。 按钮名为:BuStartGame 要加载的场景:CategoriesScene 加载类的场景:CategoriesSceneClass。

这是我的代码。

import Foundation
import SpriteKit
import GameplayKit
import UIKit

class GameScene: SKScene {

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

            //Mark: ===================== StartGame
            if atPoint(location).name == "BuStartGame"{
                if let scene = CategoriesSceneClass(fileNamed:"CategoriesScene"){
                    scene.scaleMode = .aspectFill
                    view!.presentScene(scene, transition: SKTransition.doorsOpenVertical(withDuration: 2))

                }
            }
        }
    }
}

EDITED: 我看到问题出在这段代码中。当我删除I​​F它工作,但作为一个按钮工作整个场景。

if atPoint(location).name == "BuStartGame"{

2 个答案:

答案 0 :(得分:0)

我去年在SpriteKit的演示游戏中苦苦挣扎,这里有一些我用于类似按钮转换的代码,更改了类名和文件名到你的下面。尝试在你的&#34中替换它;如果让场景&#34;部分。

if let scene = CategoriesSceneClass(fileNamed:"CategoriesScene") {

            // Configure the view.
            let skView = self.view!
            skView.showsFPS = true
            skView.showsNodeCount = true


            /* Sprite Kit applies additional optimizations to improve rendering performance */
          skView.ignoresSiblingOrder = false 


            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .AspectFill
            skView.presentScene(scene, transition: SKTransition.doorsOpenVertical(withDuration: 2))

答案 1 :(得分:0)

此代码解决了问题

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {  
        for touch in touches {   
            let location = touch.location(in: self);  
            if atPoint(location).name == "Start" {   
                    print("I'm at start point")  
                if let view = self.view {  
                    print("gonna create scene")  
                    if let scene = GameplaySceneClass(fileNamed: "GameplayScene") {  
                        scene.scaleMode = .aspectFill  
                        print("prepare to transition")  
                        view.presentScene(scene,transition:  
                                SKTransition.doorsOpenVertical(withDuration:  
                                TimeInterval(2)));  
                    }  
            }  
        }  
    }