Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please tell me, if you feel good please support a lot of praise. Thank you! Hopy;)
Rebound bar change props implementation has been introduced before, we can do some articles on the small ball, to achieve a prop can be changed out more small ball.
We call it a 3-ball prop: When the bounce stick hits the prop, the ball turns into 3 pieces, and then you try to keep the balls from falling so you can get more points than the 1 balls.
Open Xcode and add a new branch to the Spawnstar method condition in STAR.M:
case brkColorPurple: star = [Star starWithType:starTypeThreeBalls]; break;
Because you need to know the state of the ball in the current scene in the Star.m method, add 2 new methods to the GameScene.h interface first:
-(void)addBall:(CCSprite*)ball;-(NSInteger)currentBallsCount;
Back in GAMESCENE.M, the extension code adds a new instance variable to represent all the current balls:
*_balls;
Then we implement the above 2 new methods:
//在GameScene中添加小球,小球的位置必须已经被正确设置-(void)addBall:(CCSprite*)ball{ @synchronized(self){ [_physicsWorld addChild:ball]; [_balls addObject:ball]; }}-(NSInteger)currentBallsCount{ @synchronized(self){ return _balls.count; }}
Note that synchronization is set in the method, because changes can occur while reading, and if not synchronized, the app may occur crash!
Back in star.m, we add the key prop function method Dothreeballs:
+(void)doThreeBalls:(CGPoint)ballLocation{ GameScene *gameScene = [GameScene sharedGameScene]; if1) { return; } load:@"Elements/Ball"]; ball2.position = ballLocation; [gameScene addBall:ball2]; load:@"Elements/Ball"]; ball3.position = ballLocation; [gameScene addBall:ball3];}
The code first checks how many balls are in the current scene, and if the extra 1 is returned directly, that means the prop can only function when there are only 1 balls.
Then create another 2 balls, and add them to the scene using the previously defined Addball method.
Finally, the new selection branch is added in GAMESCENE.M collision processing:
casestarTypeThreeBalls: @synchronized(self){ if ([self1) { [selfscheduleBlock:^(CCTimer *timer){ [StardoThreeBalls:((CCSprite*)_balls[0]).position]; delay:0]; } } break;
Now compile the connection app and run the following results:
In the next article, we will continue to implement a new missile star prop: You can launch a more reckless missile kill, the next goodbye;)
(no.00004) iOS implementation Brick games (14): 3-ball prop implementation