dimanche 18 janvier 2015

Generate random images at different intervals

i'm creating a game like Traffic Racer, the player control a car, and he must avoid the others drivers on the road. The game is in 2D, and there is 3 ways on the road.


I want to generate random cars at different intervals, on the first, the second or the third way, randomly, and the cars can't be 3 at the same time, because the player would be blocked and and couldn't avoir the cars.


In the didMoveToView, I generate the nodes of the three cars :



car1.anchorPoint = CGPointMake(0.5, 0)
car1.position = CGPointMake(self.size.width * 1/6, self.size.height - 100)
car1.size.width = 50
car1.size.height = 97
self.addChild(car1)

car2.anchorPoint = CGPointMake(0.5, 0)
car2.position = CGPointMake(self.size.width * 3/6, self.size.height - 100)
car2.size.width = 50
car2.size.height = 97
self.addChild(car2)

car3.anchorPoint = CGPointMake(0.5, 0)
car3.position = CGPointMake(self.size.width * 5/6, self.size.height - 100)
car3.size.width = 50
car3.size.height = 97
self.addChild(car3)


I've 3 timers for the 3 cars movement :



car1MovementTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("car1Movement"), userInfo: nil, repeats: true)

car2MovementTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("car2Movement"), userInfo: nil, repeats: true)

car3MovementTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("car3Movement"), userInfo: nil, repeats: true)


and 3 functions to move the three cars :



func car1Movement() {

car1.position.y = car1.position.y - car1Speed

}

func car2Movement() {

car2.position.y = car2.position.y - car2Speed

}

func car3Movement() {

car3.position.y = car3.position.y - car3Speed

}


In the update function, I've conditions to detect if the cars are out of the screen :



if car1.position.y < 0 - car1.size.height {

var randomYPositionCar1 = CGFloat(arc4random_uniform(100))

car1.position.y = self.size.height + randomYPositionCar1

}

if car2.position.y < 0 - car2.size.height {

var randomYPositionCar2 = CGFloat(arc4random_uniform(100))

car2.position.y = self.size.height + randomYPositionCar2

}

if car3.position.y < 0 - car3.size.height {

var randomYPositionCar3 = CGFloat(arc4random_uniform(100))

car3.position.y = self.size.height + randomYPositionCar3

}


I don't think using the right method, can you help me ?


Thanks !





Aucun commentaire:

Enregistrer un commentaire