我在GameScene完成后创建了一个主菜单场景视图。 GameScene本身运作良好。但是,在从主菜单场景视图中传输后,它不会显示在中心。两个sks文件都具有相同的宽度和高度尺寸和锚点。那是为什么?
class Menu: SKScene {
var newGameButtonNode: SKSpriteNode?
var bossFightButtonNode: SKSpriteNode?
var highScoreLabelNode: SKLabelNode?
override func didMove(to view: SKView) {
newGameButtonNode = self.childNode(withName: "newGameButton") as? SKSpriteNode
bossFightButtonNode = self.childNode(withName: "bossFightButton") as? SKSpriteNode
highScoreLabelNode = self.childNode(withName: "highScoreLabel") as? SKLabelNode
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//set touch to self
let touch = touches.first
if let location = touch?.location(in: self) {
let nodesArray = self.nodes(at: location)
if nodesArray.first?.name == "newGameButton" {
let transition = SKTransition.flipHorizontal(withDuration: 0.5)
let gameScene = GameScene(size: self.size)
gameScene.scaleMode = .aspectFill
self.view?.presentScene(gameScene, transition: transition)
}
}
}
}
答案 0 :(得分:1)
如果您在场景编辑器中创建了相应的SKS文件,那么您可以加载场景......
// Load the SKScene from GameScene.sks'
if let GameScene = GameScene(fileNamed: "GameScene") {
gameScene.scaleMode = .aspectFill
self.view?.presentScene(GameScene, transition)
}
如果您没有在场景编辑器中创建相应的SKS文件,而是希望加载在代码中使用创建的场景......
GameScene = GameScene(size: self.view?.bounds.size)
gameScene.scaleMode = .aspectFill
self.view?.presentScene(gameScene, transition)
你说两者都有相应的SKS文件但是你试图通过场景中的代码init加载它