In the last article we took the old code base and cleaned it up by using the principles of refactoring. Refactoring allowed us to make our code better and future proof for changes. In this article we are going to dive into the action and let our spaceship and Demon go into fight.

Recommended Reading:

In the last two article we created the background for our Space Demon game. The background used the magic of particle effects in Cocos2d. We also added a spaceship to the game. It is highly recommended that you check out the following two articles.

1) Implementing Space Demon iPhone Game Part 1

2) Implementing Space Demon iPhone Game Part 2 (Refactoring)

Moving the Spaceship:

Currently, the spaceship simply sits on the screen. We need to move the spaceship where the user taps. In the future version of the game we will make sure that the user is able to move the spaceship using the drag and drop behavior. The implementing below shows the move method of the Spaceship class.

The move method of the Spaceship instance is triggered inside the TouchesBegin event as shown below:

If you run the application you will notice that now the spaceship moves in the horizontal direction whenever you tap the screen. Our next step is to load up our spaceship with ammo and fire laser. Put on your seat belts because the ride is about to get a little bumpy!

Laser Me!

Our spaceship is quite useless until it has someway of protecting itself from the bad guys. In this section we are going to fire laser (kind of) from our spaceship. The fire method of the Spaceship is implemented below:

We have used the CCParticleMeteor class to create our laser. You can use any type of Particle effect you desire. You can even use the Particle Designer to create a custom particle effect. The laser starts at the same position as the spaceship and then goes upwards increasing the value of y-axis. The gravity of the laser is set to -90 so that the tail of the laser is towards the spaceship. Once the laser is exceeded the height of the screen we trigger a callback function which removes the laser from the screen.

There are other ways to remove the CCParticle from the view which we will cover in the future articles.

The destroyLaser method is pretty simply which is shown below:

The laser effect is triggered when the spaceship is touched. Run the code and see the effect in action. Pretty cool right! Finally, we have a freaking laser gun mounted on our spaceship! Currently, our spaceship is feeling like a king in the galaxy since there are no enemies. In the next section we are going to add enemies to the game and give our little spaceship a hard time.

Demon is in the Space:

The space is about to get crowded. In this section we are going to add a demon, bad guy to the space. You can use any image for the demon we are going to use the following image:

Scary right!

The demon is added inside the init method of the game layer. The implementation below shows how the demon is added to the game.

The above code will simply add the demon to the game. But the demon does not have any weapons and cannot defend itself against the attacks of the spaceship. Demon class also has a fire method which makes the demon throw fireballs at the spaceship. The fire method is implemented below:

To create a fireball we have again utilized the power of particles framework. This time we are using the CCParticleSun class to make our fireball. To make things more interesting we are going to move our demon to random positions on the screen so it will be hard for our spaceship to hit it. The start method of the Demon class makes the demon move randomly.

We have used the arc4random method to generate the random x and y coordinates and then used Cocos2d actions to move the demon. If you run the application you will notice that currently the demon and the spaceship are immortal as they are not affected by the laser or the fireball. In the next section we are going to implement collision detection.

Help I am Hit!

We have refactored out the collision detection code into a separate class. The CollisionManager class is responsible for making sure if the CCNode objects have collided or not. The isCollided method of CollisionManager is shown below:

The CollisionManager is utilized inside the game layer class as shown below:

The hit method is responsible for destroying the spaceship or the demon. You can even have a health bar which looses health whenever any sprite is hit.

The game play video can be viewed below:


In this article we introduced the battle between the spaceship and the demon. We used particle effects to create laser and the fireball. We also used our custom made CollisionManager class to easily find the collision between two sprites. In the next article we are going to introduce the scoreboard which will be responsible for keeping track of the hits.