Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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:

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

Question:

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

Thanks, here is the full 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});


        }
    }

}
share|improve this question
add comment

1 Answer

Got help from atomparker at stackexchange, The class needed is QuickContacts inside BOX2D library.

Here's the code for future reference:

   var simContacts:QuickContacts;

public function LuckyHit()
{
    ...
    simContacts = sim.addContactListener();
    simContacts.addEventListener(QuickContacts.ADD, contactListener);
}

...

private function contactListener(event:Event)
{
    if (simContacts.isCurrentContact(winBox, gameBall))
    {
        // They won.
    }
}

The relevant class in the QuickBox2D documentation is QuickContacts.

share|improve this answer
    
Feel free to accept your own answer, btw :) –  ZorbaTHut Jun 13 '12 at 2:23
    
maybe complex physics, you will need APE cove.org/ape and of course there are a bunch other libraries as well. –  zinking Jun 13 '12 at 6:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.