I have written a method whose intent is to load the next level in a game. It finds the index of the level, loads it from the level loader (a class I wrote), creates a layer holding that level, then informs the scene to transition between the new and old level layers. The player sprite is turned invisible at this time, so that there are not two players during the transition.
We've run into a problem where occasionally, the player will be turned invisible but the transition will not happen. After doing a lot of digging, all I've found out is that the method is not finishing running, even though there are no returns or other calls in the method.
Method (as it is now with logs in to help monitor what is happening):
if([owningScene mayChangeLayer])
{
NSLog(@"May change layer");
levelFinished = YES;
NSLog(@"Level marked as finished");
[carver setVisible:NO];
NSLog(@"Carver hidden");
GameState *state = [GameState sharedGameState];
NSLog(@"game state loaded");
state.screenXOffset += xOffset;
state.screenYOffset += yOffset;
NSLog(@"offset: %d, %d", state.screenXOffset, state.screenYOffset);
GameLevel *aLevel = [loader getLevelAtXOffset:state.screenXOffset
atYOffset:state.screenYOffset];
NSLog(@"loaded level %@", aLevel);
GameLevelLayer *layer = [[GameLevelLayer alloc] initWithLevel:aLevel
withOwningScene:owningScene
withLevelLoader:loader
withHud:hud
showTitle:NO
startTileIndex:startIndex];
NSLog(@"Got layer %@", layer);
[owningScene replaceLayer:layer xMove:xOffset yMove:yOffset];
[layer release];
NSLog(@"Done");
Log messages:
2011-01-24 21:38:03.541 Squirrel[14659:307] May change layer
2011-01-24 21:38:03.544 Squirrel[14659:307] Level marked as finished
2011-01-开发者_运维知识库24 21:38:03.545 Squirrel[14659:307] Carver hidden
2011-01-24 21:38:03.547 Squirrel[14659:307] game state loaded
2011-01-24 21:38:03.550 Squirrel[14659:307] offset: 4, -22
It looks like the code is quitting out at GameLevelLayer *layer= ..., for no log messages appear after that, yet as you can see there are clearly no exits in the code after that point. Any idea what could be causing this issue?
Thanks in advance! -Stephen
This may or may not help someone else ... here is what was happening:
In the GameLevelLoader, a new level was being created. An object in that level was taller than I had expected, and so when it was being positioned randomly, I was using arc4random% a negative number, which was causing bad things to happen. I don't know why the app continued to run in an invalid state, but cocos2d projects tend to do that sometimes (even running after an assert fails). Fixing it to check for a value > 0 before doing the arc4random() worked.
精彩评论