Take the 2-minute tour ×

I have a cocos2d based iphone app with a problem. When the user pauses the game and then hits the resume button, some CCSprites will disappear from the screen. This behavior is random, no pattern followed. I just know that this only happens when the user resumes the game.

Here is the code, what am I doing wrong? I first thought it might be a memory management problem, but I never get any EXC_BAD_ACCESS when the user hits resume... So the sprites probably still exist.

The sprites are a property within an object I'll call "myobject".

In myObject.h I have:

@interface myObject : CCNode{
   CCSprite *_sprite1,*_sprite2;
   // some other code
@property (nonatomic,retain) CCSprite *sprite1,*sprite2;

And in myObject.m file:

@synthesize sprite1=_sprite1;
@synthesize sprite2=_sprite2;

    myobject.sprite1 = [CCSprite spriteWithFile:spriteFile];
    [scene addChild:myobject.sprite1];
    // same for sprite2

-(void) move:(ccTime)dt{
    self.sprite1.position=ccp(self.x,self.y); // same for sprite2

then they get moved around with a function called on the myobject.

In the main scene, here is how these objects are created and moved around:

myObject *myObject;
NSMutableArray *_myObjects;

@implementation HelloWorld 

+(id) scene
    CCScene *scene = [CCScene node];
    HelloWorld *layer = [HelloWorld node];
    [scene addChild:layer z:0 tag:33];
    return scene;

-(id) init
    if( (self=[super init] )) {
    _myObjects = [[NSMutableArray alloc] init];
            // some other code

-(void) addObject(){
      myObject=[myObject create:self];
      [_fallingObjects addObject:fallingObject];

-(void) nextFrame:(ccTime) dt{
for(myObject *theObject in _myObjects){
            [theObject move:dt];

// And here is the function that does the pause/unpause, here it is:

- (void) pauseGame{
        [[CCDirector sharedDirector] pause];
        // some code to display menu etc... such as:
    [self addChild:pauseMenu z:10];
    [self removeChild:pauseMenu cleanup:YES];
        [self removeChild:scoreLabel cleanup:YES];
        [self removeChild:highscoreLabel cleanup:YES];
        [self removeChild:titleLabel cleanup:YES];
        [self removeChild:pauseLayer cleanup:YES];
        [[CCDirector sharedDirector] resume];


I have discovered that this problem is also true for sprites that I add directly to my scene, such as the sprite clown added as shown below:

CCSprite *clown;
@implementation HelloWorld
-(id) init
if( (self=[super init] )) {
       // some code
       clown = [[CCSprite spriteWithFile:@"clown.png"] retain];
       [self addChild:clown z:2];
       // some more code


share|improve this question
There should be a pause() method in Cocos2D you can call to stop elapsing its virtual time. Are you already calling it? Cheers. –  Loic Argelies Dec 8 '11 at 22:58

1 Answer 1

up vote 0 down vote accepted

There's something odd about your setup. FallingObject is a CCNode that contains two sprites. Yet instead of adding the sprites to the FallingObject class (self), and then adding FallingObject to the scene, you are adding the sprites directly to the scene. This leaves the FallingObject node outside of the scene hierarchy.

That's also why you have to do things like updating sprite positions manually which would otherwise require no code at all:


For what it's worth, I suppose that due to the nonconformity of this setup you may be accidentally moving the sprites outside the screen, or the node either isn't properly paused or resumed.

share|improve this answer

Your Answer


