-
Notifications
You must be signed in to change notification settings - Fork 66
/
how_to_detect_the_Collisions.html
146 lines (132 loc) · 5.59 KB
/
how_to_detect_the_Collisions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<link href="./markdown.css" rel="stylesheet"></link>
<h1>How to Detect the Collisions</h1>
<hr />
<p>Our hero could fire bullets now, but the bullets are only visually there, how could they kill the enemies?</p>
<p>In this chapter, we will introduce Collision Detection to implement it.</p>
<p>Firstly, it's necessary to track the enemies and the bullets.</p>
<p>In the game, we add a tag for these two kinds of sprites to identify them. tag = 1 stands for a enemy, 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 inherited them, which we could utilize.</p>
<p>Add the two member variabls below to HelloWorld in HelloWorldScene.h, these two member variables are used to store the existing enemies and bullets.</p>
<pre><code> // cpp with cocos2d-x
protected:
cocos2d::CCMutableArray<cocos2d::CCSprite*> *_targets;
cocos2d::CCMutableArray<cocos2d::CCSprite*> *_projectiles;
</code></pre>
<p>Then initialize the two variables in the construct function, new them in init(), and release them in the destruct function.</p>
<pre><code> // cpp with cocos2d-x
// in init()
// Initialize arrays
_targets = new CCMutableArray<CCSprite*>;
_projectiles = new CCMutableArray<CCSprite*>;
HelloWorld::~HelloWorld()
{
if (_targets)
{
_targets->release();
_targets = NULL;
}
if (_projectiles)
{
_projectiles->release();
_projectiles = NULL;
}
// cpp don't need to call super dealloc
// virtual destructor will do this
}
HelloWorld::HelloWorld()
:_targets(NULL)
,_projectiles(NULL)
{
}
</code></pre>
<p>Now modify addTarget() to add a new target to targets array, and set its tag to be 1.</p>
<pre><code> // cpp with cocos2d-x
// Add to targets array
target->setTag(1);
_targets->addObject(target);
</code></pre>
<p>Modify ccTouchesEnded() to add a new bullet to bullets array, and set its tag to be 2.</p>
<pre><code> // cpp with cocos2d-x
// Add to projectiles array
projectile->setTag(2);
_projectiles->addObject(projectile);
</code></pre>
<p>Then, modify spriteMoveFinished() as follows. Here we remove the sprites from their corresponding arrays. <br />
</p>
<pre><code> // cpp with cocos2d-x
void HelloWorld::spriteMoveFinished(CCNode* sender)
{
CCSprite *sprite = (CCSprite *)sender;
this->removeChild(sprite, true);
if (sprite->getTag() == 1) // target
{
_targets->removeObject(sprite);
}
else if (sprite->getTag() == 2) // projectile
{
_projectiles->removeObject(sprite);
}
}
</code></pre>
<p>The function update() below is used to detect collisions every frame, remove the collided bullet and enemy from the scene.
Declare it in HelloWorldScene.h and Define it in HelloWorldScene.cpp.</p>
<pre><code> // cpp with cocos2d-x
void HelloWorld::update(ccTime dt)
{
CCMutableArray<CCSprite*> *projectilesToDelete =
new CCMutableArray<CCSprite*>;
CCMutableArray<CCSprite*>::CCMutableArrayIterator it, jt;
for (it = _projectiles->begin(); it != _projectiles->end(); it++)
{
CCSprite *projectile =*it;
CCRect projectileRect = CCRectMake(
projectile->getPosition().x
- (projectile->getContentSize().width/2),
projectile->getPosition().y
- (projectile->getContentSize().height/2),
projectile->getContentSize().width,
projectile->getContentSize().height);
CCMutableArray<CCSprite*>*targetsToDelete
= new CCMutableArray<CCSprite*>;
for (jt = _targets->begin(); jt != _targets->end(); jt++)
{
CCSprite *target =*jt;
CCRect targetRect = CCRectMake(
target->getPosition().x - (target->getContentSize().width/2),
target->getPosition().y - (target->getContentSize().height/2),
target->getContentSize().width,
target->getContentSize().height);
if (CCRect::CCRectIntersectsRect(projectileRect, targetRect))
{
targetsToDelete->addObject(target);
}
}
for (jt = targetsToDelete->begin();
jt != targetsToDelete->end();
jt++)
{
CCSprite *target =*jt;
_targets->removeObject(target);
this->removeChild(target, true);
}
if (targetsToDelete->count() >0)
{
projectilesToDelete->addObject(projectile);
}
targetsToDelete->release();
}
for (it = projectilesToDelete->begin();
it != projectilesToDelete->end();
it++)
{
CCSprite* projectile =*it;
_projectiles->removeObject(projectile);
this->removeChild(projectile, true);
}
projectilesToDelete->release();
}
</code></pre>
<p>Ok, the last thing we should do is adding update() to the schedule to let it be called every frame.</p>
<pre><code> // cpp with cocos2d-x
this->schedule( schedule_selector(HelloWorld::update) );
</code></pre>
<p>Compile and run the project, fire the bullets as you like, then: AH..AH.., the enemies are killed one by one.</p>