How to detect collision in cocos2d-x

Source: Internet
Author: User

First, tracking monsters and bullets is required.

In the game, we use different tags for the two genie to differentiate them. When tag = 1, it indicates this is a monster, and when tag = 2, it indicates this is a bullet. Because m_nTag is a member variable in CCNode and the setTag and getTag methods are available, CCSprite inherits these methods and can be used.

In HelloWorldScene. h, add the following two member variables to HelloWorld, which are used to cache existing monsters and bullets.

1 // cpp with cocos2d-x

2 protected:

3 cocos2d: CCMutableArray * _ targets;

4 cocos2d: CCMutableArray * _ projectiles;

1 // objcwith cocos2d-iphone

2 NSMutableArray * _ targets;

3 NSMutableArray * _ projectiles;

In the cocos2d-x, CCMutableArray is equivalent to NSMutableArray in iOS SDK, and the Members in this array can be NSObject or their subclasses. But the difference is that you must tell it the specific type to be put.

Then initialize these two variables in the constructor, new them in init (), and release them in the destructor.

1 // cpp with cocos2d-x

2

3 // in init ()

4 // Initialize arrays

5_targets = new CCMutableArray;

6_projectiles = new CCMutableArray;

7

8 HelloWorld ::~ HelloWorld ()

9 {

10 if (_ targets)

11 {

12 _ targets-> release ();

13 _ targets = NULL;

14}

15

16 if (_ projectiles)

17 {

18 _ projectiles-> release ();

19 _ projectiles = NULL;

20}

21

22 // cpp don't need to call super dealloc

23 // virtual destructor will do this

24}

25

26 HelloWorld: HelloWorld ()

27: _ targets (NULL)

28, _ projectiles (NULL)

29 {

30}

1 // objcwith cocos2d-iphone

2 // in init ()

3 // Initialize arrays

4_targets = [[NSMutableArray alloc] init];

5_projectiles = [[NSMutableArray alloc] init];

6

7-(void) dealloc

8 {

9 [_ targets release];

10 _ targets = nil;

11

12 [_ projectiles release];

13 _ projectiles = nil;

14

15 // don't forget to call "super dealloc"

16 [super dealloc];

17}

Now you can modify addTarget (), add the new target to the target array, and set its tag to 1.

1 // cpp with cocos2d-x

2 // Add to targets array

3target-> setTag (1 );

4_targets-> addObject (target );

1 // objcwith cocos2d-iphone

2 // Add to targets array

3target. tag = 1;

4 [_ targets addObject: target];

Modify ccTouchesEnded (), add the new bullet to the bullet array, and set its tag to 2.

1 // cpp with cocos2d-x

2 // Add to projectiles array

3projectile-> setTag (2 );

4_projectiles-> addObject (projectile );

1 // objcwith cocos2d-iphone

2 // Add to projectiles array

3projectile. tag = 2;

4 [_ projectiles addObject: projectile];

Then, modify spriteMoveFinished () as follows (). Here, based on the tag, remove the genie from the corresponding array

1 // cpp with cocos2d-x

2 void HelloWorld: spriteMoveFinished (CCNode * sender)

3 {

4 CCSprite * sprite = (CCSprite *) sender;

5 this-> removeChild (sprite, true );

6

7 if (sprite-> getTag () = 1) // target

8 {

9 _ targets-> removeObject (sprite );

10}

11 else if (sprite-> getTag () = 2) // projectile

12 {

13 _ projectiles-> removeObject (sprite );

14}

15}

1 // objcwith cocos2d-iphone

2-(void) spriteMoveFinished :( id) sender

3 {

4 CCSprite * sprite = (CCSprite *) sender;

5 [self removeChild: sprite cleanup: YES];

6

7 if (sprite. tag = 1) // target

8 {

9 [_ targets removeObject: sprite];

10}

11 else if (sprite. tag = 2) // projectile

12 {

13 [_ projectiles removeObject: sprite];

14}

15}

The following update () function is used to detect the collision of each frame and delete the bullets and monsters in the collision from the game.

Declare in HelloWorldScene. h and define in HelloWorldScene. cpp.

