因此,我的应用程序中有一个菜单屏幕。Swift,SpriteKit,iOS 7-10,11,12,13.
我只想让用户点击我屏幕上的按钮,然后让它将他们移动到另一个屏幕上。这在我的代码中的其他引用中是有效的。然而,我所做的只是将GameViewController中的第一个加载场景更改为我自己的swift文件,代码如下:
import Foundation
import SpriteKit
import GameplayKit
import UIKit
class FrontMenu : SKScene {
var play = SKSpriteNode(imageNamed: "pbut")
var credit = SKSpriteNode()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let node : SKNode = self.atPoint(location)
if node.name == "play" {
print("play tapped.")
// this is where I would have placed the transition, but this did not work.
}
else if node.name == "cred" {
NSLog("credits tapped")
}
if location.x <= -90 && location.y >= -160 {
NSLog("Area hit for play")
// This area is still not detected within the app, and there is no button function.
let gameScene = GameScene(fileNamed: "GameScene")
gameScene?.scaleMode = .aspectFill
self.view?.presentScene(gameScene!, transition: SKTransition.fade(withDuration: 0.86))基本上就是这样。您可以看到我在哪里实现了转换。我是不是错过了一个导入?我是否遗漏了对整个Plist文件进行更改的内容?或者是GameViewController.swift中的其他东西,我更改了第一个加载swift文件,这样它就可以在应用程序中首先加载这些代码。但是,它不会检测到触摸。谢谢你的帮助!
发布于 2020-10-01 05:11:26
看起来您缺少为您的节点设置名称的代码。确保在didMove函数中设置节点的名称并将其添加到场景中。
//inside scene
override func didMove(to view: SKView) {
play.name = "play"
self.addChild(play)
}您必须确保的另一件事是在项目中创建了SpriteKit场景文件,并将自定义类设置为FrontMenu
至于在GameViewController中切换文件,您应该只使用文件的名称,而不是fileNamed:的文件扩展名
//inside GameViewController
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'MneuScene.sks'
if let scene = SKScene(fileNamed: "MenuScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .resizeFill
// Present the scene
view.presentScene(scene)
}
}
}https://stackoverflow.com/questions/64142908
复制相似问题