教程 SKScene 上未显示标签

问题描述 投票:0回答:1

早上好,

我的 SKLabel 没有显示在我的教程 SKScene 上,这是一个问题。我的代码如下,我似乎找不到哪里出错了。当尝试手动添加 SKLabel 时,它工作正常。通过代码添加它时,没有任何反应。我哪里错了?

游戏视图控制器:

import UIKit
import SpriteKit
import GameplayKit

class GameViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // Configure the view
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        
        // Create and present the scene
        let scene = Start(size: skView.bounds.size)
        scene.scaleMode = .aspectFill
        skView.presentScene(scene)
        // Print the size of the scene
                print("Scene size: \(scene.size)")
        
        
         
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .allButUpsideDown
        } else {
            return .all
        }
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }
}

开始场景

import SpriteKit
import GameplayKit

class Start: SKScene {
    
    private var label : SKLabelNode?
    private var PlayButton : SKSpriteNode?
    
    
    
    
    override func didMove(to view: SKView) {
        
        // Create a label node
        let labelNode = SKLabelNode(text: "Block Maze")
        
        // Set position of the label just below the top with a fixed margin
        let topMargin: CGFloat = 100 // Adjust this value for the desired margin
        labelNode.position = CGPoint(x: self.size.width / 2, y: self.size.height - topMargin)
        
        // Add the label node to the scene
        self.addChild(labelNode)
        
        // Print the position of the label
        print("Label position: \(labelNode.position)")
        
        
        // Create a play button box
        let buttonSize = CGSize(width: 150, height: 60)
        let playButtonBox = SKShapeNode(rectOf: buttonSize, cornerRadius: 10)
        playButtonBox.fillColor = SKColor.clear
        playButtonBox.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
        
        // Create a label node for the play button text
        let playLabel = SKLabelNode(text: "Play")
        playLabel.fontColor = .white
        playLabel.fontSize = 24
        playLabel.position = CGPoint(x: 0, y: -10) // Adjust this value to position the text inside the box
        
        playButtonBox.name = "playButton" // Set the name property
        
        // Add the label node as a child of the button box
        playButtonBox.addChild(playLabel)
        
        // Add the play button box to the scene
        self.addChild(playButtonBox)
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let location = touch.location(in: self)
            
            // Check if the touch is on the play button
            if let node = self.atPoint(location) as? SKShapeNode, node.name == "playButton" {
                // Perform the action when the play button is tapped
                print("Play button tapped!")
                
                // Add your code here to perform the desired action
                
                //Go to Tutorial
                // Create and present the scene
                // Create and present the scene
                if let tutorialScene = SKScene(fileNamed: "Tutorial") {
                    tutorialScene.scaleMode = .fill
                    
                    // Present the TutorialScene
                    self.view?.presentScene(tutorialScene)
                    
                }
                
            }
            
            
        }
        
        
    }
}

教程场景

import SpriteKit
import GameplayKit

class Tutorial: SKScene {
    
    override func didMove(to view: SKView) {
        print("Tutorial scene did move to view.") // Confirm that the scene's didMove(to:) method is called
        
        // Create a label node
        let labelNode = SKLabelNode(text: "Block Maze")
        labelNode.fontColor = SKColor.black // Set label text color
        labelNode.fontSize = 24 // Set label font size
        
        // Set position of the label just below the top with a fixed margin
        let topMargin: CGFloat = 100 // Adjust this value for the desired margin
        labelNode.position = CGPoint(x: self.size.width / 2, y: self.size.height - topMargin)
        
        // Add the label node to the scene
        self.addChild(labelNode)
        
        // Print the position of the label
        print("Label position: \(labelNode.position)")
    }
    
}
swift sprite-kit uikit sklabelnode
1个回答
0
投票

教程场景需要一个大小和缩放模式,以便视图可以正确渲染它。例如,在

didMove
:

中添加以下代码
class Tutorial: SKScene {

    override func didMove(to view: SKView) {
        size = view.bounds.size
        scaleMode = .resizeFill

        // ... 
    }

}

view.bounds.size
为场景提供可见部分的大小,
.resizeFill
告诉场景的可见部分与容纳它的视图的比例相匹配。

© www.soinside.com 2019 - 2024. All rights reserved.