I am having trouble with loading random scenes in swift. I am using array which is created on start of game and it has all unique random numbers, with range of 0...30. Now, it happens that 2 scenes somehow load together and I have only blank screen and all the sounds. Here is the code for shuffle:
if touchedNode.name == "shufle" {
var pageAdded = defaults.array(forKey: "page")as? [Int] ?? [Int]()
if pageAdded.count < 30 {
DispatchQueue.global().async {
let scene = Int(arc4random_uniform(30) + 1)
var array = self.defaults.array(forKey: "sheme") as? [Int] ?? [Int]()
var scene1 = array[scene]
if pageAdded.contains(scene1){
if let find = array.index(of: scene1) {
if find < 30 {
scene1 = scene1 + 1
let transition = SKTransition.flipHorizontal(withDuration: 0.3)
if let oldGameScene = GameScene(fileNamed: "GameScene\(scene1)") {
oldGameScene.scaleMode = .aspectFill
self.view?.presentScene(oldGameScene, transition: transition)
}
} else {
scene1 = scene1 - 1
let transition = SKTransition.flipHorizontal(withDuration: 0.3)
if let oldGameScene = GameScene(fileNamed: "GameScene\(scene1)") {
oldGameScene.scaleMode = .aspectFill
self.view?.presentScene(oldGameScene, transition: transition)
}
}
}
} else {
let transition = SKTransition.flipHorizontal(withDuration: 0.3)
if let oldGameScene = GameScene(fileNamed: "GameScene\(scene1)") {
oldGameScene.scaleMode = .aspectFill
self.view?.presentScene(oldGameScene, transition: transition)
}
}
}
} else {
pageAdded.removeAll()
defaults.set(pageAdded, forKey: "page")
print("Igrajte ponovo")
}
}
And here is the code when func(didMove:) is loaded:
pageAdded = defaults.array(forKey: "page")as? [Int] ?? [Int]()
if pageAdded.contains(pageID) {
pageTest = false
}
if pageAdded.count < 30 {
if pageTest == false{
let game = Int(arc4random_uniform(30) + 1)
if let oldGameScene = GameScene(fileNamed: "GameScene\(game)") {
oldGameScene.scaleMode = .aspectFill
self.view?.presentScene(oldGameScene)
}
} else {
pageAdded.append(pageID)
defaults.set(pageAdded, forKey: "page")
print("Stranica \(pageID) dodata")
print(pageAdded.description)
}
} else {
print("Cestitamo, sredio si sva slova!")
pageAdded.removeAll()
if let oldGameScene = GameScene(fileNamed: "GameSceneFirst") {
oldGameScene.scaleMode = .aspectFill
self.view?.presentScene(oldGameScene)
}
}
"pageId" is number of scene, "scene1" is number element of saved array which is used for selecting new scene, "pageAdded" is array which is filled every time the scene is loaded, and not already contained in it. I know it is a little bit big, but if anyone has more advanced and smaller code that can solve my problem, I would be very grateful.
Aucun commentaire:
Enregistrer un commentaire