سؤال

I am making a game similar to bust-a-move for the iphone. I am using cocos2d. I am encountering lag sometimes during the game. I have 6 different types of balls. I am creating 30 instances of each type and I reuse them. The problem comes when I shoot the ball and the ball does not collide with either the wall on the sides or the balls on the screen. I believe it may be due to a lag. When the ball is shot I initialize a scheduler to run every 0.01s to check if it collides with any of the other balls. In each interval I create a rect for the ball that was shot and create a rect for all other balls and check for intersection.

I was wondering what the reason for the lag could be. I am also trying to figure out if there is any bottle neck by using the Instruments. Could having a lot of images loaded cause lag? or could a lot of numerical computation be the reason?

It would be helpful if you can share any similar experiences or if you can suggest some possible reasons for lag and how to avoid them.

Thanks Abhinav

هل كانت مفيدة؟

المحلول

a 100Hz (0.01sec) timer is going to slow your game right down. Most PC's can't even handle a scheduler with that frequency (mainly, the OS overhead, but I digress).

Highly recommend that you use the CCLayer update method instead of a Scheduler or NSTimer.

نصائح أخرى

OK,So i've figured out the problem :)

The thing is, in my game I am loading 100 balls. 20 of each of the 5 types. Besides that I am also loading some explosion animation and some other animated creatures. So the problem comes because each texture was loaded individually even though most of them are using the same texture. So each frame 120 odd textures were loaded.

The solution is to use Batchnode. So instead of 20, now I load only 1 texture. So in total instead of 120 I am now loading only 6 or 7 :) Now all the lag and lag related bugs are gone.

[[CCTextureCache sharedTextureCache] addImage:@"ball-black.png"];
SpriteSheet1 = [CCSpriteBatchNode batchNodeWithFile:@"ball-black.png"];
[self addChild:SpriteSheet1 z:4];
//---------FIRE BALLS---------

for (int i = 0; i<20; i++) 
{
    f[i] = [[Fire alloc]init];
    //[f[i] getball] = [CCSprite spriteWithFile:@"ball-black.png"];
    [SpriteSheet1 addChild:[f[i] getball]];
    [[f[i] getball] setPosition:ccp(220,-200)];
    f[i].isBallMoving = FALSE;
}

Hope it helps someone with the same problem.

Thanks Stephen for your replies :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top