J'ai un tableau qui contient les valeurs CGFloat et je suis en train de comprendre comment j'aller sur randomiser à travers ce tableau pour obtenir une coordonnée aléatoire x:Obtention d'une valeur CGFloat aléatoire à partir d'un tableau?
let xAxisSpawnLocations: [CGFloat] = [screenWidth + 150, screenWidth + 100, screenWidth + 50 , screenWidth - 50, screenWidth - 100, screenWidth - 150]
parce que je dois les mettre en ligne de code qui positionnera un objet sur la carte. J'ai essayé de chercher comment randomiser à travers un tableau mais il ne l'a que pour les valeurs Int32.
Peut-être s'il vous plaît fournir et exemple sur la façon de le faire.
Voici le code complet pour référence si vous devez regarder:
let xAxisSpawnLocations: [CGFloat] = [screenWidth + 150, screenWidth + 100, screenWidth + 50 , screenWidth - 50, screenWidth - 100, screenWidth - 150]
let yAxisSpawnLocations: [CGFloat] = [0]
for xLocation in xAxisSpawnLocations {
for (var i = 0; i < Int(numberOfObjectsInLevel); i++) {
let obstacle:Object = Object()
obstacle.theType = LevelType.road
obstacle.createObject()
addChild(obstacle)
obstacle.position = CGPoint(x: xLocation, y: yAxisSpawnLocations[0])
obstacle.zPosition = 9000
// addChild(greenFrog)
}
}
}
Le xLocation doit être remplacé par le randomiser
double possible de [Comment fait-on nombre aléatoire entre la plage pour arc4random \ _uniform()?] (http://stackoverflow.com/questions/24132399/how-does-one-make-random-number-between-range-for-arc4random-uniform) – SmokeDispenser
Probablement un doublon de [Choisissez un élément aléatoire parmi un tableau] (http://stackoverflow.com/questions/24003191/pick-a-random-element-from-an-array) – tebs1200