Game Development Reference
In-Depth Information
The first thing to look at in this method is the parameter being passed to the method. Here
you have a contact parameter with a type of
SKPhysicsContact
. The
SKPhysic-
sContact
contains all of the information you need to handle a node contact. The
SKPhysicsContact
class contains five properties to help you determine the character-
istics of the contact. Each of these is defined in
Table 3-2
.
Method
Purpose
The
bodyA
property, an
SKPhysicsBody
, represents first body in the con-
tact. (This will be the
playerNode
.)
bodyA
The
bodyB
property, an
SKPhysicsBody
, represents second body in the
contact. (This will be the
orbNode
.)
bodyB
The
contactPoint
, a
CGPoint
, represents the contact point between the
two physics bodies in the scene coordinates.
contactPoint
The
contactImpulse
, a
CGFloat
, specifies how hard these two bodies
struck each other using newton-seconds as the unit of measure.
collisionImpulse
The
collisionNormal
, a
CGVector
, specifies the direction of the colli-
sion.
collisionNormal
You are interested only in the second of these properties,
bodyB
. Getting back to the
didBeginContact()
method, notice the second line of the method. Here you are ref-
erencing the contact's
bodyB
property, which as I mentioned earlier is an
SKPhysic-
sContact
. Once you have this reference, you can then get the
SKNode
instance from
the
bodyB.node
property, which represents the second node in the collision.
Once you have the second
SKNode
in the collision, you can check to see whether it has a
name of
POWER_UP_ORB
. If it does, then you invoke the node's
removeFromPar-
ent()
method, which, as it sounds, removes the
orbNode
from the scene.
Go ahead and replace the contents of the current
didBeginContact()
with this ver-
sion and run the app one last time. This time, when the
playerNode
collides with the
orbNode
, the
orbNode
disappears from the scene.
Listing 3-2
.
GameScene.swift: The SuperSpaceMan GameScene with Collison Detection