Implementing Space Demon iPhone Game Part 4 (Scenes and Lives)
In the last article we started the battle by adding the spaceship and the demon to the game. Both the demon and the spaceship had the capabilities to defend themselves using firepower. In this article we are going to add lives for our spaceship and also make our home screen which will allow the user to start the game.
Displaying Lives on the Screen:
There are many ways to display lives on the screen. We will use smaller images of our spaceship to be displayed as lives. We will start by adding three lives and each time our spaceship is hit we will remove a life from the game. The following implementation is used drawLives on the view.
The "small_spaceship.png" is a small image of the spaceship which will represent the user's life. The drawLives method also takes number of lives into consideration. The Game layer is responsible for passing the number of lives to the drawLives method. Each spaceship is represented by a Sprite instance which is uniquely identified by a tag. The screenshot below shows the lives in action:
The lives are displayed on the upper left hand corner of the screen. The code below is triggered by the scheduler to check if the spaceship is hit or not.
The checkIfSpaceshitIsHit is constantly triggered throughout the game. Spaceship is considered hit when it collides with the demon or when it collides with the fireball emitted by the demon. Remember, the unique tag we assigned to our sprites representing the lives we used those tags to remove the life from the layer using the removeLifeByTag method. The removeLifeByTag method is implemented below:
We used the CCLayer removeChildByTag method to remove the CCNode from the layer. The cleanup attribute is set to YES which means that after the CCNode will be removed from the layer the memory occupied by the CCNode will be released. Run the application and allow the demon to kill you! You will notice that your ship will be destroyed and one life will be removed from the screen.
If you run the Space Demon game you will notice that the game immediately starts. This might be reasonable approach in some games but in Space Demon we need to create a home scene with menu. We can add a nice fire background image for home scene but who needs images when we have the power of the particles. In the implementation below we have added the particles effect to make our home scene background.
We also need to tell our delegate to load the HomeScene scene first. The following implementation shows that now we are loading the HomeScene first instead of the GameScene.
The complete video of the game is provided below:
In this article we focused on creating the HomeScene and lives for our spaceship. We moved one step closer to finishing our Space Demon game. In the next article we will introduce the scoreboard to the game.