Skip to content

ERA – Update

November 29, 2010

Today I have been working on:

  • Added a basic menu
  • Cleaning up my collision detection class
  • Added portals

I’m really happy with my new collision detection class I wrote, it cleans everything up nicely and reduces the amount of if statements. What all the other detection classes I’ve seen do is validating by the userdata names. I have done the same thing, but made a dynamic function which validates the shapes against the names and dispatches an event if the declared names are matched. This is what it looks like:

/* Pass the first object name, second object name, this method will validate if the first and second names match to the collisions, if it does it will
* dispatch the event passed. It will also attach the _shape1 and _shape2 as parameters to the event.
*/
private function validate_collision(_first_name:String, _second_name:String, _event_to_dispatach:String, _shape1:b2Shape, _shape2:b2Shape):void {
  var shape_1_user_data:Object = _shape1.GetBody().GetUserData();
  var shape_2_user_data:Object = _shape2.GetBody().GetUserData();
  if(shape_1_user_data.obj_name == _first_name || shape_1_user_data.obj_name == _second_name) {
     if(shape_2_user_data.obj_name == _first_name || shape_2_user_data.obj_name == _second_name) {
        if(shape_1_user_data.obj_name == _first_name) {
          _bHolder.dispatchEvent(new BionicEvent(_event_to_dispatch, _shape1.GetBody()));
        } else {
          _bHolder.dispatchEvent(new BionicEvent(_event_to_dispatch, _shape2.GetBody()));
        }
     }
   }
}

Then all you do is validate each object you want, for example, these 2:

// Check if the enemy has been hit by a bullet, if it has, dispatch bEvent.DAMAGE_ENEMY event
validate_collision('enemy', 'bullet', bEvent.DAMAGE_ENEMY, shape1, shape2);

// Check if the character has hit an enemy, if he has, dispatch the bEvent.DAMAGE_CHARACTER event
validate_collision('main_character', 'enemy', bEvent.DAMAGE_CHARACTER, shape1, shape2);

Here is my latest progress: http://www.willdonohoe.com/university/idat313/mutant_labs/era/update_29_11_10/

Note: There is a slight bug where when you click on play, you need to click on the stage for the game to register the keyboard events, just click on the stage anywhere to make it work, I will fix this soon.

Cheers,

Will

Leave a Comment

Leave a comment