首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检测第一个场景中的轻击: SpriteKit Swift iOS,有小问题

检测第一个场景中的轻击: SpriteKit Swift iOS,有小问题
EN

Stack Overflow用户
提问于 2020-10-01 01:35:30
回答 1查看 85关注 0票数 0

因此,我的应用程序中有一个菜单屏幕。Swift,SpriteKit,iOS 7-10,11,12,13.

我只想让用户点击我屏幕上的按钮,然后让它将他们移动到另一个屏幕上。这在我的代码中的其他引用中是有效的。然而,我所做的只是将GameViewController中的第一个加载场景更改为我自己的swift文件,代码如下:

代码语言:javascript
复制
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文件,这样它就可以在应用程序中首先加载这些代码。但是,它不会检测到触摸。谢谢你的帮助!

EN

回答 1

Stack Overflow用户

发布于 2020-10-01 05:11:26

看起来您缺少为您的节点设置名称的代码。确保在didMove函数中设置节点的名称并将其添加到场景中。

代码语言:javascript
复制
//inside scene
override func didMove(to view: SKView) {
    play.name = "play"
    self.addChild(play)
}

您必须确保的另一件事是在项目中创建了SpriteKit场景文件,并将自定义类设置为FrontMenu

至于在GameViewController中切换文件,您应该只使用文件的名称,而不是fileNamed:的文件扩展名

代码语言:javascript
复制
//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)
            }
        }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64142908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档