http://brm.io/matter-js/docs/
https://github.com/liabru/matter-js
【点击事件 https://www.coder.work/article/7638867】
里面的点击对象必须用Mouse创建
var mouse = Matter.Mouse.create(render.canvas),
mouseConstraint = Matter.MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
stiffness: 0.2,
render: {
visible: false
}
}
});
Matter.Events.on(mouseConstraint, 'mousedown', function(event) {
var mousePosition = event.mouse.position;
console.log('mousedown at ' + mousePosition.x + ' ' + mousePosition.y);
});
点击事件目前想的是点击的时候得到位置后 循环已知元素的位置 匹配
=====================已解决==================================
const rect2 = Bodies.rectangle(110,130,50,80);
rect2.label = "11111"
console.log("rect2",rect2)
engine.world.gravity.y = 0.01
World.add(engine.world,[rect,rect2]);
Engine.run(engine);
Render.run(render);
var mouse = Matter.Mouse.create(render.canvas),
mouseConstraint = Matter.MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
stiffness: 0.2,
render: {
visible: false
},
},
});
World.add(engine.world, mouseConstraint);
Matter.Events.on(mouseConstraint, 'mousedown', function(event) {
var mousePosition = event.mouse.position;
// console.log(mousePosition.x,"对象11==",event)
// console.log('mousedown at ' + mousePosition.x + ' ' + mousePosition.y);
var bodiesUnder = Matter.Query.point([rect2], { x: mousePosition.x, y: mousePosition.y });
//alert("click!\n" + mouseX2 + " " + mouseY2 +"\n");
if (bodiesUnder.length > 0) {
var bodyToClick = bodiesUnder[0];
console.log("获取点击对象")
}
});