public
Last active

  • Download Gist
GameScene.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
// 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.