Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

Understanding NAPE collision detection

One of the features which highly contribute to the success of a physics engine is the collision detection.

NAPE has a robust collision detection, although you won’t find that much information around the web, that’s why this blog is a must read :)

To explain the basics of NAPE collision detection, I stripped the Hundreds game prototype and now I only have one ball running around the stage and bouncing off walls.

I will show you how to detect collision between the ball and the walls.

Look at the result:

Each time the balls bounce off the walls, the stage color changes.

This is the script:

Let’s comment line by line what’s involved in collision detection:

Line 20: defining an interaction listener. Since a collision is an interaction, we need to define it at first.

Lines 21-22: these are Callback Types, and I am going to use them in a smart way to detect only some kind of collisions. Think about a Callback Type like a built-in Box2D collision filtering, if you are used to it.

Line 25: the core of the script: the Interaction Listener. You can handle any type of interaction with it, but since I am looking for collisions, here is the list of arguments:

CbEvent.BEGIN: the event type to listen for, in this example the begin of the event.

InteractionType.COLLISION: the interaction type to listen for, in this example a collision.

wallCollisionType: the OptionType to match first Interactor against, in this case the wall collision type

ballCollisionType: the OptionType to match second Interactor against, in this case the ball collision type

ballToWall: the callback function

Line 26: at this time, the newly created listeners is added to the NAPE world.

Line 44: this is how I add the ball to its Callback Type

Line 58: same thing for the wall.

And that’s it. No need to download any source code, just copy/paste it in the Hundreds game prototype source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 4.30 out of 5)
Loading ... Loading ...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 4 comments

  1. Understanding NAPE collision detection – Emanuele Feronato « eaflash

    on February 4, 2013 at 12:26 pm

    […] on http://www.emanueleferonato.com Share this:TwitterFacebookLike this:LikeBe the first to like […]

  2. Walter Torres

    on May 11, 2013 at 4:02 am

    Thank you, this helped! but I have a question for you, do you know how to add your own object to the “collision:InteractionCallback” in order to affect objects beyond the scope of nape? such as the hp of a character when it gets hit by a projectile.

  3. Justin Livi

    on June 5, 2013 at 11:54 pm

    Walter, Nape body objects have a property called userData (though for some reason it doesn’t appear in the documentation) but you can put any user created data in it.

    Just access it like so:

    myBody.userData.myUniqueVariable = myCharacter;

    Hope that helps!

  4. Walter Torres

    on June 28, 2013 at 7:33 am

    Hey Justin, I am glad to say I was able to get to that exact solution, thanks a lot :)

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×