问题
Hello if I have some code like the following for the matter.js library :
// create two boxes and a ground
var boxA = Bodies.rectangle(400, 200, 80, 80);
var boxB = Bodies.rectangle(450, 50, 80, 80);
var ground = Bodies.rectangle(400, 610, 810, 60, { isStatic: true });
// add all of the bodies to the world
World.add(engine.world, [boxA, boxB, ground]);
Events.on(engine, 'tick', function(event) {
if(mouseConstraint.mouse.button == 0){
alert("what is clicked?");
}
});
Is there a way I can tell if boxA or boxB has been clicked with the mouse in the event handler ?
回答1:
mouseConstraint.body
contains the body that was clicked.
See https://www.youtube.com/watch?v=W-ou_sVlTWk around 9:40
来源:https://stackoverflow.com/questions/28324303/matter-js-mouse-click-on-body