OMGPOP
OMGPOP

Reputation: 942

cocos2d schedule selector outside class

I want to schedule a @selector(count) interval: 1.0f to count the time left. here is my code: (In GameManager.m file)

-(void) count {
duration++;
[[[GameScene sharedScene] gadgetLayer] updateTimerLabel];
if (timeLimit - duration <= 5 && ticking == NO) {
    ticking = YES;
    [self schedule:@selector(untick) interval:5];
    [[SimpleAudioEngine sharedEngine] playEffect:@"tick.caf"];
}
if (duration >= timeLimit) {
    [self lose];
}
}

gadgetLayer is where i put timerLayer and scoreLayer stuff. the the count is not scheduled in GameManager.m, instead, i put it in my GameScene.m file:

-(void) onEnter {
[[GameManager sharedManager] schedule:@selector(count) interval:1.0];
[super onEnter
}


- (void)onExit {
[[GameManager sharedManager] unschedule:@selector(count)];
[super onExit];
}

But the timerLabel won't change. The count method is in GameManager.m file, does it have to be inside GameScene.m file? Anything wrong with it?

+(GameManager*) sharedManager {
if (instanceOfGameManager == nil) {
    return [[GameManager alloc] init];
}
else return instanceOfGameManager;
}


-(id) init {
if ((self = [super init])) {
    instanceOfGameManager = self;

    [self scheduleUpdate];
}
return self;
}`


-(void) update: (ccTime) delta {
    int a = 2;

}

`

i set a breakpoint in 'int a = 2' line, but can not be reached. [GameManager sharedManager] is called in appDidFinishLaunching method, so it won't be alloc'ed and init'ed again i guess.

Upvotes: 0

Views: 1821

Answers (1)

Andrew
Andrew

Reputation: 24846

Actually I don't know why, but it work:

-(void) onEnter {
    GameManager *sharedManager = [GameManager sharedManager];
    [[CCScheduler sharedScheduler] scheduleSelector:@selector(count)
                                          forTarget:sharedManager
                                           interval:1.0
                                             paused:NO];

//    [self schedule:@selector(tick:) interval:0.5];
}

Answering next question:

Unscheduling using sharedScheduler works perfect. Your problem is that you don't receive touch events because you've forgot [super onEnter] (and by the way super onExit) in HelloWorld.m and super onEnter is the place where CCLayer makes self registration with touchDispatcher. If you will add this everything will work.

Upvotes: 1

Related Questions