Im beginning work with Cocos2d and have been working off this tutorial (link is for part 9) the past few days.
In reading the source and trying to understand it I have reached a section that doesnt make any sense to me.
-(void)gameLogic:(ccTime)dt {
static double lastTimeTargetAdded =0;
double now = [[NSDate date] timeIntervalSince1970];
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
if(lastTimeTargetAdded == 0 || now - lastTimeTargetAdded >= delegate.curLevel.spawnRate) {
[self addTarget];
lastTimeTargetAdded = now;
}
}
called via this:
[self schedule:@selector(gameLogic:) interval:0.2];
With the fact that lastTimeTargetAdded is created and set each time the function runs how is it ever not 0? And if thats the case what is the point of lastTimeTargetAdded = now? With the if statement being an OR (||) it never evaluates the other side of it so why is that even there?
I understand well enough what the function does just not so much how it does it. This method is suppose to spawn creeps based on their spawn rate. Making sure all the creeps in the wave arent just dumped on screen. And the method does do that well enough.
it is a static var … the first statement sets to 0 only on the first invocation of gameLogic. On each subsequent invocation, lastTimeTargetAdded has the value that was set in the previous invocation.