I was working on my little app called MetorideStorm. It's a very simple 开发者_如何学JAVAgame, where some walls need to move down, each second. Now the game needs to be more difficult, and the walls will need to fall down faster. How can i do that? Can i make a float, and control the NSTimer with that?
By the was, the game is for iPhone, but i don't know if that matter. I am very new to iPhone and iPad programming, so i don't know so much about it yet.
Thanks - Frederik Witte
If I were to rephrase your question I would put it like this. I have a set of walls, each wall needs to have a velocity based on difficulty. At a given time, each wall's position needs to be updated based on its velocity.
In general you wouldn't want to vary the time at which this update takes place, as the movement could begin to look choppy, think frame rate. If you have a look at the book "Learn iPhone and iPad cocos2d Game Development" Steffen Itterheim even goes so far as to recommend always assuming a constant frame rate. The argument being, that during normal game play you really want a constant frame rate. In cases where the frame rate may drop (for example there is an incoming email and the iPhone has to do extra processing) you don't want any movement to jump too far. That being said, it would be better that for this short amount of time things move slowly and then resume where the user was before the disruption.
So rather than changing the interval of your NSTimer, it may be better to change the distance moved. Set up a variable that holds the velocity of the wall(s). Set up your timer with a sane value (1/60 is a good choice). Then in your callback from your timer update the position of the wall(s) based on the velocity.
Of course this is just if you are rolling your own game engine. If you are using cocos2d then you should really use the CCDirector's scheduler as using your own NSTimer will cause problems when the director is paused/resumed, etc... And if you aren't using cocos2d I'd recommend looking into it, as it takes care of this sort of thing for you.
Ummm. If you need to change the speed do this:
//This is the way you make a timer. Where @selector(meth:) is the method you want to
//call and interval is the amount of time for each call. Decrease x to speed up the
//timer.
[self schedule: @selector(yourMethod:) interval:x];
//This is the method your timer will call.
-(void) yourMethod: (ccTime) dt
{
//Do stuff in here
}
This is the way for Cocos2d since you said you were using Cocos2d.
If you insist on "speeding up" a timer, the only way i've found to do this when i looked it up was invalidating and creating a new one, don't think there's another way... The only thing you can change is the moment the timer will fire using the fireDate property, but this will not change the interval between timer fires, and i think it will only represent the first time the timer fired or will fire.
精彩评论