Book Image

Learning iOS 8 Game Development Using Swift

By : Siddharth Shekar
Book Image

Learning iOS 8 Game Development Using Swift

By: Siddharth Shekar

Overview of this book

<p>Game development has been simplified with Apple's new programming language—Swift. If you're looking to start learning iOS development then you'll get everything you need - from&nbsp;the absolute basics such as the Xcode interface and takes you all the way to Swift programming.</p> <p>You will take a walk through the creation of 2D and 3D games followed by an introduction to SpriteKit and SceneKit. The book also looks at how game objects are placed in 3D scenes, how to use the graphics pipeline, and how objects are displayed on mobile screens. You will also delve into essential game concepts such as collision detection, animation, particle systems, and scene transitions. Finally, you will learn how to publish and distribute games to the iTunes store.</p>
Table of Contents (18 chapters)
Learning iOS 8 Game Development Using Swift
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Collision detection


Collision detection in 2D games is done using the intersectsNode function of the sprite class itself. We can check whether the current sprite is overlapping the other sprite. In the intersectsNode function of the current node, we pass in a node that we want to check for collision with the current node. If there is an intersection, the function will return true, if not, it returns false.

For checking collision, we will first check the collision between the enemy's bullets and the hero. If there is collision, then the game is over. Then we will check collision between the hero's rockets and the enemies. If we detect a collision, then we have to update the score.

Also, if the enemy goes beyond the left of the screen, the game is over, so we will call the GameOver function in this case also.

For checking collision, create a new function called checkCollision in the GameplayScene and add the following code:

//Hero and Bullets
 
for bullet in bullets{
            
    var sprite...