EDIT: I have changed the title as I think that this is a general gamedevelopment question but, on a technical note, I am using Cocos2d v2.0

Some time ago I had asked a question on which was the most efficient way/method to spawn enemies.

I have choosen to spawn my enemies on the "update" cycle of the main GameScene. In short: at each update call every single enemy in the enemy array is checked and if the enemy has not spawn and the total elapsed time is greater than the current time then the enemy will spawn.

I have realized only now that basing the spawn time on the total elapsed time has the following drawback.

For example, in a vertical scrolling shooter game, ff the frame rate is very low (e.g. 20 fps) then the spawn position will be much different from each other.

I have "planned" and "engineered" my gameplay around 60fps, so if for any chance there is a framedrop then the user will have a different user experience also in terms of enemy spawn position (normally, in those "bad" cases the user should be affected only by the lower frame rate but here is actually getting a different game - in certain cases easier in other cases more difficult).

On the bright side I have been using Cocos2d 2.0 that limits the game to only more powerful devices plus I have optimized my game quiet a lot (use of spritebatch nodes and pvr.ccz compressed images) which means that on avarage the game is running relatively fast.

Remains the drawback that we are talking about "avarage fps" and hence different users with different iPhones will have different spawning positions of enemies.

Any suggestion to limit this drawback? Am I the only one experiencing/noticing this?


Frames Per Second (FPS) should be just that. The number of frames drawn every second. What FPS is not is something that you should be basing your gameplay timings around. You are running into issues when spawning enemies according to elapsed time because the rest of your game is not based around time, I bet.

In your current setup I imagine you are updating fixed values every frame. The trick is to make sure the change in time has been taken into account here as well. A simple example of this is when it comes to moving your character.

The simple way that we all start out with when making games is we think "I want the player to move 1 pixels forward on the X axis every frame." This usually results in code that looks like the following:

Player.Position.X += 1; - this is not correct.

The correct way of thinking would be "I want the player to move 60 pixels forward on the X axis every second (equating to 1 pixel per frame at 60fps). The reason you want to think this way is because you can now very easily incorporate time into the equation.

Player.Position.X += time * speed;

With this approach, if you for some reason get a 4 second stutter in your gameplay, this has been handled for because your movement is based around time instead of frames.

While I don't feel this question / answer is really a duplicate considering the circumstances around your question, am rather confident they are relevant to the issue you are seeing and I would highly recommend you read through the information there to further your understanding on what I explained above.

| improve this answer | |
  • \$\begingroup\$ Thank you, I hadn't considered the second option. It really helps as otherwise I would have had to change all my code using CCMoveTo actions handled by the CCActionManager. \$\endgroup\$ – mm24 Jun 12 '13 at 16:52

Create your own thread that will be checking the array of enemies and adding them to the game. Also sort the enemies by time they are supposed to arrive and stop on the first enemy that in not supposed to arrive yet - then you don't need to check all of them. You can implement this by using stack. Push the enemies to the stack in the opposite order and pop() them when needed.

How to do this in cocos2d, I don't know, but this should be the general idea.

| improve this answer | |
  • \$\begingroup\$ That is an extremely inefficient solution and doesn't even totally solve the problem (the 2nd thread can also be slowed down by something). \$\endgroup\$ – Tara Feb 7 '14 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.