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.

Background: Trying to make a ball appear in the same location the mouse is positioned. Using Box2D Library. Working on a class file (.as)

Issue: The mouse event does not work (I click but nothing happens) . No output errors whatsoever. :(

Mouse Event:

   private function _clicked(e:MouseEvent)
            {
                sim.addCircle({x:mouseX, y:mouseY, radius:0.25, density:5});
                sim.start();  

Full Code:

package  {

    import flash.display.MovieClip;
    import com.actionsnippet.qbox.*;  
    import flash.events.MouseEvent;

    [SWF(width = 350, height = 600, frameRate = 60)]  

    public class MouseTest extends MovieClip {

        public var sim:QuickBox2D;

        /**
         * Constructor
         */
        public function MouseTest()
        {
            sim = new QuickBox2D(this);  
            sim.createStageWalls();

            addEventListener(MouseEvent.CLICK, _clicked);
        }

        /**
         * ..
         * @param e MouseEvent.CLICK
         */
        private function _clicked(e:MouseEvent)
        {
            sim.addCircle({x:mouseX, y:mouseY, radius:0.25, density:5});
            sim.start();  

        }
    }

}

Download Project (BOX2D Included)

http://gabrielmeono.com/download/BOX2D-Mouse_Test.zip

share|improve this question
    
What version of Flash did you use to create the fla? I couldn't open it with CS4. Anyway, I think you just need to use stage.addEventListener(MouseEvent.CLICK, _clicked) in your constructor instead of addEventListener(MouseEvent.CLICK, _clicked). Probably should also call super() on the first line of your constructor as well. –  NoobsArePeople2 Dec 2 '11 at 2:57
add comment

2 Answers 2

When you use addEventListener in the class it only dispatches the mouse clicks that fall on the actual visual data inside that DisplayObject. If you use stage.addEventListener you can use the whole stage.

share|improve this answer
add comment

QuickBOX2D uses meters instead of pixels. To work with pixels with this library, one must divide each number by 30. I simply added a "/30" to the mouseX and it worked! I updated my code for future reference.

package  {

    import flash.display.MovieClip;
    import com.actionsnippet.qbox.*;  
    import flash.events.MouseEvent;

    [SWF(width = 350, height = 600, frameRate = 60)]  

    public class MouseTest extends MovieClip {

        public var sim:QuickBox2D;

        /**
         * Constructor
         */
        public function MouseTest()
        {
            sim = new QuickBox2D(this);  
            sim.createStageWalls();

            stage.addEventListener(MouseEvent.CLICK, _clicked);
        }

        /**
         * ..
         * @param e MouseEvent.CLICK
         */
        private function _clicked(e:MouseEvent)
        {
            sim.addCircle({x:(mouseX/30), y:(mouseY/30), radius:0.25, density:5});
            sim.start();  

        }
    }

}
share|improve this answer
    
You should mark this as the answer then. –  NoobsArePeople2 Dec 2 '11 at 18:29
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.