Creation of a Flash Stabilize! clone using Box2D – part 4

Here I am with the 4th part of this prototype. In part 3 we saw the “next” feature, this time we’ll trigger the collision among crates of the same color.

This will allow you to create the classic “match stuff of the same color to remove it from the game” or maybe something more creative.

In this example every crate has a counter, and every time a crate hits another crate of the same color, counters of both crates increase by one.

I already showed you how Box2D manages collisions but I made it directly editing the built in class.

It’s not the best practice as I needed to modify the original library.

The right practice is writing your own class extending b2ContactListener, then overriding the functions you need.

Let’s look at the main class:

at line 27 I am creating a new variable as custom_contact_listener class, then I simply set it as the contact listener at line 39

Now I can create a custom_contact_listener class in a file to extend b2ContactListener class and override existing functions.

This is how you extend a class… at line 10 I am declaring custom_contact_listener class extending b2ContactListener one.

Then at line 17 I override (think as “overwrite”) Persist function.

The remaining interesting code is already commented.

This is the result:

Download the source code.

Next time, more gameplay…