How To Make a Catapult Shooting Game with Cocos2D and Box2D Part 2

This is a blog post by iOS Tutorial Team member Gustavo Ambrozio, a software engineer with over 20 years experience, including over three years of iOS experience. He is the founder of CodeCrop Software. You can also find him on Google+. This is the second part of a two part tutorial series where we’ll build […] By Ray Wenderlich.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Contents

Hide contents

Finishing Touches

Before we wrap this up let’s add a way to reset everything in case we run out of bullets or enemies. This is gonna be pretty easy as we have most of our scene creation in separate methods anyway.

The best way to reset our game would be to call resetGame. But if you simply do this you’ll end up with a bunch of old targets and enemies on your scene. So we’ll have to add some cleanup to our resetGame method to take care of this. Fortunately we have kept references of everything so it’s gonna be pretty easy.

So go ahead and add this to the beginning of the resetGame method:

// Previous bullets cleanup
if (bullets)
{
    for (NSValue *bulletPointer in bullets)
    {
        b2Body *bullet = (b2Body*)[bulletPointer pointerValue];
        CCNode *node = (CCNode*)bullet->GetUserData();
        [self removeChild:node cleanup:YES];
        world->DestroyBody(bullet);
    }
    [bullets release];
    bullets = nil;
}

// Previous targets cleanup
if (targets)
{
    for (NSValue *bodyValue in targets)
    {
        b2Body *body = (b2Body*)[bodyValue pointerValue];
        CCNode *node = (CCNode*)body->GetUserData();
        [self removeChild:node cleanup:YES];
        world->DestroyBody(body);
    }
    [targets release];
    [enemies release];
    targets = nil;
    enemies = nil;
}

This is pretty simple. We just go through the sets we have and remove the bodies and the associated sprites from the scene. The conditional is because we will not have the sets defined the first time we call this method because we didn’t create anything yet.

Now let’s call resetGame at the appropriate times. If you remember we left some conditions blank on our resetBullet method. Well, this is the appropriate place. So go there and replace the two comments we left about doing something later for this:

[self performSelector:@selector(resetGame) withObject:nil afterDelay:2.0f];

Run the game and you’ll see that when the enemies or the bullets run out the game will reset and you’ll be able to play it again without having to re-run the project.

Let’s add just one more nice little detail to our game. When the game starts you can’t see the targets so you don’t know what you have to destroy. Let’s fix this, again, changing something on resetGame.

At the end of resetGame we call 3 methods:

[self createBullets:4];
[self attachBullet];
[self attachTargets];

Let’s change this a bit and add some action:

[self createBullets:4];
[self createTargets];
[self runAction:[CCSequence actions:
                 [CCMoveTo actionWithDuration:1.5f position:CGPointMake(-480.0f, 0.0f)],
                 [CCCallFuncN actionWithTarget:self selector:@selector(attachBullet)],
                 [CCDelayTime actionWithDuration:1.0f],
                 [CCMoveTo actionWithDuration:1.5f position:CGPointZero],
                 nil]];
 

Now we’ll create the bullets and targets and then start a sequence of actions. These actions will run one after the other.

The first will move our scene to the right so we can see our targets. When this movement finishes we’ll call the method that attaches the bullet. This will make the bullet get attached when we’re not seeing it so we’ll avoid the very crude attachment we have now. Then we’ll wait a second so you can get your strategy straight.

And finally… we’ll move back to the left so you can start the destruction!

Want More?

I had a blast working on this tutorial, and everyone I showed it to really liked it (or are all really nice to me!)

If you guys are interested, I’d love to keep working on this some more and make a full “Catapult Game Starter Kit” for sale on this site, kind of like the Space Game Starter Kit.

The full Catapult Game Starter Kit would take the above tutorial and game and expand it to include the following features:

  • Adding finger scrolling to review the scene
  • New artwork and enemies!
  • Sound effects, music, and animation!
  • Tons polish to make the game feel better
  • Using LevelHelper to easily create multiple levels
  • Adding a neat level selection scene
  • Adding a title scene and main menu
  • Creating power ups
  • Scores and Game Center support!
  • Anything else you guys want (post if you have an idea!)

Before I put the time and energy into creating this though, I’d like to check if this is something you’re interested in.

Could you please answer the following poll to let me know if this is something you’d like?

[poll id=”20″]

It’s OK if you’re not interested, so please answer honestly! I enjoy working on this but have plenty of other stuff to keep me busy :]

Where To Go From Here?

The repository tag for this point in the tutorial is FinalTouches. You can download the whole project here or get it at any point here.

What a trip right? From a few block to some destruction and flying acorns in no time.

If you feel adventurous and want to work on this code some more there are some known issues that are somewhat easy to solve and that can be a good exercise. Check out the issues page of the repository.

If you have any questions or comments or want to join in on improving this game, please join the forum discussion below!


This is a blog post by iOS Tutorial Team member Gustavo Ambrozio, a software engineer with over 20 years experience, including over three years of iOS experience. He is the founder of CodeCrop Software. You can also find him on .

Contributors

Over 300 content creators. Join our team.