微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在第一个场景中检测到拍子:SpriteKit Swift iOS,有小问题

如何解决在第一个场景中检测到拍子:SpriteKit Swift iOS,有小问题

所以我的应用程序中有一个菜单屏幕。 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文件,以便它确实首先在应用程序中加载了此代码。但是,它无法检测到触摸。 谢谢您的帮助!

解决方法

您似乎缺少设置节点名称的代码。确保在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)
            }
        }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。