ios - 无法通过触摸按钮加载新的 SpriteKit SKScene

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

                }
            }
        }
    }
}

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

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

最佳答案

去年我在 SpriteKit 中玩了一个演示游戏,这里是一些我用于类似按钮转换的代码,下面将类名和文件名更改为你的。尝试在“if let scene”部分中替换它。

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

关于ios - 无法通过触摸按钮加载新的 SpriteKit SKScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48659503/

相关文章:

iphone - 将 HEX NSString 转换为 NSData

ios - App Store Receipt 有时会返回意外的收据

ios - 在 Swift 中完成下载后调度?

ios - 将对象保持在屏幕内。 swift SpriteKit

swift - SpriteKit 通过物理移动节点而不是更新位置

ios - 核心数据轻量级迁移与 NSManagedObject 子类和关系

ios - 如何设置 Action 以在ios中返回键

swift - 无法解锁 "UITabBarController.h"以进行编辑

ios - AudioKit 问题,麦克风不够灵敏

ios - SKShapeNode : How to set String to name of SKShapeNode