gpt4 book ai didi

ios - 以编程方式完成时,SpriteKit 中没有任何内容加载到屏幕

转载 作者:可可西里 更新时间:2023-11-01 01:57:49 25 4
gpt4 key购买 nike

我不明白为什么这行不通,而且似乎场景没有改变,因为背景颜色没有变成红色。这是我的代码:

GameViewController.swift

class GameViewController: UIViewController {

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

let skView = self.view as! SKView
if skView.scene == nil {

// Set options
skView.ignoresSiblingOrder = true
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsPhysics = true

// Create a scene
let gameScene = SKScene(size: skView.bounds.size)

// Set the scale mode to scale to fit the window
gameScene.scaleMode = .aspectFill

// Present the scene
skView.presentScene(gameScene)
}
}

override var shouldAutorotate: Bool {
return true
}

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

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}

override var prefersStatusBarHidden: Bool {
return true
}
}

GameScene.swift

class GameScene: SKScene {

// Create objects
var player = SKSpriteNode()

// Initialize
override init(size: CGSize) {
super.init(size: size)

//Other init code
player.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
player.setScale(0.5)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func didMove(to view: SKView) {
self.backgroundColor = UIColor.red
self.addChild(player)
}
}

屏幕上没有加载任何内容,它只是黑色/灰色。有什么想法吗?

最佳答案

您没有在代码中的任何位置指定要打开 GameScene。

// Create a scene
let gameScene = SKScene(size: skView.bounds.size)

如果您没有在场景编辑器中创建相应的 SKS 文件,而是希望加载在代码中创建的场景,请使用...

它应该是什么......

// Create a scene
let gameScene = GameScene(size: skView.bounds.size)

关于ios - 以编程方式完成时,SpriteKit 中没有任何内容加载到屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49956244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com