Back in Xcode, add the collision protocol to the MainScene.h interface:
@interface MainScene : CCNode <CCPhysicsCollisionDelegate>//...@end
Then open the collision proxy in the MAINSCENE.M initialization method:
_physicWorld = (CCPhysicsNode*)[self getChildByName:@"physicWorld" recursively:YES]; NSAssert(_physicWorld, @"physicWorld must not nil"); _physicWorld.collisionDelegateself;
Because we are dealing with collisions between bullets and sensors, we need to add a corresponding collision callback method:
-(BOOL) Ccphysicscollisionbegin: (Ccphysicscollisionpair *) pair bullet: (Ccnode *) bullet sensor: (Ccnode *) sensor{ Ccphysicsbody *phybullet = Bullet. Physicsbody; Phybullet. Collisiontype= @"NULL"; [ Selfscheduleblock:^ (Cctimer *timer) {Bullet. Visible=NO; Bullet. Position= CCP (0,0); } Delay:5]; _score++; _scorelabel. String= [NSStringstringwithformat:@"score:%d", _score];return NO;}
The physical object of the bullet is first obtained, and then the collision type is changed to @ "null", because the following situation may occur:
The bullet enters the basket first in contact with the sensor, but may hit the inner wall of the basket to bounce upward away from the sensor, and then fall across the sensor, causing a bullet to enter the basket multiple times to score.
After the collision model is set to @ "null", the bullet that has touched the sensor will no longer collide with the sensor. Of course it's just a solution.
Next we wait for 5 seconds to make the bullet invisible and move it out of the basket, which will not affect the other shot, and can be reused.
Final scoring, and update the score labels in the HUD layer.
Compile and run the game, the final effect is as follows:
It omits the steps to make the HUD layer, after a few exercises this should no longer talk.
This game is here for the time being, the next game no.00004 will develop a game similar to pinball playing bricks, see;)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(no.00003) iOS games simple robot projection game forming (21)