jeudi 22 janvier 2015

SWIFT - Sprite Kit : Array with nodes at each index

i'm creating a game with Sprite Kit in Swift, and i've three nodes (jewels) generated at 3 x position :



self.size.width * 1/6 // left
self.size.width * 1/6 // middle
self.size.width * 1/6 // right


I've created an array with the three positons to call them randomly :



let randomX = [self.size.width * 1/6, self.size.width * 3/6, self.size.width * 5/6] // Array with three positions
let randomIndex = Int(arc4random_uniform(UInt32(randomX.count)))
let xPos : CGFloat = randomX[randomIndex]


I've an other array with the three nodes of jewels :



let randomJewels = [SKSpriteNode(imageNamed: "jewel1"), SKSpriteNode(imageNamed: "jewel2"), SKSpriteNode(imageNamed: "jewel3"),]


And I set the position and the size of my generated node :



let jewel1 = randomJewels[Int(arc4random_uniform(3))]
jewel1.anchorPoint = CGPointMake(0.5, 0)
jewel1.position = CGPointMake(xPos, self.size.height / 2)
jewel1.size.width = 40
jewel1.size.height = 20
self.addChild(jewel1)


The three positions need to be taken by three random node (jewel) but if I generate two other jewelry, they may have the same position, as they may have three different positions. How can I do so that the jewels hold the three positions ? It can not be void position, there is a jewel on every x position !


Thanks !





Aucun commentaire:

Enregistrer un commentaire