mercredi 21 janvier 2015

SWIFT - Sprite Kit : Generation of a random node, on random x position

i'm making a game with Sprite Kit in Swift, and I have 3 type of node (a red circle, a green square and a blue triangle), and the screen is divided in 3 parts, the first one is the screen's width * 1/6, the second one is the screen's width * 3/6, and the last one is the screen's width * 5/6.


I want the jewelry was generated randomly on the first, the second or the last part, randomly. So the jewel can be the circle, the square or the triangle.



func jewelryGeneration() {


var moveDuration: NSTimeInterval = 4


// GENERATE LEFT JEWELRY //

let jewelryLeft = SKSpriteNode(imageNamed: "jewelry1")
jewelryLeft.anchorPoint = CGPointMake(0.5, 0)
jewelryLeft.position = CGPointMake(self.size.width * 1/6, CGRectGetHeight(self.frame))
jewelryLeft.size.width = 40
jewelryLeft.size.height = 20
self.addChild(jewelryLeft)

var moveJewelryLeft = SKAction.moveTo(CGPointMake(jewelryLeft.position.x, -jewelryLeft.size.height), duration: moveDuration)
jewelryLeft.runAction(moveJewelryLeft)


// GENERATE MIDDLE JEWELRY //

let jewelryMiddle = SKSpriteNode(imageNamed: "jewelry2")
jewelryMiddle.anchorPoint = CGPointMake(0.5, 0)
jewelryMiddle.position = CGPointMake(self.size.width * 3/6, CGRectGetHeight(self.frame))
jewelryMiddle.size.width = 40
jewelryMiddle.size.height = 20
self.addChild(jewelryMiddle)

var moveJewelryMiddle = SKAction.moveTo(CGPointMake(jewelryMiddle.position.x, -jewelryMiddle.size.height), duration: moveDuration)
jewelryMiddle.runAction(moveJewelryMiddle)


// GENERATE RIGHT JEWELRY //

let jewelryRight = SKSpriteNode(imageNamed: "jewelry3")
jewelryRight.anchorPoint = CGPointMake(0.5, 0)
jewelryRight.position = CGPointMake(self.size.width * 5/6, CGRectGetHeight(self.frame))
jewelryRight.size.width = 40
jewelryRight.size.height = 20
self.addChild(jewelryRight)

var moveJewelryRight = SKAction.moveTo(CGPointMake(jewelryRight.position.x, -jewelryRight.size.height), duration: moveDuration)
jewelryRight.runAction(moveJewelryRight)


}


I know how to generate a one type of jewel, but not the three, how can i do it ?


Thanks !





Aucun commentaire:

Enregistrer un commentaire