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;)
The previous post introduces the collision of small balls and bricks in physical objects, and in this article we look at what happens to the ball and the rebound stick;)
Ball and rebound stick start to collide
Also we have to adjust the moment of the ball in the collision, so we also have to separate processing, first of all, the process of collision start:
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair ball:(CCNode *)ball stick:(CCNode *)stick{ //一旦小球碰到反弹棒恢复得分比率 _scoreRatio = 1; return YES;}
We realized that the score ratio would be increased when the ball collided with the bricks multiple times, so here we reset the score ratio when the ball hits the rebound stick. That's all he has to do. You can actually put it in the end of the collision, and then completely remove the processing of the start collision.
Ball and rebound stick end collision
Next is the process of collision end:
-(BOOL) Ccphysicscollisionpresolve: (Ccphysicscollisionpair *) pair ball: (Ccnode *) ball Stick: (Ccnode *) stick{Ccphysicsbody * Phyball = Ball. Physicsbody;CgpointVelocity = Phyball. Velocity;NsintegerNeg = Arc4random_uniform (2);NsintegerRnd = Arc4random_uniform ( -);//When the ball hits the rebound stick, give the ball a random impulse if(Neg = =0) {Velocity. x+ = rnd; }ElseVelocity. x-= rnd; [Phyball applyimpulse:ccpmult (Velocity,1.3)];return YES;}
The method code is not long, but involves the operation of some physical objects, it is necessary to elaborate:
First the physical object of the ball is obtained, then a force of the ball is randomly generated, and then the impulse is applied to the ball. That ' s all!
The movement of the rebound stick
We are now going to implement the move function of the bounce stick, in gamescene.m, add the mobile touch method:
-(void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event{ //CCLOG(@"touch began from GameScene");}-(void)touchMoved:(CCTouch *)touch withEvent:(CCTouchEvent *)event{ CGPoint location = [[CCDirector sharedDirector] convertTouchToGL:touch]; @synchronized(self){ [self.stickInGameScene moveStickTo:location]; }}
Compile and run the app, now the game effect is as follows:
Okay, the game has a certain playability, and in the next one we go back to the level and let it build more bricks. See;) in the next chapter.
(no.00004) iOS implementation brick Game (ix): The game of the ball and rebound stick collision