Game Development Reference
In-Depth Information
orbNode!.physicsBody!.dynamic = false
addChild(orbNode!)
As you look over this snippet, you will see that it looks similar to the code you used to
add the playerNode with a couple exceptions. First, the orbNode is being positioned
25 points from the top of the scene and a little to the left of the player. The second thing to
notice is that the orbNode.physicsBody.dynamic property is being set to false .
This is because you don't want this node to react to other nodes. You want the player-
Node to pass through the scene collecting power-up orbs to fuel its ascent. This is why
you have given the orbNode 's physicsBody a static volume.
Go ahead and run the app again. This time, tap the screen until the playNode collides
with the orbNode . Because the playerNode is a dynamic body, it bounces off and
goes spinning into the distance, and because the orbNode is a static body, it remains
constant.
This is pretty cool for so little coding. But before moving on to detecting the collision and
removing the orb, you need to do one more thing. As you will have noticed when the
playerNode collided with the orbNode , the playerNode started to spin. This is a
correct response in a lot of cases, but for this game you want the playerNode to just
keep blasting through the orbs without spinning off. Sprite Kit provides a simple method
of preventing these rotations. It does so with the physicsBody.allowsRotation
property. Add the following line of code immedietly before the playerNode is added to
the scene:
playerNode!.physicsBody!.allowsRotation = false
Now run the app again. This time when the player collides with the orb, it will bounce off
but will not spin.
Adding Collision Detection
You now have your playerNode and orbNode colliding and reacting to the collisions
properly. It is time to add explicit code that will detect collisions between the player and
the orb and remove the orb from the scene when they collide.
When you run your game, you do see the collision between the orb and player, but what
you want to do now is detect when that collision occurs and remove the orb that was part
Search WWH ::




Custom Search