Legomite Posted August 24, 2015 Share Posted August 24, 2015 Using my modified version of pandas.net/demos/p2.html , I added a way to manually add objects, and delete objects but when I delete it, the body still exists but is invisible. Why???Demo here:http://legomite.hostoi.com/test/dev.htmlAdd objects until the objects start stacking and press the sprite in top-right to toggle destruction, destroy the objects below the tower, and the objects above are still affected by the invisible body.Code:game.module( 'game.sprites').require( 'engine.core', 'plugins.p2').body(function() {game.addAsset('rectangle.png');game.addAsset('circle.png');game.addAsset('panda.png');game.addAsset('panda1.png');game.createClass('PhysicsObject', { size: 70, init: function(x, y) { // Add body and shape var shape = Math.random() > 0.5 ? new game.Circle(this.size / 2 / game.scene.world.ratio) : new game.Rectangle(this.size / game.scene.world.ratio, this.size / game.scene.world.ratio); this.body = new game.Body({ mass: 1, position: [ x / game.scene.world.ratio, y / game.scene.world.ratio ], angularVelocity: 1 }); this.body.addShape(shape); // Apply velocity var force = 1; var angle = Math.random() * Math.PI * 2; this.body.velocity[0] = Math.sin(angle) * force; this.body.velocity[1] = Math.cos(angle) * force; this.sprite = new game.Sprite(shape.radius ? 'circle.png' : 'rectangle.png'); this.sprite.anchor.set(0.5, 0.5); this.sprite.position.set(x, y); this.sprite.interactive = true; game.scene.addObject(this); game.scene.stage.addChild(this.sprite); game.scene.world.addBody(this.body); //game.scene.addTimer(5000, this.remove.bind(this)); this.sprite.click = this.sprite.tap = function() { if(mode == 1) { game.scene.addTimer(0, this.remove.bind(this)); } } }, remove: function() { game.scene.removeObject(this); game.scene.stage.removeChild(this.sprite); game.scene.world.removeBody(this.body); }, update: function() { this.sprite.position.x = this.body.position[0] * game.scene.world.ratio; this.sprite.position.y = this.body.position[1] * game.scene.world.ratio; this.sprite.rotation = this.body.angle; }});game.createScene('Main', { init: function() { mode = 2; var pandaToggle = new game.Sprite('panda.png'); pandaToggle.scale.set(0.25, 0.25); pandaToggle.position.set(0, 0); pandaToggle.interactive = true; pandaToggle.click = pandaToggle.tap = function() { pandaToggle.visible = false; pandaToggle.interactive = false; pandaToggle1.visible = true; pandaToggle1.interactive = true; if(mode == 2) { this.stage.addChild(pandaToggle1); } mode = 1; } var pandaToggle1 = new game.Sprite('panda1.png'); pandaToggle1.scale.set(0.25, 0.25); pandaToggle1.position.set(0, 0); pandaToggle1.interactive = false; pandaToggle1.visisble = false; pandaToggle1.click = pandaToggle1.tap = function() { pandaToggle1.visible = false; pandaToggle1.interactive = false; pandaToggle.visible = true; pandaToggle.interactive = true; mode = 0; } this.stage.addChild(pandaToggle); // Init physics world this.world = new game.World({ gravity: [0, 9] }); this.world.ratio = 100; // Add walls var wallShape, wallBody; wallShape = new game.Rectangle(50 / this.world.ratio, game.system.height * 2 / this.world.ratio); wallBody = new game.Body({ position: [0, game.system.height / 2 / this.world.ratio] }); wallBody.addShape(wallShape); this.world.addBody(wallBody); wallShape = new game.Rectangle(50 / this.world.ratio, game.system.height * 2 / this.world.ratio); wallBody = new game.Body({ position: [game.system.width / this.world.ratio, game.system.height / 2 / this.world.ratio] }); wallBody.addShape(wallShape); this.world.addBody(wallBody); wallShape = new game.Rectangle(game.system.width / this.world.ratio, 50 / this.world.ratio); wallBody = new game.Body({ position: [game.system.width / 2 / this.world.ratio, game.system.height / this.world.ratio] }); wallBody.addShape(wallShape); this.world.addBody(wallBody); /*this.addTimer(200, function() { var object = new game.PhysicsObject(game.system.width / 2, game.system.height / 2); game.scene.addObject(object); }, true);*/ }, mousedown: function(event) { if(mode == 2 || mode == 0) { var x = event.global.x; var y = event.global.y; var object = new game.PhysicsObject(x, y); game.scene.addObject(object); } }});}); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.