Matthias Elephant Posted February 28, 2016 Share Posted February 28, 2016 Hi guys, i have a problem with nested sprites with enabled body. I need to add a child with a body to a parent with a body. In the example you can see that the body of the children isn't at the same position as the sprite himself. In addition if i rotate the parent, only the childrens sprite, but not the body will rotate. Just copy the following code into the example: http://phaser.io/examples/v2/p2-physics/basic-movement#gv var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update }); function preload() { game.load.image('atari', 'assets/sprites/atari130xe.png'); } var sprite; var children; function create() { game.physics.startSystem(Phaser.Physics.P2JS); children = game.add.sprite(100, 100, 'atari'); children.scale.set(0.5); game.physics.p2.enable(children, true); children.body.setZeroDamping(); children.body.moveRight(10); sprite = game.add.sprite(200, 200, 'atari'); game.physics.p2.enable(sprite, true); sprite.body.setZeroDamping(); sprite.body.moveRight(10); //sprite.body.rotateLeft(10); sprite.addChild(children); } function update() { } You can help me? Thanks Link to comment Share on other sites More sharing options...
Matthias Elephant Posted March 1, 2016 Author Share Posted March 1, 2016 Push. No ideas? Any help? I also tried some alternatives without physics, but without success. Link to comment Share on other sites More sharing options...
Grubs Posted April 10, 2016 Share Posted April 10, 2016 I've been trying this too and same problem. var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update }); function preload() { game.load.image('atari', 'assets/sprites/atari130xe.png'); game.load.image('sky', 'assets/skies/sunset.png'); } var sprite; var cursors; function create() { game.add.image(0, 0, 'sky'); // Enable p2 physics game.physics.startSystem(Phaser.Physics.P2JS); // Make things a bit more bouncey game.physics.p2.defaultRestitution = 0.8; // Add a sprite sprite = game.add.sprite(200, 200, 'atari'); child = game.add.sprite(200, 200, 'atari'); sprite.addChild(child); // Enable if for physics. This creates a default rectangular body. game.physics.p2.enable(sprite, true, true); // Modify a few body properties // child.body.kinematic = true; sprite.body.setZeroDamping(); sprite.body.fixedRotation = true; text = game.add.text(20, 20, 'move with arrow keys', { fill: '#ffffff' }); cursors = game.input.keyboard.createCursorKeys(); } function update() { sprite.body.setZeroVelocity(); if (cursors.left.isDown) { sprite.body.moveLeft(400); } else if (cursors.right.isDown) { sprite.body.moveRight(400); } if (cursors.up.isDown) { sprite.body.moveUp(400); } else if (cursors.down.isDown) { sprite.body.moveDown(400); } } How do you make the body follow the child? Link to comment Share on other sites More sharing options...
Phaser Posted July 4, 2016 Share Posted July 4, 2016 When I add a child it follows perfectly but as soon as I give that child a p2 body bad things happen. Link to comment Share on other sites More sharing options...
flow Posted February 11, 2018 Share Posted February 11, 2018 Has there been any solution for this? It seems the child collision body is placed in the world due to the its relative coordinates to the parent. So when I add a child at 0,0 relative to the parent it shows up at worldX=0, worldY=0 when P2 physics is active. Link to comment Share on other sites More sharing options...
Recommended Posts