по существу у меня было приложение работает нормально, но затем, когда я попытался запустить его на любом другом симуляторе, кроме симулятора XS в XCode, кнопки главного меню больше не работают, и я не вижу, где искать ошибку, так как ничегобросает какие-либо ошибки.Более того, когда я помещаю операторы print внутри узлов, оператор print печатает на консоль, которая сообщает мне, что экран определяет выбор, но по какой-то причине код просто не запускается.Что странно, потому что это простой оператор if внутри вызываемой функции, поэтому я не вижу причин, по которым он также не должен запускаться.
var background = SKSpriteNode(imageNamed: "bgImage")
var newGameButtonNode:SKSpriteNode!
var difficultyButtonNode:SKSpriteNode!
var difficultyLabelNode:SKLabelNode!
var gameModeNode:SKSpriteNode!
var gameModeLabel:SKLabelNode!
var gameMode:Int = 1
override func didMove(to view: SKView) {
background.zPosition = -4
background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
background.scale(to: (self.scene?.size)!)
addChild(background)
newGameButtonNode = self.childNode(withName: "newGame") as? SKSpriteNode
difficultyButtonNode = self.childNode(withName: "difficultyButton") as? SKSpriteNode
difficultyLabelNode = self.childNode(withName: "difficultyLabel") as? SKLabelNode
gameModeNode = SKSpriteNode(imageNamed: "toggle")
gameModeLabel = self.childNode(withName: "gameModeLabel") as! SKLabelNode
let userDefaults = UserDefaults.standard
if userDefaults.bool(forKey: "hard") {
difficultyLabelNode.text = "HARD MODE"
} else {
difficultyLabelNode.text = "EASY MODE"
}
gameMode = userDefaults.integer(forKey: "mode")
if gameMode == 1{
gameModeLabel.text = "Crypto Catch"
userDefaults.set(1, forKey: "mode")
} else if gameMode == 2 {
gameModeLabel.text = "Crypto Invaders"
userDefaults.set(2, forKey: "mode")
} else if gameMode == 3 {
gameModeLabel.text = "Crypto Galiga"
userDefaults.set(3, forKey: "mode")
}
difficultyButtonNode.texture = SKTexture(imageNamed: "toggleYellow")
difficultyButtonNode.anchorPoint = CGPoint(x: 0, y: 0)
difficultyButtonNode.scale(to: CGSize(width: 250 , height: 250))
newGameButtonNode.texture = SKTexture(imageNamed:"playGameYellow")
newGameButtonNode.anchorPoint = CGPoint(x: 0, y: 0)
gameModeNode.anchorPoint = CGPoint(x: 0, y: 0)
gameModeNode.scale(to: CGSize(width: 250, height: 250))
gameModeNode.name = "gameModeNode"
newGameButtonNode.scale(to: CGSize(width: 250, height: 250))
difficultyButtonNode.position = CGPoint(x: (scene?.size.width)!/2-125, y: (scene?.size.height)!/2-150)
newGameButtonNode.position = CGPoint(x: (scene?.size.width)!/2-125, y: (scene?.size.height)!/2+150)
difficultyLabelNode.position = CGPoint(x: (scene?.size.width)!/2, y: (scene?.size.height)!/2-175)
gameModeLabel.position = CGPoint(x: (scene?.size.width)!/2, y: (scene?.size.height)!/2-175 - 300)
self.addChild(gameModeNode)
gameModeNode.position = CGPoint(x: (scene?.size.width)!/2-125, y: (scene?.size.height)!/2-175 - 250)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self){
let nodesArray = self.nodes(at: location)
if nodesArray.first?.name == "newGame"{
if gameMode == 1{
let transition = SKTransition.flipHorizontal(withDuration: 0.5)
let gameSceneOB = GameScene(fileNamed: "GameScene")
gameSceneOB?.scaleMode = .aspectFill
self.view?.presentScene(gameSceneOB!, transition: transition)
} else if gameMode == 2 {
let transition = SKTransition.flipHorizontal(withDuration: 0.5)
let gameSceneOB = GameScene(fileNamed: "ShooterGame")
gameSceneOB?.scaleMode = .aspectFill
self.view?.presentScene(gameSceneOB!, transition: transition)
} else if gameMode == 3 {
let transition = SKTransition.flipHorizontal(withDuration: 0.5)
let gameSceneOB = GameScene(fileNamed: "cryptoGal")
gameSceneOB?.scaleMode = .aspectFill
self.view?.presentScene(gameSceneOB!, transition: transition)
}
} else if nodesArray.first?.name == "difficultyButton" {
changeDifficulty()
} else if nodesArray.first?.name == "gameModeNode"{
toggleGameMode()
}
}
}
func changeDifficulty() {
let userDefaults = UserDefaults.standard
if difficultyLabelNode.text == "EASY MODE"{
difficultyLabelNode.text = "HARD MODE"
userDefaults.set(true, forKey: "hard")
} else {
difficultyLabelNode.text = "EASY MODE"
userDefaults.set(false, forKey: "hard")
}
userDefaults.synchronize()
}
func changeGameMode() {
let userDefaults = UserDefaults.standard
if gameMode == 1{
gameModeLabel.text = "Crypto Catch"
userDefaults.set(1, forKey: "mode")
} else if gameMode == 2 {
gameModeLabel.text = "Crypto Invaders"
userDefaults.set(2, forKey: "mode")
} else if gameMode == 3 {
gameModeLabel.text = "Crypto Galiga"
userDefaults.set(3, forKey: "mode")
}
userDefaults.synchronize()
}
func toggleGameMode(){
if gameMode == 1{
gameMode = 2
} else if gameMode == 2 {
gameMode = 3
} else if gameMode == 3 {
gameMode = 1
}
changeGameMode()
}