(NO.00004)iOS實現打磚塊遊戲(十四):3球道具的實現

來源:互聯網
上載者:User

(NO.00004)iOS實現打磚塊遊戲(十四):3球道具的實現

 

 反彈棒變化道具實現前面已經介紹過了,我們下面可以在小球上做些文章,實現一個道具可以變出更多的小球出來.

我們稱之為3球道具:當反彈棒碰到該道具時,小球變為3枚,接下來你儘可能保持這些小球不掉落,這樣你可以得到比1個球時更多地分數.

開啟Xcode,在Star.m中的spawnStar方法條件中加入新的分支:

case brkColorPurple:            star = [Star starWithType:starTypeThreeBalls];            break;

因為在Star.m的方法中需要知道當前情境中小球的狀態,所以先在GameScene.h介面中添加2個新的方法:

-(void)addBall:(CCSprite*)ball;-(NSInteger)currentBallsCount;

回到GameScene.m中,擴充代碼增加一個新的執行個體變數,用來表示當前所有小球:

NSMutableArray *_balls;

接著我們實現上面新加的2個方法:

//在GameScene中添加小球,小球的位置必須已經被正確設定-(void)addBall:(CCSprite*)ball{    @synchronized(self){        [_physicsWorld addChild:ball];        [_balls addObject:ball];    }}-(NSInteger)currentBallsCount{    @synchronized(self){        return _balls.count;    }}

注意方法中都設定了同步,因為讀取時可能發生修改,如果不加同步,app可能會發生crash!

再回到Star.m中,我們添加關鍵的道具功能方法doThreeBalls:

+(void)doThreeBalls:(CGPoint)ballLocation{    GameScene *gameScene = [GameScene sharedGameScene];    if ([gameScene currentBallsCount] != 1) {        return;    }    CCSprite *ball2 = (CCSprite*)[CCBReader load:@Elements/Ball];    ball2.position = ballLocation;    [gameScene addBall:ball2];    CCSprite *ball3 = (CCSprite*)[CCBReader load:@Elements/Ball];    ball3.position = ballLocation;    [gameScene addBall:ball3];}

代碼首先檢查當前情境中有多少個小球,如果多餘1個則直接返回,就是說該道具只能在只有1枚小球時發揮作用.

接著建立另外2個小球,通過之前定義的addBall方法,把它們添加到情境中去.

最後在GameScene.m的碰撞處理中增加新選擇分支:

case starTypeThreeBalls:            @synchronized(self){                if ([self currentBallsCount] >= 1) {                    [self scheduleBlock:^(CCTimer *timer){                        [Star doThreeBalls:((CCSprite*)_balls[0]).position];                    } delay:0];                }            }            break;

現在編譯串連app,運行效果如下:

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.