大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;)
创建游戏场景类头文件
在Xcode创建新GameScene类,继承于CCNode,将其头文件替换为以下内容:
#import "CCNode.h"
@class Stick;
@interface GameScene : CCNode <CCPhysicsCollisionDelegate>
//读取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序