(NO.00004)iOS實現打磚塊遊戲(十三):伸縮自如,我是如意金箍棒(下)!
準備縮短反彈棒素材
和上一篇類似,我們如法炮製一張縮短後反彈棒的素材.
開啟SpriteBuilder,建立StickShorter.ccb檔案,按繪製其sprite幀和物理對象:
vc/y0rK74bHktPMs1eK+zbK7ysfO0sPHz+vSqrXEwcsuPC9wPg0KPGgyIGlkPQ=="建立縮短道具星">建立縮短道具星
我們用紅色的星星表示縮短道具,所以spawStar中是這樣寫的:
case brkColorRed: star = [Star starWithType:starTypeStickShorter]; break;
在GameScene.m中,在星星與反彈棒碰撞的代碼中,加入如下代碼:
case starTypeStickShorter: @synchronized(self){ [self scheduleBlock:^(CCTimer *timer){ [Star doStickShorterWork:self.stickInGameScene]; } delay:0]; } break;
好了,最後我們回到Star.m中添加doStickShorterWork方法:
+(void)doStickShorterWork:(Stick *)stick{ GameScene *gameScene = [GameScene sharedGameScene]; CCPhysicsNode *physicsWorld = (CCPhysicsNode*)stick.parent; @synchronized(gameScene){ if ([stick.name isEqualToString:@stickShorter]) { return; } if ([stick.name isEqualToString:@stickLonger]) { Stick *stickNormal = [Stick stickNormal]; stickNormal.position = stick.position; [stick removeFromParent]; //[physicsWorld removeChild:stick cleanup:YES]; [physicsWorld addChild:stickNormal]; gameScene.stickInGameScene = stickNormal; return; } } CGPoint position = stick.position; __block Stick *stickShorter; @synchronized(gameScene){ stickShorter = [Stick stickShorter]; [stick removeFromParent]; //[physicsWorld removeChild:stick cleanup:YES]; stickShorter.position = position; [physicsWorld addChild:stickShorter]; stickShorter.visible = NO; gameScene.stickInGameScene = stickShorter; CCSprite *stickNode = (CCSprite*)[CCBReader load:@Elements/StickNode]; stickNode.position = stickShorter.position; [gameScene addChild:stickNode z:50]; CCActionScaleTo *shorterAction = [CCActionScaleTo actionWithDuration:0.4f scaleX:0.5f scaleY:1.0f]; CCActionCallBlock *blk = [CCActionCallBlock actionWithBlock:^{ [stickNode removeFromParent]; stickShorter.visible = YES; }]; CCActionSequence *seq = [CCActionSequence actions:shorterAction,blk,nil]; [stickNode runAction:seq]; } [stickShorter scheduleBlock:^(CCTimer *timer){ @synchronized(gameScene){ Stick *stickNormal = [Stick stickNormal]; stickNormal.position = stickShorter.position; [stickShorter removeFromParent]; [physicsWorld addChild:stickNormal]; gameScene.stickInGameScene = stickNormal; } } delay:10];}
大家可以和變長的對應代碼對比下,基本都是一樣的.
下面編譯運行遊戲,效果如下:
大家可以在變短和變長中添加更多的特效,腦洞開啟吧,童鞋們 ;)