QuickBox2D collision detection?

I'm trying to make a simple game, when the ball falls into certain block, you win.

Mechanics: The ball falls through several obstacles, in the end there are two blocks, if the ball touches the left block you win, the next level will contain more blocks and less space between them.

Test the movie (click on the screen to drop the ball):
http://gabrielmeono.com/downloads/Lucky_Hit_Alpha.swf

These are the main variables:

Code:
      var winBox:QuickObject;//You win
      var looseBox:QuickObject;//You loose
      var gameBall:QuickObject;//Ball dropped


Questions:

1) How do I trigger a collision function if the ball hits the winBox? (Win message/Next level).

2) How do I kill balls that get stuck/don't move before falling?

3) How do I stop dropping balls while there is one active gameBall?


Thanks, here is the full code:

Code:
        package  {
       
       import flash.display.MovieClip;
       import com.actionsnippet.qbox.*; 
       import flash.events.MouseEvent;
       
       [SWF(width = 600, height = 600, frameRate = 60)] 
   
       public class LuckyHit extends MovieClip {
          
          public var sim:QuickBox2D;
          var winBox:QuickObject;
          var looseBox:QuickObject;
          var gameBall:QuickObject;
          
          /**
           * Constructor
           */
          public function LuckyHit()
          {
             sim = new QuickBox2D(this); 
             //sim.createStageWalls();
             winBox = sim.addBox({x:5,y:600/30, width:300/30, height:10/30, density:0});
             looseBox = sim.addBox({x:15,y:600/30, width:300/30, height:10/30, density:0});
                      
             
             // make obstacles
             for (var i:int = 0; i<(stage.stageWidth/50); i++){
             //End
                 sim.addCircle({x:1 + i * 1.5, y:16, radius:0.1, density:0});
                 sim.addCircle({x:2 + i * 1.5, y:15, radius:0.1, density:0});
              
             //Mid End
             
                  sim.addCircle({x:0 + i * 2, y:14, radius:0.1, density:0});
                  sim.addCircle({x:0 + i * 2, y:13, radius:0.1, density:0});
                  sim.addCircle({x:0 + i * 2, y:12, radius:0.1, density:0});
                  sim.addCircle({x:0 + i * 2, y:11, radius:0.1, density:0});
                  sim.addCircle({x:0 + i * 2, y:10, radius:0.1, density:0});
                 
                 
             //Middle Start
                 sim.addCircle({x:0 + i * 1.5, y:09, radius:0.1, density:0});
                 sim.addCircle({x:1 + i * 1.5, y:08, radius:0.1, density:0});
                 sim.addCircle({x:0 + i * 1.5, y:07, radius:0.1, density:0});
                 sim.addCircle({x:1 + i * 1.5, y:06, radius:0.1, density:0});
                
             }
   
             sim.start();
             
          
             stage.addEventListener(MouseEvent.CLICK, _clicked);
          }
          
          /**
           * ..
           * @param e MouseEvent.CLICK
           */
          
          private function _clicked(e:MouseEvent)
          {
             gameBall = sim.addCircle({x:(mouseX/30), y:(1), radius:0.25, density:5});
              
             
          }
       }
       
    }
I took a quick look at the QuickBox2D documentation (I've never used it before).

1.) You would need to add a contact listener: http://actionsnippet.com/qb2d/docs/html ... ctListener
The way that apparently works (for what you want) is you call addContactListener() and assign it to a QuickContacts variable. Then, add an event listener (with a QuickContacts.ADD event) with a function that would call the "isCurrentContact (a:QuickObject, b:QuickObject)" function. The "isCurrentContact" function returns true if both QuickObjects are the objects in the current contact/collision (i.e. the winBox and the gameBall).

2.) You can check if the ball is sleeping. It's either "gameBall.body.IsAwake()" or "gameBall.body.IsSleeping()", depending on what version of Box2D the QuickBox2D engine is using (so, if "IsAwake()" doesn't work (either syntax or runtime error), use "IsSleeping()"). If it's sleeping (not awake), then call "gameBall.destroy()", then set "gameBall" as null.

3.) You can check if "gameBall" is null ("if (!gameBall) /*line to create a new ball*/"), assuming if you follow through with the answer of the previous question (setting "gameBall" as null when it's destroyed).
Thanks for the response:

I have tried with various ways, the only one that it is not giving me errors is:

Code:
         if (gameBall.body.IsSleeping())
         {
               gameBall.destroy();
         }


But it's not doing anything.

Maybe because it's part of this function:

Code:
      private function _clicked(e:MouseEvent)
      {
         gameBall = sim.addCircle({x:(mouseX/30), y:(1), radius:0.25, density:5});
         
         if (gameBall.body.IsSleeping())
         {
               gameBall.destroy();
         }
          
         
      }


What do you think?
Yeah, you don't want it in the click listener function because that function is only ever called when you click something, and even if you want it to only check during clicks, nothing would happen because you are checking if the gameBall is sleeping AFTER you have already made another instance of the gameBall (at the very least, you would want it before the addCircle bit).

If you want it to be destroyed whenever it stops moving, you'll need to add an ENTER_FRAME event listener so that it checks every frame, and put the sleeping body check in that listener.

If you only want it to check every time you click (i.e. potentially want to add another ball), you'll need to move the sleeping body check before the ball creation (and make sure that it's not accessing an empty variable). Example:
Code:
      private function _clicked(e:MouseEvent)
      {
         //"if (gameBall)" part is to make sure you're not trying to
         //access an null reference (otherwise, you will have a runtime error)
         if (gameBall && gameBall.body.IsSleeping())
         {
               gameBall.destroy();
               gameBall = null;
         }
         
         //"if (!gameBall)" means "if gameBall is null" (i.e. destroyed because of the sleeping body check)
         //This "if" is needed so you don't make multiple gameBalls.
         if (!gameBall)
         {
               gameBall = sim.addCircle({x:(mouseX/30), y:(1), radius:0.25, density:5});
         }
Thank you!

With your help I was able to make it work 😁

Original thread: https://box2d.org/forum/viewtopic.php?t=7978