I want it to react when I pull the light bulb
What’s the problem?
I want an alarm to come when I pull the light bulb
but got a error
Uncaught TypeError: Cannot read properties of undefined (reading ‘position’)
group = Body.nextGroup(true);
var ropeB = Composites.stack(width/2, 100, 10, 1, 10, 10, function(x, y) {
return Bodies.rectangle(x , y, 20, 4, {
collisionFilter: {
group: group
},
render : ropeRenderStyle
});
});
const lamp = Matter.Bodies.circle(width/2, 210, 30, {
density: 0.0005,
frictionAir: 0.06,
restitution: 0.3,
friction: 0.01,
render: {
sprite: {
texture: './img/lamp.png',
xScale: 0.12,
yScale: 0.14
}
}
})
Composite.add(ropeB, lamp);
Composites.chain(ropeB, 0.5, 0, -0.5, 0, {
stiffness: 0.8,
length: 1,
render: ropeRenderStyle
});
Composite.add(
ropeB,
Constraint.create({
bodyB: ropeB.bodies[0],
pointB: { x: 0, y: 0 },
pointA: { x: ropeB.bodies[0].position.x, y: ropeB.bodies[0].position.y },
stiffness: 0.5,
render : ropeRenderStyle
})
);
Events.on(engine, 'collisionStart', () => {
const { bodies } = engine.world;
const lamp = bodies.find(body => body.label === 'Circle Body');
if (lamp.position.y < height) {
alert("click");
}
});