matter.js mouse click on body

人盡茶涼 提交于 2019-12-23 07:49:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!