tags:

views:

341

answers:

5

Hi everyone. I am in trouble with my game now, it keep crashing when I stress it too much. Meaning I crazily sliding, moving, or tapping my fingers on the screen, it will crash. The crash has no rule. I tried to detect, in the console log shows the message low memory warning. I know it is about the memory stuff, but I am using Cocos2D to make this game, I followed the instructions in best practice. It seems smoother after that but still crashes if I do like what I mentioned above. If like in Cocoa, we have alloc and release, but it Cocos2D, I think we don't need to do so. My game is just loading images, and make animation after touching.

//where the fingers ended , this will determine the correct actions made. 
-(BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)events
{
    int touchCount = 0;
    NSSet *allTouches3 = [events allTouches];
    for( UITouch *touch in allTouches3)
    {
        location3 = [touch locationInView: [touch view]];
        location3 = [[Director sharedDirector] convertCoordinate: location3];

        NSLog(@"end TOUCHed x2: %3.3f, y2: %3.3f",location3.x,location3.y);
        touchCount++;
    }
    [self removeChildByTag:kTagWord cleanup:YES]; 

    timeEnd = [NSDate timeIntervalSinceReferenceDate];
    touchDuration = timeEnd - timeStart;
    //float rangeX = location3.x - location.x;
    rangeY2 = location3.y - location.y;

    //loading the succesful opened box with 2 touches same direction 
    if ((touchCount > 0 && touchCount ==2) && (rangeY2 > 0.0 && rangeY2 <15.0))
    {
        box++;
        self.isTouchEnabled = NO;
        [self removeErrorText];

        if (box == 1)
        {
            [self loadingTheRest];
        }
        else if (box == 2)
        {
            [self loadingTheRest1];
        }
        else if (box ==3)
        {
            [self loadingTheRest2];
        }

        if (currentBox == 0 )
        {
            [self addText];
            [self boxOpenAnimation];
            [self schedule:@selector(enableTheTouches) interval:1.0f];

        } else if(currentBox == 1 )
        {
            [self addText1];
            [self boxOpenAnimationPink];
            //[self schedule:@selector(winGame) interval:0.4f];
            [self schedule:@selector(enableTheTouches) interval:1.0f];

        }
    }
}

and this is an example of a loading box

-(void)loadingTheRest
{
    //LOADING OTHER COLOURS
    AtlasSpriteManager *managerPink  = [AtlasSpriteManager spriteManagerWithFile:@"PinkNew.png"];
    AtlasSpriteManager *error2  = [AtlasSpriteManager spriteManagerWithFile:@"PinkErrors.png"];
    [self addChild:managerPink z:1 tag:kTagSpriteManagerPink ];
    [self addChild:error2 z:1 tag:kTagSpriteErrorPink ];

}

-(void)boxOpenAnimation
{
    if (isBusy==YES)
    {
            return;
    }
    isBusy=YES;

    [self removeBoxColours];
    AtlasSpriteManager *mrg = (AtlasSpriteManager *)[self getChildByTag:kTagSpriteManager];
    AtlasSprite *box = [AtlasSprite spriteWithRect:CGRectMake(482, 322,480, 320) spriteManager:mrg];
    box.position = ccp(240,160);
    [mrg addChild:bra z:1 tag:1984];

    AtlasAnimation *animation = [AtlasAnimation animationWithName:@"open" delay:0.1];

    [animation addFrameWithRect: CGRectMake(1, 322, 480, 320) ];    
    [animation addFrameWithRect:CGRectMake(482, 1, 480, 320)];
    [animation addFrameWithRect:CGRectMake(1, 1, 480, 320)];
    [animation addFrameWithRect:CGRectMake(1, 643, 480, 320)];
    id action = [Animate actionWithAnimation:animation];

    [box runAction:action];
    [self loadingTheRest];
    isBusy=NO;

}

Please share your knowledge with me if you know about the reasons why my game crashes. Thank you so much

+5  A: 

You don't have the code here, but my guess is that you are dividing by 0 with the value here.

touchDuration = timeEnd - timeStart;

According to Apple, timeIntervalSinceReferenceDate returns seconds which means that if you call it within a second timeEnd - timeStart == 0.

I deduced this from your description of the crashes.

jbcreix
A: 

I have another code here:

else if ((touchCount>0 && touchCount==2)&&touchDuration < 0.5)
{
// do something if it is too fast
}

the timeStart is the time u put ur fingers down, timeEnd is the time you remove your fingers. So I will calculate the time from there. Is it the cause of the crash?

Rocker
If I was right that duration was the problem, then it is likely that the problem is inside that branch but really if you provided more information, people would be better able to help you. I think I did a good guess, but it could be anything. Maybe something you are doing is allocating memory but never freeing and you run out. Or maybe you have a race condition. Who knows.
jbcreix
Thanks for your help. But one thing, are you sure timeEnd - timeStart == 0. ? I am quite in doubt with that. Just not I say u wrong, I just don't have anything to testify this case. Because when I adjust the touchDuration , it quite sure follows what I give it to. What do you mean by race condition? using Cocos2D, I have not seen any example showing allocating and freeing objects
Rocker
hmm, I think those codes are mostly everything i had in my game scene. I need to slide my 2 fingers to the centre to open the box in 30 seconds, 10 of the boxes in total. And the box will react if i do something wrong sliding, such as 1 finger slide or too fast sliding. after I tap or sliding too fast, for quite some time, it will crash. The crash has no rule or timing. It crashes when it likes to.
Rocker
A: 

Maybe your game is consuming too much memory? In such case, the system itself would just kill your game if anything else needs more memory and your game has taken all of it. Such situations would look like random crashes in your game.

Albert
what can I do to reduce the memory usage to the minimum? I followed the best practice in Cocos2D. It helped a bit, but random crashes still occur
Rocker
+1  A: 

Make your code respond to the DidRecieveMemoryWarning message. Then when you are getting low on memory the OS should (it won't always, especially if your memory usage goes through the roof quickly) notify you, and then you can get rid of objects that may have gone off screen or such.

jamone
+1  A: 

I'd suggest you attach a debugger, disable catching first chance exceptions, enable grabbing second chance exceptions and then cause your crash. Debugger will show you the call stack of the crash.

Ariel