(no.00003) iOS games simple robot projection game forming (21)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.