Lose Screens, Start Screens, Win Screens

iOS Simulator Screen Shot 11 May 2015 15.06.30
The start screen of the game

After deciding previously how we wanted Elias to win and to lose the game, it was time to implement this into the game. First I put in the start screen of the game, this was relatively simple as all I had to do was create a new Game Scene class, make the view controller display it and then make it display the main game scene when the screen is tapped using the same function I used to control the Elias jumping mechanic.

However putting in the win and lose functions was more tricky. For this I would need to detect collisions. After some research I decided the best way to do this would be to use bitwise operators.

Bitwise operators treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values.

(Mozilla Developer Network 2015)

Swift does not have class categories so the best way to compare two objects is to use bitwise operators. As they use bit patterns or binary numerals using bitwise operators comparisons can be performed far faster by the CPU than by decimal arithmetic or category comparison. This is important in gameplay as instant responses are key to making the game respond to player interaction. For example the game can quickly calculate if the player is touching the floor, and then allow them to jump.

//bitwise operators to allow player to contact different obstacles and for that to have different effects
let playerCategory: UInt32 = 1 << 0
let groundCategory: UInt32 = 1 << 1
let objectCategory: UInt32 = 1 << 2
let finishCategory: UInt32 = 1 << 3
let loseCategory: UInt32 = 1 << 4

Next I gave the objects, the ground and the player bitwise values. I also created two invisible objects a finish and a lose line. I placed the lose line behind the start of the screen and the finish line at the position of the cathedral.

  if (contact.bodyA.categoryBitMask & finishCategory) == finishCategory ||
            (contact.bodyB.categoryBitMask & finishCategory) == finishCategory {
                gameEnd(true)
        }

Then I added a function to check if the player was contacting these lines and called it in the update function so it would be called every frame.

fail

After doing this, Elias can get pushed to the back of the screen by the objects and this screen will show. However during testing I found that the “win” screen did not work. After a while, I realized I had forgotten to make the win line move backwards with the background and the objects. After adding in a function to make it do this it worked.

win

Again this showed me the importance of having another pair of eyes look at your code, as after you have been coding for a while you can miss very simple mistakes.

Mozilla Developer Network, 2015. Bitwise operators. [online] Available from: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators [Accessed 19 May 2015].