xcode - Swift fatal error: Array index out of range -


i'm creating game small project , i'm trying make after ever 5 points walls generate more when playing game, crashes @ reaching around 24 points the error "fatal error: array index out of range"

gamescene.swift

 if pointslabel.number % knumberofpointsperlevel == 0 {                     currentlevel++                     wallgenerator.stopgenerating()                     wallgenerator.startgeneratingwallsevery(klevelgenerationtimes[currentlevel])                 } 

constants.swift

let knumberofpointsperlevel = 5 let klevelgenerationtimes: [nstimeinterval] = [1.0, 0.8, 0.6, 0.4, 0.3] 

it looks error here:

wallgenerator.startgeneratingwallsevery(klevelgenerationtimes[currentlevel]) 

when currentlevel equal or greater number of elements in klevelgenerationtimes.

i don't know correct solution is, depending on game logic - propose 2 versions, first restarting first element of array once end reached:

wallgenerator.startgeneratingwallsevery(klevelgenerationtimes[currentlevel % klevelgenerationtimes.count]) 

whereas second use last element of array (i take increasing complexity player progresses):

let index = currentlevel < klevelgenerationtimes.count ? currentlevel : klevelgenerationtimes.count - 1 wallgenerator.startgeneratingwallsevery(klevelgenerationtimes[index]) 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -