admin管理员组文章数量:1379419
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?
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 Take a look at how it's done in Matter.MouseConstraint.update – liabru Commented Jun 6, 2015 at 12:06
- @liabru how would it be done in the OP's case? – Jesse Hattabaugh Commented Mar 7, 2017 at 4:53
2 Answers
Reset to default 2To elaborate on this answer, here's a runnable example of using mouseConstraint.body
in your event handler to determine which body is being clicked:
const engine = Matter.Engine.create();
const runner = Matter.Runner.create();
const render = Matter.Render.create({
element: document.body,
engine: engine,
});
const bodies = [
Matter.Bodies.rectangle(
400, 310, 810, 60, {isStatic: true, angle: 0.0}
),
...[...Array(100)].map(() =>
Matter.Bodies.rectangle(
Math.random() * 400, // x
Math.random() * 100, // y
Math.random() * 50 + 10, // w
Math.random() * 50 + 10, // h
{angle: Math.random() * (Math.PI * 2)},
)
),
];
const mouseConstraint = Matter.MouseConstraint.create(
engine, {element: document.body}
);
Matter.Events.on(runner, "tick", event => {
if (mouseConstraint.body) {
Matter.Composite.remove(engine.world, mouseConstraint.body);
}
});
// also possible, testing the condition on mousedown only:
//Matter.Events.on(mouseConstraint, "mousedown", () => {
// if (mouseConstraint.body) {
// Matter.Composite.remove(engine.world, mouseConstraint.body);
// }
//});
Matter.Composite.add(engine.world, [...bodies, mouseConstraint]);
Matter.Runner.run(runner, engine);
Matter.Render.run(render);
<script src="https://cdnjs.cloudflare./ajax/libs/matter-js/0.20.0/matter.js"></script>
You can also use Matter.Query.point
and pass in the mouse X and Y position on click to obtain an array of bodies at that point.
mouseConstraint.body
contains the body that was clicked.
本文标签: javascriptmatterjs mouse click on bodyStack Overflow
版权声明:本文标题:javascript - matter.js mouse click on body - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744451188a2606757.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论