(NO.00004)iOS實現打磚塊遊戲(五):遊戲情境類

來源:互聯網
上載者:User

(NO.00004)iOS實現打磚塊遊戲(五):遊戲情境類

 

建立遊戲情境類標頭檔

在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序

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.