AS3 > Mouse Event not happening?

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 EDIT: Marty Wallace corrected my code, but still the mouse event won't work.

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

        }
    }

}

Answers


EDIT by Gabriel Meono:

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

Either do this (recommended):

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/30), y:(mouseY/30), density:5});
    sim.start();  
}

Or this (not so recommended, but closer to your code):

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

    addEventListener(
        MouseEvent.CLICK,
        function(e:MouseEvent):void
        {
            sim.addCircle({x:(mouseX/30), y:(mouseY/30), radius:0.25, density:5});
            sim.start();
        }
    );
}

Try declaring the clicked function outside the MouseTest function.


Need Your Help

Can still access a control's properties after it is disposed

.net vb.net winforms using

I am using the following template below to show a modal form

PHP Server error on include() and required_once() function

php url webserver

I am using the php page includes in my site, it's perfectly working without error in localhost, but at the time of running this with a live web server it shows the error.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.