(No. 00004) implementing the brick-hitting game for iOS (13th): It is scalable and I am a golden hoop (bottom )!
Prepare materials for shortening the bounce stick
Similar to the previous article, we have developed a material that shortens the size of the rebound rod.
Open SpriteBuilder, create the StickShorter. ccb file, and draw its sprite frame and physical object as follows:
Vc/y0rK74bHktPMs1eK + zbK7ysfO0sPHz + vSqrXEwcsuPC9wPg0KPGgyIGlkPQ = "Create a shortened item Star"> Create a shortened item star
We use red stars to shorten the item, so spawStar writes the following:
case brkColorRed: star = [Star starWithType:starTypeStickShorter]; break;
In GameScene. m, add the following code to the code for the collision between stars and bounce Rods:
case starTypeStickShorter: @synchronized(self){ [self scheduleBlock:^(CCTimer *timer){ [Star doStickShorterWork:self.stickInGameScene]; } delay:0]; } break;
Now, let's go back to Star. m and add the doStickShorterWork method:
+(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];}
You can compare it with the variable-length code, which is basically the same.
Compile and run the game as follows:
You can add more special effects in shortening and increasing length. Open the brain hole, kids shoes ;)