Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – Detecting collisions


We will use the hitTestObject function that every display object has. It allows one to test if two display objects are colliding (this is based on the bounds of these objects, so if you are looking for something that's pixel perfect, then you should use something else.)

   private static function updatePositions()
   {
      for(d in droppables)
      {
         d.y += 10;
         if(d.y>= 450)
         {
            droppables.remove(d);
            flash.Lib.current.stage.removeChild(d);
            continue;
         }
         //Test for collisions
         if(player.hitTestObject(d))
         {
            if(Std.is(d, Bomb))
            {
               trace("GAME OVER");
               timer.stop();
               var go = new Gameover();
               go.x = 0;
               go.y = 150;
               flash.Lib.current.stage.addChild(go);
            } else
            {
               trace("Score + 1");
               droppables.remove...