Chapter 5 - How to Detect the Collisions - cocos2d-x
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Cocos2d-x » Tutorials »
Chapter 5 - How to Detect the Collisions
Our hero could fire bullets now, but the bullets are only adornments, how could they kill the monsters?
In this chapter, we will introduce Collision Detection to realize the effect.
Firstly, it’s necessary to trace the monsters and the bullets.
In the game, we add a tag for this two kinds of sprites to discriminate them. When tag = 1, it signs a monster, and tag = 2 means a bullet. Because there is already a member named m_nTag of CCNode, and the methods setTag() and getTag() also exist, CCSprite has inherit them, and we could utilize them.
Add the two member variabls below to HelloWorld in HelloWorldScene.h, this two member variables are used to cache the existing monsters and bullets.
1//c p p w i t h c o c o s2d-x
2p r o t e c t e d:
3c o c o s2d::C C M u t a b l e A r r a y
4c o c o s2d::C C M u t a b l e A r r a y
2N S M u t a b l e A r r a y*_t a r g e t s;
3N S M u t a b l e A r r a y*_p r o j e c t i l e s;
In cocos2d-x, CCMutableArray is realized as iOS SDK’s NSMutableArray, its members are NSObjects and their subclasses. Something different is that you should specify the concrete category of its members.
Then initialize the two variables in the construct function, new them in init(), and release them in the destruct function.
1//c p p w i t h c o c o s2d-x
2
3//i n i n i t()
4//I n i t i a l i z e a r r a y s
5_t a r g e t s=n e w C C M u t a b l e A r r a y
6_p r o j e c t i l e s=n e w C C M u t a b l e A r r a y
7
8H e l l o W o r l d::~H e l l o W o r l d()
9{
10i f(_t a r g e t s)
11{
12_t a r g e t s->r e l e a s e();
13_t a r g e t s=N U L L;
14}
15
16i f(_p r o j e c t i l e s)
17{
18_p r o j e c t i l e s->r e l e a s e();
19_p r o j e c t i l e s=N U L L;
20}
21
22//c p p d o n't n e e d t o c a l l s u p e r d e a l l o c 23//v i r t u a l d e s t r u c t o r w i l l d o t h i s
24}
25
26H e l l o W o r l d::H e l l o W o r l d()
27:_t a r g e t s(N U L L)
28,_p r o j e c t i l e s(N U L L)
29{
30}
1//o b j c w i t h c o c o s2d-i p h o n e
2//i n i n i t()
3//I n i t i a l i z e a r r a y s
4_t a r g e t s=[[N S M u t a b l e A r r a y a l l o c]i n i t];
5_p r o j e c t i l e s=[[N S M u t a b l e A r r a y a l l o c]i n i t]; 6
7-(v o i d)d e a l l o c
8{
9[_t a r g e t s r e l e a s e];
10_t a r g e t s=n i l;
11
12[_p r o j e c t i l e s r e l e a s e];
13_p r o j e c t i l e s=n i l;
14
15//d o n't f o r g e t t o c a l l"s u p e r d e a l l o c"
16[s u p e r d e a l l o c];
17}
Now modify addTarget() to add a new target to targets array, and set its tag to be 1.
1//c p p w i t h c o c o s2d-x
2//A d d t o t a r g e t s a r r a y
3t a r g e t->s e t T a g(1);
4_t a r g e t s->a d d O b j e c t(t a r g e t);1//o b j c w i t h c o c o s2d-i p h o n e 2//A d d t o t a r g e t s a r r a y
3t a r g e t.t a g=1;
4[_t a r g e t s a d d O b j e c t:t a r g e t];
Modify ccTouchesEnded() to add a new bullet to bullets array, and set its tag to be 2.
1//c p p w i t h c o c o s2d-x
2//A d d t o p r o j e c t i l e s a r r a y
3p r o j e c t i l e->s e t T a g(2);
4_p r o j e c t i l e s->a d d O b j e c t(p r o j e c t i l e);1//o b j c w i t h c o c o s2d-i p h o n e
2//A d d t o p r o j e c t i l e s a r r a y
3p r o j e c t i l e.t a g=2;
4[_p r o j e c t i l e s a d d O b j e c t:p r o j e c t i l e];
Then, modify spriteMoveFinished() as follows. Here we remove the sprites from their corresponding arrays.
1//c p p w i t h c o c o s2d-x
2v o i d H e l l o W o r l d::s p r i t e M o v e F i n i s h e d(C C N o d e*s e n d e r) 3{
4C C S p r i t e*s p r i t e=(C C S p r i t e*)s e n d e r;
5t h i s->r e m o v e C h i l d(s p r i t e,t r u e);
6
7i f(s p r i t e->g e t T a g()==1)//t a r g e t
8{
9_t a r g e t s->r e m o v e O b j e c t(s p r i t e);
10}
11e l s e i f(s p r i t e->g e t T a g()==2)//p r o j e c t i l e
12{
13_p r o j e c t i l e s->r e m o v e O b j e c t(s p r i t e);
14}
15}
1//o b j c w i t h c o c o s2d-i p h o n e
2-(v o i d)s p r i t e M o v e F i n i s h e d:(i d)s e n d e r
3{
4C C S p r i t e*s p r i t e=(C C S p r i t e*)s e n d e r; 5[s e l f r e m o v e C h i l d:s p r i t e c l e a n u p:Y E S]; 6
7i f(s p r i t e.t a g==1)//t a r g e t
8{
9[_t a r g e t s r e m o v e O b j e c t:s p r i t e];
10}
11e l s e i f(s p r i t e.t a g==2)//p r o j e c t i l e 12{
13[_p r o j e c t i l e s r e m o v e O b j e c t:s p r i t e]; 14}
15}
The function update() below is used to detect collisions every frame, remove the collided bullet and monster from the scene. Declare it in HelloWorldScene.h and Define it in HelloWorldScene.cpp.
1//c p p w i t h c o c o s2d-x
2v o i d H e l l o W o r l d::u p d a t e(c c T i m e d t)