I was reading and trying some of the code from this article (Cocos2D iphone tutorial: Die, Grossini, Die! – Part I) about some basic Cocos2D stuffs. One of the things I have is to create a clickable "Restart" text as below:
-(void)stateChange_gameOverDisplay {
CCLOG(@"inside stateChange_gameOverDisplay");
[CCMenuItemFont setFontName:@"Marker Felt"];
[CCMenuItemFont setFontSize:30];
CCMenuItem *play_menu = [CCMenuItemFont itemFromString:@"Restart" target:self selector:@selector(restartGame:)];
CCMenu *menu =[CCMenu menuWithItems:play_menu,nil];
menu.anchorPoint=ccp(0,0);
menu.position = ccp(280, 20);
[self addChild:menu];
CCLOG(@"end of stateChange_gameOverDisplay");
}
But when I run the code, there's nothing been displayed and it just keep looping and looping as below:
inside stateChange_gameOverDisplay
inside stateChange_gameOverDisplay
inside stateChange_gameOverDisplay
inside stateChange_gameOverDisplay
inside stateChange_gameOverDisplay
inside stateChange_gameOverDisplay
inside stateChange_gameOverDisplay
It took me a long time to work out it's because when I specify the selector, there's an extra ":" behind the method name.
@selector(restartGame:)
But I declared the method incorrectly as below:
-(void)restartGame {
CCLOG(@"inside restartGame");
//do something here
}
The correct way should be as below, with the "(id) sender" parameter
-(void)restartGame: (id) sender {
CCLOG(@"inside restartGame");
//do something here
}
I guess it's probably because the "@selector" thing can't find the correct method to call? But as there's no error or warning, it might be a bit difficult to relate the symptom to the root cause...
Back in June, I wrote in this post about crash caused by missing ":", I guess this one is also similar, but from different angle, somehow.
So one more tips for everyone if you get similar strange behaviour like this...
0 comments:
Post a Comment