###前提・実現したいこと
matter.jsで特定の要素を削除したいのですが、うまくいきません。個別にWorldに追加したground1,2などは
現状のコードのようにドラッグを開始すると
削除できるのですが、(床が抜ける)stackの方の特定の要素を
削除する場合はどうしたらよいでしょうか?
とりあえず、ドラッグした物体を(this.body)を
削除したいです。
###該当のソースコード
<!doctype html> <html> <head> <meta charset="utf-8"> <title>無題ドキュメント</title> </head> <body> <main id="stage"></main> <script src="../matter.js" type="text/javascript"></script> <script> (function() { var Engine = Matter.Engine, Events = Matter.Events; Common = Matter.Common; World = Matter.World, Mouse = Matter.Mouse, MouseConstraint = Matter.MouseConstraint, Bodies = Matter.Bodies Composites = Matter.Composites; // Matter.js エンジン作成 var engine = Engine.create(document.getElementById("stage")); var world = engine.world; var stack = Composites.stack(20, 20, 15, 4, 0, 0, function(x, y) { var sides = Math.round(Common.random(1, 8)); // triangles can be a little unstable, so avoid until fixed sides = (sides === 3) ? 4 : sides; // round the edges of some bodies var chamfer = null; if (sides > 2 && Common.random() > 0.7) { chamfer = { radius: 10 }; } switch (Math.round(Common.random(0, 1))) { case 0: if (Common.random() < 0.8) { return Bodies.rectangle(x, y, Common.random(25, 50), Common.random(25, 50), { chamfer: chamfer }); } else { return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(25, 30), { chamfer: chamfer }); } break; case 1: return Bodies.polygon(x, y, sides, Common.random(25, 50), { chamfer: chamfer }); } }); World.add(world, stack); // マウスドラッグ追加 var mousedrag = MouseConstraint.create(engine); World.add(engine.world, mousedrag); Events.on(mousedrag, "startdrag", function(e) { World.remove(engine.world, ground1);//削除 }); // 地面固定 var ground1 = Bodies.rectangle(400, 600, 800, 10, { isStatic: true }); var ground2 = Bodies.rectangle(0, 300, 10, 600, { isStatic: true }); var ground3 = Bodies.rectangle(800, 300, 10, 600, { isStatic: true }); World.add(engine.world, [ground1, ground2, ground3]); Engine.run(engine); })(); </script> </body> </html>
###試したこと
World.remove(engine.world, this.body);//削除
や
World.remove(engine.world, stack.bodies[1]);//削除
など、stack内の物体の特定はできてるので、
指定していろいろ入れてはいるのですが・・・
ご教授のほどよろしくお願いいたします。
あなたの回答
tips
プレビュー