我正在使用主菜单创建游戏,但是当我在iOS模拟器中运行游戏时,它无法正确显示场景,如下面的屏幕截图所示。
但是,当我移动到选项菜单场景并返回主菜单场景时,它会以正确的方式显示场景。
这是主菜单的代码:
let playButton = SKSpriteNode(imageNamed: "PlayButton")
let optionButton = SKSpriteNode(imageNamed: "Options")
let facebookButton = SKSpriteNode(imageNamed: "Facebook")
let twitterButton = SKSpriteNode(imageNamed: "Twitter")
let gamecenterButton = SKSpriteNode(imageNamed: "Gamecenter")
let nosoundButton = SKSpriteNode(imageNamed: "Nosound")
let runningBar = SKSpriteNode(imageNamed: "Bar")
let character = SKSpriteNode(imageNamed: "Character")
let backgroundImage = SKSpriteNode(imageNamed: "Background")
let mountains = SKSpriteNode(imageNamed: "Mountains")
let water = SKSpriteNode(imageNamed: "Water")
let sand = SKSpriteNode(imageNamed: "Sand")
let sun = SKSpriteNode(imageNamed: "Sun")
let cloud01 = SKSpriteNode(imageNamed: "Cloud01")
let cloud02 = SKSpriteNode(imageNamed: "Cloud02")
let cloud03 = SKSpriteNode(imageNamed: "Cloud03")
override func didMoveToView(view: SKView) {
addScene()
addMenuButtons()
addSocial()
}
func addScene() {
self.backgroundImage.anchorPoint = CGPointMake(0.5, 0.5)
self.backgroundImage.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.backgroundImage.size.width = self.frame.size.width * 1.4
self.backgroundImage.size.height = self.frame.size.height * 1.4
self.backgroundImage.zPosition = 1
self.mountains.anchorPoint = CGPointMake(0.5, 0.5)
self.mountains.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.mountains.size.width = self.frame.size.width * 1.4
self.mountains.size.height = self.frame.size.height * 1.4
self.mountains.zPosition = 2
self.water.anchorPoint = CGPointMake(0.5, 0.5)
self.water.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.water.size.width = self.frame.size.width * 1.4
self.water.size.height = self.frame.size.height * 1.4
self.water.zPosition = 3
self.sand.anchorPoint = CGPointMake(0, 0)
self.sand.position = CGPointMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame))
self.sand.size.width = self.frame.size.width
self.sand.size.height = self.frame.size.height
self.sand.zPosition = 4
self.sun.anchorPoint = CGPointMake(0.5, 0.5)
self.sun.position = CGPointMake(CGRectGetMaxX(self.frame), CGRectGetMaxY(self.frame))
self.sun.size.width = 150
self.sun.size.height = 150
self.sun.zPosition = 2
self.cloud01.anchorPoint = CGPointMake(0.5, 0.5)
self.cloud01.size.width = self.frame.size.width / 3
self.cloud01.size.height = self.cloud01.size.width / 5
self.cloud01.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) - 50)
self.cloud01.zPosition = 2
self.cloud02.anchorPoint = CGPointMake(0.5, 0.5)
self.cloud02.size.width = self.frame.size.width / 3
self.cloud02.size.height = self.cloud01.size.width / 5
self.cloud02.position = CGPointMake(CGRectGetMaxX(self.frame) - 50, CGRectGetMaxY(self.frame) - 200)
self.cloud02.zPosition = 2
self.cloud03.anchorPoint = CGPointMake(0.5, 0.5)
self.cloud03.size.width = self.frame.size.width / 3
self.cloud03.size.height = self.cloud01.size.width / 5
self.cloud03.position = CGPointMake(CGRectGetMinX(self.frame) + 50, CGRectGetMaxY(self.frame) - 125)
self.cloud03.zPosition = 2
self.addChild(self.backgroundImage)
self.addChild(self.mountains)
self.addChild(self.water)
self.addChild(self.sand)
self.addChild(self.sun)
self.addChild(self.cloud01)
self.addChild(self.cloud02)
self.addChild(self.cloud03)
}
func addMenuButtons() {
self.playButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) * (3/5))
self.playButton.size.width = self.frame.size.width * (36/100)
self.playButton.size.height = self.playButton.size.width * (18/100)
self.playButton.zPosition = 5
self.optionButton.position = CGPointMake(CGRectGetMidX(self.frame), self.playButton.position.y - self.playButton.size.height - 10)
self.optionButton.size.width = self.frame.size.width * (36/100)
self.optionButton.size.height = self.optionButton.size.width * (18/100)
self.optionButton.zPosition = 5
self.nosoundButton.anchorPoint = CGPointMake(1, 0)
self.nosoundButton.position = CGPointMake(CGRectGetMaxX(self.frame) - 10, CGRectGetMinY(self.frame) + 10)
self.nosoundButton.size.height = 40
self.nosoundButton.size.width = 40
self.nosoundButton.zPosition = 5
// Add buttons
self.addChild(self.playButton)
self.addChild(self.optionButton)
self.addChild(self.nosoundButton)
}
func addSocial() {
self.twitterButton.anchorPoint = CGPointMake(0, 0)
self.twitterButton.position = CGPointMake(CGRectGetMinX(self.frame) + 10, CGRectGetMinY(self.frame) + 10)
self.twitterButton.size.height = 40
self.twitterButton.size.width = 40
self.twitterButton.zPosition = 5
self.facebookButton.anchorPoint = CGPointMake(0, 0)
self.facebookButton.position = CGPointMake(CGRectGetMinX(self.frame) + 10, self.twitterButton.position.y + self.twitterButton.size.height + 5)
self.facebookButton.size.height = 40
self.facebookButton.size.width = 40
self.facebookButton.zPosition = 5
self.gamecenterButton.anchorPoint = CGPointMake(1, 0)
self.gamecenterButton.position = CGPointMake(CGRectGetMaxX(self.frame) - 10, self.nosoundButton.position.y + self.nosoundButton.size.height + 5)
self.gamecenterButton.size.height = 40
self.gamecenterButton.size.width = 40
self.gamecenterButton.zPosition = 5
self.addChild(self.twitterButton)
self.addChild(self.facebookButton)
self.addChild(self.gamecenterButton)
}
这是选项菜单代码:
let removeAdsButton = SKSpriteNode(imageNamed: "Removeads")
let resetHighScoreButton = SKSpriteNode(imageNamed: "Resethighscore")
let creditsButton = SKSpriteNode(imageNamed: "Credits")
let backButton = SKSpriteNode(imageNamed: "BackButton")
let facebookButton = SKSpriteNode(imageNamed: "Facebook")
let twitterButton = SKSpriteNode(imageNamed: "Twitter")
let gamecenterButton = SKSpriteNode(imageNamed: "Gamecenter")
let nosoundButton = SKSpriteNode(imageNamed: "Nosound")
let runningBar = SKSpriteNode(imageNamed: "Bar")
let backgroundImage = SKSpriteNode(imageNamed: "Background")
let mountains = SKSpriteNode(imageNamed: "Mountains")
let water = SKSpriteNode(imageNamed: "Water")
let sand = SKSpriteNode(imageNamed: "Sand")
let sun = SKSpriteNode(imageNamed: "Sun")
let cloud01 = SKSpriteNode(imageNamed: "Cloud01")
let cloud02 = SKSpriteNode(imageNamed: "Cloud02")
let cloud03 = SKSpriteNode(imageNamed: "Cloud03")
var score = 0
override func didMoveToView(view: SKView) {
addScene()
addMenuButtons()
addSocial()
}
func addScene() {
self.backgroundImage.anchorPoint = CGPointMake(0.5, 0.5)
self.backgroundImage.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.backgroundImage.size.width = self.frame.size.width * 1.4
self.backgroundImage.size.height = self.frame.size.height * 1.4
self.backgroundImage.zPosition = 1
self.mountains.anchorPoint = CGPointMake(0.5, 0.5)
self.mountains.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.mountains.size.width = self.frame.size.width * 1.4
self.mountains.size.height = self.frame.size.height * 1.4
self.mountains.zPosition = 2
self.water.anchorPoint = CGPointMake(0.5, 0.5)
self.water.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.water.size.width = self.frame.size.width * 1.4
self.water.size.height = self.frame.size.height * 1.4
self.water.zPosition = 3
self.sand.anchorPoint = CGPointMake(0, 0)
self.sand.position = CGPointMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame))
self.sand.size.width = self.frame.size.width
self.sand.size.height = self.frame.size.height
self.sand.zPosition = 4
self.sun.anchorPoint = CGPointMake(0.5, 0.5)
self.sun.position = CGPointMake(CGRectGetMaxX(self.frame), CGRectGetMaxY(self.frame))
self.sun.size.width = 150
self.sun.size.height = 150
self.sun.zPosition = 2
self.cloud01.anchorPoint = CGPointMake(0.5, 0.5)
self.cloud01.size.width = self.frame.size.width / 3
self.cloud01.size.height = self.cloud01.size.width / 5
self.cloud01.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) - 50)
self.cloud01.zPosition = 2
self.cloud02.anchorPoint = CGPointMake(0.5, 0.5)
self.cloud02.size.width = self.frame.size.width / 3
self.cloud02.size.height = self.cloud01.size.width / 5
self.cloud02.position = CGPointMake(CGRectGetMaxX(self.frame) - 50, CGRectGetMaxY(self.frame) - 200)
self.cloud02.zPosition = 2
self.cloud03.anchorPoint = CGPointMake(0.5, 0.5)
self.cloud03.size.width = self.frame.size.width / 3
self.cloud03.size.height = self.cloud01.size.width / 5
self.cloud03.position = CGPointMake(CGRectGetMinX(self.frame) + 50, CGRectGetMaxY(self.frame) - 125)
self.cloud03.zPosition = 2
self.addChild(self.backgroundImage)
self.addChild(self.mountains)
self.addChild(self.water)
self.addChild(self.sand)
self.addChild(self.sun)
self.addChild(self.cloud01)
self.addChild(self.cloud02)
self.addChild(self.cloud03)
}
func addMenuButtons() {
self.removeAdsButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) - self.removeAdsButton.size.height * (2/3))
self.removeAdsButton.size.width = self.frame.size.width * (36/100)
self.removeAdsButton.size.height = self.removeAdsButton.size.width * (18/100)
self.removeAdsButton.zPosition = 5
self.resetHighScoreButton.position = CGPointMake(CGRectGetMidX(self.frame), self.removeAdsButton.position.y - self.removeAdsButton.size.height - 5)
self.resetHighScoreButton.size.width = self.frame.size.width * (36/100)
self.resetHighScoreButton.size.height = self.resetHighScoreButton.size.width * (18/100)
self.resetHighScoreButton.zPosition = 5
self.creditsButton.position = CGPointMake(CGRectGetMidX(self.frame), self.resetHighScoreButton.position.y - self.resetHighScoreButton.size.height - 5)
self.creditsButton.size.width = self.frame.size.width * (36/100)
self.creditsButton.size.height = self.creditsButton.size.width * (18/100)
self.creditsButton.zPosition = 5
self.backButton.position = CGPointMake(CGRectGetMidX(self.frame), self.creditsButton.position.y - self.creditsButton.size.height - 5)
self.backButton.size.width = self.frame.size.width * (36/100)
self.backButton.size.height = self.backButton.size.width * (18/100)
self.backButton.zPosition = 5
self.nosoundButton.anchorPoint = CGPointMake(1, 0)
self.nosoundButton.position = CGPointMake(CGRectGetMaxX(self.frame) - 10, CGRectGetMinY(self.frame) + 10)
self.nosoundButton.size.height = 40
self.nosoundButton.size.width = 40
self.nosoundButton.zPosition = 5
// Add buttons
self.addChild(self.removeAdsButton)
self.addChild(self.resetHighScoreButton)
self.addChild(self.creditsButton)
self.addChild(self.backButton)
self.addChild(self.nosoundButton)
}
func addSocial() {
self.twitterButton.anchorPoint = CGPointMake(0, 0)
self.twitterButton.position = CGPointMake(CGRectGetMinX(self.frame) + 10, CGRectGetMinY(self.frame) + 10)
self.twitterButton.size.height = 40
self.twitterButton.size.width = 40
self.twitterButton.zPosition = 5
self.facebookButton.anchorPoint = CGPointMake(0, 0)
self.facebookButton.position = CGPointMake(CGRectGetMinX(self.frame) + 10, self.twitterButton.position.y + self.twitterButton.size.height + 5)
self.facebookButton.size.height = 40
self.facebookButton.size.width = 40
self.facebookButton.zPosition = 5
self.gamecenterButton.anchorPoint = CGPointMake(1, 0)
self.gamecenterButton.position = CGPointMake(CGRectGetMaxX(self.frame) - 10, self.nosoundButton.position.y + self.nosoundButton.size.height + 5)
self.gamecenterButton.size.height = 40
self.gamecenterButton.size.width = 40
self.gamecenterButton.zPosition = 5
self.addChild(self.twitterButton)
self.addChild(self.facebookButton)
self.addChild(self.gamecenterButton)
}
答案 0 :(得分:0)
尝试将CGRectGetMinX(self.frame)
替换为0
,将CGRectGetMaxX(self.frame)
替换为self.frame.size.width
y位置。
答案 1 :(得分:0)
解决方案:
我已将scene.size = skView.bounds.size
添加到视图中。这解决了这个问题。