Created
October 10, 2009 10:39
-
-
Save Thomvis/206772 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by Thomas Visser on 10/10/2009. | |
#import "GameScene.h" | |
#import "LoadProgressLayer.h" | |
#import "GameLayer.h" | |
#import "Loader.h" | |
@implementation GameScene | |
- (id) init { | |
if( self = [super init] ) { | |
loadLayer = [[LoadProgressLayer alloc] init]; | |
[self addChild: loadLayer]; | |
gameLayer = [[GameLayer alloc] init]; | |
loadingCondition = [[NSCondition alloc] init]; | |
} | |
return self; | |
} | |
- (void) loadAndStart { | |
// should be called after the scene became the currentScene | |
// starts the loading of the game and makes sure the game is | |
// started when the loading is done | |
Loader * gameLoader = [[Loader loaderWithLoadable:gameLayer asynchronous:YES] retain]; | |
[gameLoader loadWithDelegate: self]; // loading starts in a new thread, | |
// the execution of this thread continues | |
[loadingCondition lock]; | |
[loadingCondition wait]; // the execution on this thread will pause | |
// untill [loadingCondition signal] is called | |
[loadingCondition unlock]; | |
[gameLoader release]; | |
// loading is done, show and start the game | |
[self removeChild:loadLayer cleanup:YES]; | |
[self addChild: gameLayer]; | |
[gameLayer startGame]; | |
} | |
# pragma mark LoadableDelegate | |
- (void) loadable: (id <Loadable>) ldble reportingProgress: (float) progrs { | |
// receiving load updates from the gameLoader created in loadAndStart | |
[loadLayer displayProgress: progrs]; // you could do this in GameScene itself | |
if( progrs == 1.0 ) { // the game is loaded | |
[loadingCondition lock]; | |
[loadingCondition signal]; | |
[loadingCondition unlock]; | |
// signals the other thread (the one that called loadAndStart that the | |
// gameLayer is loaded. Don't call [self addChild: gameLayer] and [gameLayer startGame] | |
// here because that will cause the game to be played in the Loader thread. | |
} | |
} | |
- (void) dealloc { | |
[loadingCondition release]; | |
[loadLayer release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment