AS3: Mouse Click event not working

Background:

Trying to make a simple "drop the ball" game.

The code is located inside the first frame of the timeline. Nothing more is on the stage.

Issue:

Using QuickBox2D I made a simple If statement that drops and object acording the Mouse-x position:

if (MouseEvent.CLICK) {
sim.addCircle({x:mouseX, y:1, radius:0.25, density:5});

I imported the MouseEvent library:

import flash.events.MouseEvent;

Nothing happens if I click, no output errors either.

See it in action: http://gabrielmeono.com/download/Lucky_Hit_Alpha.swf http://gabrielmeono.com/download/Lucky_Hit_Alpha.fla

Full Code:

    [SWF(width = 350, height = 600, frameRate = 60)]  
import com.actionsnippet.qbox.*;  
import flash.events.MouseEvent;

var sim:QuickBox2D = new QuickBox2D(this);  
sim.createStageWalls();
//var ball:sim.addCircle({x:mouseX, y:1, radius:0.25, density:5}); //

// make a heavy circle 

sim.addCircle({x:3, y:1, radius:0.25, density:5});
sim.addCircle({x:2, y:1, radius:0.25, density:5});
sim.addCircle({x:4, y:1, radius:0.25, density:5});
sim.addCircle({x:5, y:1, radius:0.25, density:5});
sim.addCircle({x:6, y:1, radius:0.25, density:5});
// create a few platforms  

sim.addBox({x:3, y:2, width:4, height:0.2, density:0, angle:0.1});
// make 26 dominoes  
for (var i:int = 0; i<7; 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});
} 

if (MouseEvent.CLICK) {
    sim.addCircle({x:mouseX, y:1, radius:0.25, density:5});

sim.start();  
/*sim.mouseDrag();*/  




    }

Answers


That is because you are listening for the mouse click wrong.

Instead of

if (MouseEvent.CLICK) {
    sim.addCircle({x:mouseX, y:1, radius:0.25, density:5});
    sim.start();  
    /*sim.mouseDrag();*/  
}

You need to do

addEventListener(MouseEvent.CLICK, clicked);

function clicked(e:MouseEvent) {

    sim.addCircle({x:mouseX, y:1, radius:0.25, density:5});
    sim.start();  
    /*sim.mouseDrag();*/ 
}

When you don't need to listen for any more clicks, do

removeEventListener(MouseEvent.CLICK, clicked);

To remove the event listener (for freeing up memory)


You should create mouse event listener:

addEventListener(MouseEvent.CLICK, add_circle_handler);

function add_circle_handler(event:MouseEvent):void {
      sim.addCircle({x:mouseX, y:1, radius:0.25, density:5});
} 

Don't forget to remove the event listener when you no longer need to add circles. It's best practice when working with listeners (it's about memory management):

removeEventListener(MouseEvent.CLICK, add_circle_handler);

You need to add an event listener for mouse events:

addEventListener(MouseEvent.CLICK, mouseClickHandler);

protected function mouseClickHandler(event:MouseEvent):void
{
    sim.addCircle({x:mouseX, y:1, radius:0.25, density:5});
}

Need Your Help

Could I use an iframe for implement web payments procedure

security iframe

Hello I would like to implement a payments on my web site.

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.