1 // cpp with cocos2d-x

2 void HelloWorld: update (ccTime dt)

3 {

4 CCMutableArray * projectilesToDelete =

5 new CCMutableArray;

6 CCMutableArray: CCMutableArrayIterator it, jt;

7

8 for (it = _ projectiles-> begin (); it! = _ Projectiles-> end (); it ++)

9 {

10 CCSprite * projectile = * it;

11 CCRect projectileRect = CCRectMake (

12 projectile-> getPosition (). x

13-(projectile-> getContentSize (). width/2 ),

14 projectile-> getPosition (). y

15-(projectile-> getContentSize (). height/2 ),

16 projectile-> getContentSize (). width,

17 projectile-> getContentSize (). height );

18

19 CCMutableArray * targetsToDelete

20 = new CCMutableArray;

21

22 for (jt = _ targets-> begin (); jt! = _ Targets-> end (); jt ++)

23 {

24 CCSprite * target = * jt;

25 CCRect targetRect = CCRectMake (

26 target-> getPosition (). x-(target-> getContentSize (). width/2 ),

27 target-> getPosition (). y-(target-> getContentSize (). height/2 ),

28 target-> getContentSize (). width,

29 target-> getContentSize (). height );

30

31 if (CCRect: CCRectIntersectsRect (projectileRect, targetRect ))

32 {

33 targetsToDelete-> addObject (target );

34}

35}

36

37 for (jt = targetsToDelete-> begin ();

38 jt! = TargetsToDelete-> end ();

39 jt ++)

40 {

41 CCSprite * target = * jt;

42 _ targets-> removeObject (target );

43 this-> removeChild (target, true );

44}

45

46 if (targetsToDelete-> count ()> 0)

47 {

48 projectilesToDelete-> addObject (projectile );

49}

50 targetsToDelete-> release ();

51}

52

53 for (it = projectilesToDelete-> begin ();

54 it! = ProjectilesToDelete-> end ();

55 it ++)

56 {

57 CCSprite * projectile = * it;

58 _ projectiles-> removeObject (projectile );

59 this-> removeChild (projectile, true );

60}

61 projectilesToDelete-> release ();

62}

1 // objcwith cocos2d-iphone

2-(void) update :( ccTime) dt

3 {

4 NSMutableArray * projectilesToDelete

5 = [[NSMutableArray alloc] init];

6

7 for (CCSprite * projectile in _ projectiles)

8 {

9

10 CGRect projectileRect = CGRectMake (

11 projectile. position. x-(projectile. contentSize. width/2 ),

12 projectile. position. y-(projectile. contentSize. height/2 ),

13 projectile. contentSize. width,

14 projectile. contentSize. height );

15

16 NSMutableArray * targetsToDelete

17 = [[NSMutableArray alloc] init];

18

19 for (CCSprite * target in _ targets)

20 {

21

22 CGRect targetRect = CGRectMake (

23 target. position. x-(target. contentSize. width/2 ),

24 target. position. y-(target. contentSize. height/2 ),

25 target. contentSize. width,

26 target. contentSize. height );

27

28 if (CGRectIntersectsRect (projectileRect, targetRect ))

29 {

30 [targetsToDelete addObject: target];

31}

32}

33

34 for (CCSprite * target in targetsToDelete)

35 {

36

37 [_ targets removeObject: target];

38 [self removeChild: target cleanup: YES];

39}

40

41 if (targetsToDelete. count> 0)

42 {

43 [projectilesToDelete addObject: projectile];

44}

45 [targetsToDelete release];

46}

47

48 for (CCSprite * projectile in projectilesToDelete)

49 {

50

51 [_ projectiles removeObject: projectile];

52 [self removeChild: projectile cleanup: YES];

53}

54 [projectilesToDelete release];

55}

Well, the last thing is to add update () to schedule so that each frame can be called.

1 // cpp with cocos2d-x

2this-> schedule (schedule_selector (HelloWorld: update ));

1 // objcwith cocos2d-iphone

2 [self schedule: @ selector (update :)];

Compile and run the project and try to launch a bullet. At this moment, the monsters are all killed one by one.

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.