gpt4 book ai didi

swift - 如何在 Sprite Kit 中切换场景?

转载 作者:行者123 更新时间:2023-11-28 07:09:08 24 4
gpt4 key购买 nike

我的代码有什么问题?每次我按下按钮时,我都会得到一个默认的灰色屏幕。过渡按钮:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.pause {
var pauseScene = PauseScene(size: self.size)
scene?.paused = true
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
pauseScene.scaleMode = .AspectFill
pauseScene.size = skView.bounds.size
skView.presentScene(pauseScene)
}
}
}

我在那个场景中不想加载的内容:

class PauseScene: SKScene {
let pauseBackground = SKSpriteNode (imageNamed: "pauseBackground")

override func didMoveToView(view: SKView) {
self.pauseBackground.anchorPoint = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.pauseBackground.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.addChild(pauseBackground)
}

}

我错过了什么吗?

最佳答案

看起来您的问题出在您的 PauseScene 中的 didMoveToView: 中。您不想更改 self.pauseBackground 的 anchor 通常 anchor 基于 0 到 1。(.5,.5) 是 Sprite 的中心。您将其设置为比该值高得多的值,并且如果您将位置设置为场景宽度和高度的一半,我根本不明白您为什么要更改 anchor 。看起来只需删除代码行即可解决问题。

class PauseScene: SKScene {
let pauseBackground = SKSpriteNode (imageNamed: "pauseBackground")

override func didMoveToView(view: SKView) {
self.pauseBackground.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.addChild(pauseBackground)
}
}

我还看到您在初始化场景后正在更改场景的大小。我会用你想要的大小初始化它。

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.pause {
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
var pauseScene = PauseScene(size: skView.bounds.size)
scene?.paused = true
pauseScene.scaleMode = .AspectFill
skView.presentScene(pauseScene)
}
}
}

希望这对您有所帮助并且有意义。

关于swift - 如何在 Sprite Kit 中切换场景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29186414/

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