【问题标题】:Cannot load new SpriteKit SKScene with touch of the button触摸按钮无法加载新的 SpriteKit SKScene
【发布时间】:2018-02-07 08:53:38
【问题描述】:

我无法通过触摸场景上的按钮来加载新场景。 按钮名称: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))

                }
            }
        }
    }
}

编辑: 我看到问题出在这段代码中。当我删除 IF 时,它可以工作,但作为一个按钮可以在整个场景中工作。

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

【问题讨论】:

  • 如果你单步调试你的代码,它会调用presentScene吗?
  • 看起来很奇怪,但是是的。我在现场还有另一个按钮。有的时候在模拟器上渲染,有的时候没有……

标签: ios swift sprite-kit uitouch


【解决方案1】:

去年我在 SpriteKit 中的演示游戏中遇到了困难,这是我用于类似按钮转换的一些代码,在下面将类名和文件名更改为您的。尝试在“如果让场景”部分替换它。

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))

【讨论】:

  • 不幸的是它不起作用。我看到问题出在这段代码中。当我删除 IF 时,它可以工作,但作为一个按钮可以在整个场景中工作。如果 atPoint(location).name == "BuStartGame"{
【解决方案2】:

这段代码解决了问题

  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)));  
                    }  
            }  
        }  
    }

【讨论】:

    猜你喜欢
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多