Game Development Reference
In-Depth Information
3. Now, replace the createLimitJointOnScene function call with cre-
ateCollisionDetectionOnScene . Add the following code at the end of
the init function:
[self createCollisionDetectionOnScene:self];
4. Also we have to subscribe to the delegate callbacks for contact detection. To sub-
scribe, add the following line of code:
self.physicsWorld.contactDelegate = self;
5. We will also have to declare the contact delegate in the interface file. So open
GameScene.h and add the following code at the end of interface declaration
line:
<SKPhysicsContactDelegate>
6. The final code file should look something similar to following screenshot:
7. Now compile and run the project and you should be able to see both the physics
bodies on the screen and logs on the Xcode debug window. You can see that the
collision has been detected and printed in the logs. You can also notice that the
collision has been detected between the bodies with category mask as 2 and 4 .
We have a rectangular body with mask as 2 and square body with mask as 4 ,
which are colliding with each other.
8. Now we will make the collision detection more intuitive. For this, let's add a label
on the screen, which will blink when the bodies collide with each other. To do
this, add the following line of code at the top just before the @implementa-
tion GameScene code:
Search WWH ::




Custom Search