1

I'm making a simple game in swift and xcode and I ran into this problem that I can't figure out. Because I have so many levels, the code locks up indexing and slows down the whole program. I get a color wheel spinning for a few minutes but it never crashes. Just takes several minutes everytime I type in a few characters. Strange, but xcode has always had it's bugs right?

Each Button ("button1, button2..." below) gets a single number from "level1:Array". It's like a code that fills in the button's value for the game. There are only 4 buttons, but the numbers should be able to change as they each have their own variables.

I want to generate this for every level. I should be able to generate something like "button1 = level#[0]" where # is replaced by "userLevel". Changing to a string and doing something like "button1 = ("level(userLevel)") as! Array... doesn't seem to work. Look below and use my terminology when giving examples if you can. Thanks!

Here's the example:

let level1:Array = [9,7,4,1] // current puzzle, to go directly into button vars below (button1,button2,ect)
var userLevel = 1   // current user's level

if userLevel == 1 {
    print("making Level 1, setting buttons to value from array")
    button1 = level1[0]
    button2 = level1[1]
    button3 = level1[2]
    button4 = level1[3]
}

Now, since level# is repeated so often (for each level as the number progresses) I would rather just make it something like this:

//this doesn't work in swift, but what else can I do?
if userLevel > 0 {
button1 = level\(userLevel)[0]
button2 = level\(userLevel)[1]
button3 = level\(userLevel)[2]
button4 = level\(userLevel)[3]
}  

Is there an easy way to do this? Thanks! -GG

2
  • "Because I have so many levels, the code locks up indexing and slows down the whole program." I very, very much doubt this. Any evidence that indexing is slowing anything down? You don't say what "button" is, but having lots of them while your app is running seems unlikely to be good design. Commented Nov 28, 2015 at 9:49
  • @gnasher729 every time I type anything there's a several minute delay where the color wheel spins. I imagine indexing since it happened after I added 1000 levels to my otherwise small code. I can make it work by just increasing the place at the number sign in: level#[0] - basically I want to make this string "level(userlevel)[0]" to be recognized as an Array. There are only 4 button vars but the numbers change within their values, (see let level1=[9,3,4,1])The level number (userLevel) goes from 1-1000 which I would like to automate. Commented Nov 28, 2015 at 17:41

1 Answer 1

1

Try using a for-in loop. Create an array of the buttons, and then

var index = 0

for button in buttons {
button = level1[index]
index++
}

EDIT since you want both the user level and the level number to increase, I suggest you define the levels like this. (Make sure that the number of buttons is equal to the number of userLevels, otherwise you will have problems)

var array = [1,2,3]
let levels = [1:[1,3,8],2:[3,6,4],3:[4,2,5]]

var index = 0
if array.count == levels.count {
  for number in array {
  array[index] = levels[index+1]![index]//The second index can be 0 if you want
  index++
  }
}
//array = [1,6,5]
// You could create a second index to match the number of levels within the main user level.

In this case, assume array to be your array of buttons

EDIT 2 :) I've made a function that will allow you to assign all the levels to your array for a specific userLevel, since I see that is what you want

let levels = [1:[1,3,8],2:[3,6,4],3:[4,2,5]]

func assignValuesToArray(levelNo:Int) -> [Int] {

var array: [Int] = []

if (levelNo > 0) && (levelNo <= levels.count) {

for (level,levelArray) in levels {

    if level == levelNo {

        for value in levelArray {

            array.append(value)


            }

        }

    }

    return array

} else {

    print("This Level number does not exist")
    return []
}
}

var finalArray = assignValuesToArray(2)
print(finalArray) // Returns [3,6,4]

As you can see from this example, you will return your array of buttons from the function, and you can assign the returned array values to whatever you like.

Sign up to request clarification or add additional context in comments.

9 Comments

I see, this will help so I don't have to write level1[0], level[1]... right? It's a very clean solution and I like it! I think my problem was mostly with adding the level# to the end of this: button1 = level1[0] Is there a way to make the level# go up, (level2[0], level3[0]...)? The button1 var wasn't recognizing me trying to add (userLevel) to the end of "level". Thanks!
So you want the level# and the level#[#] to go up? What is the order in which it has to be incremented? @ustuss
I think your above solution may be the answer. I can automate a new set of levels to print out that way! Thanks
This didn't quite work for my situation, I kept getting an error unwrapping nil in "array[index] = levels[index+1]![index]" But I do like the idea of working with dictionaries... I'll mull this over, just need coffee. Thanks for the help again! I'm a beginner, but have been able to do a lot with just arrays, variables, NStimers, for/while loops, and some more complex stuff. I'll stick with it though, working out these problems are fun, I just don't know Swift's syntax perfectly.
Yeah, you will get an error if the number of buttons is not equal to the number of userLevels.You can create another index to access the level# within the userLevel to avoid getting an out of range array value. You could even use tuples, but I think dictionaries would be better
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.