0
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – NoobsArePeople2 Dec 2 '11 at 2:57
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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();  

        }
    }

}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You should mark this as the answer then. \$\endgroup\$ – NoobsArePeople2 Dec 2 '11 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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