在Xcode創建新GameScene類,繼承於CCNode,將其頭文件替換為以下內容:
#import CCNode.h
@class Stick;
@interface GameScene : CCNode
//讀取GameScene中Level中的反彈棒實例
@property (atomic,strong) Stick *stickInGameScene;
//
@property (nonatomic,assign) BOOL isStickShooting;
//返回GameScene當前實例
+(instancetype)sharedGameScene;
-(CCPhysicsNode*)physicsWorld;
-(void)addBall:(CCSprite*)ball;
-(NSInteger)currentBallsCount;
@end
其中添加了一些屬性和方法主要用來獲取和設置游戲場景中一些必要的物件,比如小球和反彈棒等等.因為場景中需要處理物理碰撞,所以添加了物理碰撞協議,我們在後面會用到.
在GameScene.m中實現初始化文件:
-(void)didLoadFromCCB{
//初始化內部靜態變量
sharedGameScene = self;
//加載當前Level節點
GameState *gameState = [GameState sharedInstance];
_level = (Level*)[CCBReader load:gameState.currentLevel];
[self addChild:_level];
_level.zOrder = 50;
//初始化GameScene中反彈棒實例變量
self.stickInGameScene = (Stick*)[self getChildByName:@stick recursively:YES];
NSAssert(self.stickInGameScene, @stickInGameScene must not nil);
//初始化得分比率
_scoreRatio = 1;
//取得Level中的物理世界對象
_physicsWorld = (CCPhysicsNode*)[self getChildByName:@physicsWorld recursively:YES];
NSAssert(_physicsWorld, @physicsWorld must not nil);
//打開物理調試顯示
_physicsWorld.debugDraw = YES;
//設置物理碰撞代理
_physicsWorld.collisionDelegate = self;
_userInterface.zOrder = 100;
_msgLabel.zOrder = 101;
self.userInteractionEnabled = YES;
self.zOrder = 0;
_balls = [NSMutableArray array];
_viewSize = [CCDirector sharedDirector].viewSize;
}
代碼大致做了如下這些事:
創建自身共享實例 添加當前level場景,level場景我們在後面博文中講述 初始化反彈棒和物理世界 設置各個場景的Z序