You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tutorial SKScene中SKLabel无法显示的问题排查求助

问题排查:Tutorial场景代码添加的SKLabel不显示

问题描述

在Tutorial SKScene中,通过代码添加的SKLabel无法正常显示,但手动添加该标签时却能正常工作。

相关代码

GameViewController

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

Start场景

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!")
                
                //Go to Tutorial
                if let tutorialScene = SKScene(fileNamed: "Tutorial") {
                    tutorialScene.scaleMode = .fill
                    
                    // Present the TutorialScene
                    self.view?.presentScene(tutorialScene)
                }
            }
        }
    }
}

Tutorial场景

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

排查方向及修复建议

  1. 场景尺寸不匹配
    Tutorial场景通过SKScene(fileNamed: "Tutorial")加载,自身尺寸可能与屏幕实际尺寸不一致。用self.size.height计算位置时,若尺寸不符会导致label被放在屏幕可见范围外。

    • 修复:打印self.size确认尺寸,或改用view.bounds.size计算位置:
      guard let skView = view else { return }
      labelNode.position = CGPoint(x: skView.bounds.midX, y: skView.bounds.maxY - topMargin)
      
  2. 缩放模式冲突
    跳转Tutorial场景时用了.fill缩放模式,而Start场景用的是.aspectFill,会导致场景拉伸、位置计算偏差。

    • 修复:将Tutorial的缩放模式改为与Start一致:
      tutorialScene.scaleMode = .aspectFill
      
  3. 字体设置缺失
    SKLabelNode默认字体可能在部分场景下加载异常,建议显式设置字体名称:

    labelNode.fontName = "Arial" // 或其他系统支持的字体
    
  4. 颜色与背景冲突
    若Tutorial场景背景为黑色,label字体颜色也设为黑色会导致无法看见文字,可临时改为红色测试:

    labelNode.fontColor = .red
    

内容的提问来源于stack exchange,提问作者Matt Cantrelle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 03:27:08