gpt4 book ai didi

swift - SpriteKit - SpriteNode 从不显示

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

我正在尝试学习 SpriteKit。我想在代码中向场景添加一个矩形/正方形;但该项目从未显示;

我只想在屏幕上添加一个白色方 block ,但发现它永远不会添加。

在断点处,我注意到 didMove() 似乎从未被调用过。

我做错了什么?

class GameScene: SKScene {

override func didMove(to view: SKView) {
let item = SKSpriteNode(color: .white, size: CGSize(width: 150, height: 200))
item.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(item)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}

更新

我没有更改 xcode 生成的 View Controller

class GameViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill

// Present the scene
view.presentScene(scene)
}

view.ignoresSiblingOrder = true

view.showsFPS = true
view.showsNodeCount = true
}
}

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.sks,我删除了这个文件,因为我不想使用sks文件

最佳答案

如果从未到达 didMove(to view:) 中的断点,那么肯定是您的 ViewController 出于某种原因没有呈现场景。你能打开你的 ViewController 的文件(例如 GameViewController.swift),看看你那里是否有任何这些行(或任何类似的东西):

let viewSize = UIScreen.main.bounds.size
let scene = GameScene(size: viewSize)
let skView = self.view as! SKView
skView.presentScene(scene)

关于swift - SpriteKit - SpriteNode 从不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42877934/

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