Thursday, 22 March 2012

A step in time

I decided to start my great iOS adventure by getting the menu system underway, the previous post points to an excellent article where menu's are discussed at a reasonable length, so I wont go over it again.

So I have a menu or two, yay. No I wanted to write a blog post about dealing with the 4 (actually, it's only really 2) resolutions you should be supporting on iOS systems, but after researching with how to deal with iPad screens for about an hour, I still haven't got anywhere near a proper conclusion. The only conclusion I have drawn is how much of a complete mess the iOS system and cocos2d is with regard to screen resolutions (please feel free to prove me wrong by proving a complete article on the perfect solution).

So to appease my sense of wanting to get something done, I decided to work on my main menu a bit more. In Critter Rollers, the logo on the main menu bounces up and down joyfully while waiting for player input (I hate, hate, hate, static main menu screens). To do this effectively I needed to implement a fixed time step.

If you are not familiar with a fixed time step then Google it, but the short version is that a fixed time step is a loop which will account for any discrepancies in your frame rate. I wanted to use a similar timestep to the one I employed on Android so I did a bit of searching and found a piece of information saying how to create a scheduled event, which will run as soon as the processor is available.

You call this in the init function and this will instruct the scheduler to call a particular function either after a fixed period of time, or asap. We want asap as the fixed time difference would cause slowdown (sorry for the misleading labelling)

[self schedule: @selector(update:) ];


Now this code would fire the "update" block

float fixedTimeStep = 0.03;//0.05;
float timeLeftOver = 0.0;
float timeAccumulator = 0;

-(void)update:(ccTime)dt {

float timeToRun = dt + timeAccumulator;
while(timeToRun >= fixedTimeStep)
{
//update physics perhaps

//do other stuff

timeToRun = timeToRun - fixedTimeStep;
}
timeAccumulator = timeToRun;
}


So what we have here are a few counters to keep track of time, the method the scheduler is calling and then all the gubbins to keep our operations on track. Now this code isn't perfect as far as fixed time steps goes, but as we are expecting to run our game on a fairly powerful device (and since Critter Rollers isn't too processor intensive) then we can happily get away with this.

In the //do other stuff, block, I am updating the position of my logo, for an idea of how to do that, check out Part 3 of Oscars tutorial below.

http://www.oscarvgg.com/your-first-game-in-cocos2d-part-3-the-gameplay/

Everything seems to work fairly well here, I'm still not sure if this solution is acceptable for my physics updates, but I will let you know if it isn't.

Anyway, that's me for tonight, I'm off to chill out and enjoy my bouncing logo a bit more. Andrew.

No comments:

Post a Comment