English subtitles

← Specifying the Contact Listener - HTML5 Game Development

Get Embed Code
4 Languages

Showing Revision 6 created 05/25/2016 by Udacity Robot.

  1. Now, in our callback, we drill down to get the GetUserData function. This will
  2. actually give us a pointer to the entity that created these physics bodies that
  3. are touching. Now from here, we start down a new magical rabbit hole. For each
  4. of the objects that are touching, we need to signal, that a collision has
  5. occurred to it. We do this by calling a new function, called onTouch, and
  6. passing it in the other physics body that it's touching. So you can see that
  7. entity A gets an onTouch function called, passing in body B, and entity B gets
  8. an on touch function called passing in bodyA. This set up allows each
  9. individual entity to respond to collisions in their own way. So for instance,
  10. the onTouch of that for missile, would remove five health from the player it'
  11. colliding with. Or if it's just hitting a wall, it may just decide to
  12. self-destruct. But we haven't written this onTouch function yet. And in order
  13. to do that, we need to hop all the way back to entity.js and add an onTouch
  14. function. That allows other units to overload this function with custom logic
  15. later. It's probably worth pointing out that you should be doing a whole series
  16. of data checking to ensure that the information given to the post cell function
  17. is valid. So for example, whether or not body A and body B are valid objects or
  18. simply null. And whether or not the points of proper entity types and have
  19. onTouch functions. Now we'll let you graffle through all of these issues in the
  20. future. But we figured, that we let you get a glimpse at the madness behind the
  21. mask. So, that means that anyone who defines a physics body should define an
  22. onTouch function.