0
votes

I have a UITabBarController that contains two view controllers. One is the default view controller created when Xcode creates a new "Tabbed Application". The other, I call my cocos2d view controller, is a UIViewController which contains a single CCGLView, which occupies the whole nib. It has a single CCScene and a single CCLayer within that scene and a single CCSprite within that CCLayer, which moves back and forth across the layer forever.

I run the program with the cocos2d view controller being displayed first and the sprite moving happily. When I select the second tab and come back, the sprite is no longer moving. Tabbing back and forth more doesn't make it move again. I set a breakpoint inside didSelectViewController and examined the sharedDirector. It is not paused, it has a framerate and everything.

I also have a separate tabbed app that implements ccTouchesBegan and moves the layer around according to the users gestures. In that app, the touches are still detected, the position of the layer is changed, but the view on the device/simulater never changes. Even if I change the position, tab away and back, the original non moved image is shown.

Question: Why does the cocos2d view apparently stop it's render loop?

Here's what I've got in the cocos2d view controller of my test app:

- (void)viewDidLoad
{
    [super viewDidLoad];
    director = [CCDirector sharedDirector];
    [director setDisplayStats:YES];

    [self runCocos2d];
}

- (void) runCocos2d
{
    glView.multipleTouchEnabled = YES;
    [[CCDirector sharedDirector] setView:self.glView];

    scene = [TestScene node];
    [[CCDirector sharedDirector] pushScene:scene];
    [scene startScene];
}

StartScene just calls this method in my layer

- (void) start
{
    CGSize winSize = [CCDirector sharedDirector].winSize;

    CCSprite *seeker = [CCSprite spriteWithFile:@"seeker.png"];
    seeker.position = ccp(0, winSize.height/2);

    [self addChild: seeker z:0];

    // move the table constantly so we can see if anything it rendering
    id moveToPosition1 = [CCMoveTo actionWithDuration: 3
                                         position: ccp(winSize.width, winSize.height/2)];
    id moveToPosition2 = [CCMoveTo actionWithDuration: 3
                                         position: ccp(0, winSize.height/2)];
    id sequence = [CCSequence actions:moveToPosition1, moveToPosition2, nil];

    id moveOverAndOver = [CCRepeatForever actionWithAction:sequence];

    [seeker runAction: moveOverAndOver];
}
1

1 Answers

1
votes

I fumbled around with calling different methods on the shared director and found that calling startAnimation when the cocos2d view controller was selected, would make it all work correctly. I Googled for a good while and didn't come up with that, so hopefully this'll save someone some time.