Sign in to follow this  

Sprites not follow positions in update (Physics P2)

Recommended Posts

I want to follow a sprite with another, but when the first sprite move quickly, the second is shifted. It is visible on the screen and in the logs (x/y position in each iteration of the update function).
When the first sprite slow down, the shift is reduced. When it stop, the sprite are at the same position (it’s normal).
How to do the positions of the two elements remain the same every time?
I have make a live example here :,output
Lose focus to do “pause” and watch positions and images.
Sorry for my bad english.
My example code :

var red_ball=null, green_ball=null;var MainState={    preload: function(){    this.load.image('red_ball', '');        this.load.image('green_ball', '');  },  create:function() {    this.time.advancedTiming = true;        this.physics.startSystem(Phaser.Physics.P2JS);    this.physics.p2.setImpactEvents(true);    this.physics.p2.defaultRestitution = 0.8;        red_ball = this.add.sprite(100, 100, 'red_ball');    this.physics.p2.enable(red_ball);    red_ball.enableBody = true;    red_ball.physicsBodyType = Phaser.Physics.P2JS;    red_ball.scale.set(2);    red_ball.body.setCircle(8);    red_ball.body.collideWorldBounds = true;    red_ball.body.fixedRotation = true;    red_ball.anchor.setTo(0.5, 0.5);    red_ball.body.angle = 90;        red_ball.body.moveForward(3000);        green_ball = this.add.sprite(100, 100, 'green_ball');    green_ball.scale.set(2);    green_ball.anchor.setTo(0.5, 0.5);      },  update: function(){    green_ball.x = red_ball.x;    green_ball.y = red_ball.y;      },  render: function() { || '--', 2, 14, "#00ff00");'Green ball: x: ' + green_ball.x + ' - y: ' + green_ball.y, 2, 30, "#00ff00");'Red ball:   x: ' + red_ball.x + ' - y: ' + red_ball.y, 2, 46, "#00ff00");      }};var game = new Phaser.Game(500, 500, Phaser.CANVAS, 'phaser-example', MainState);

Thanks !
Edit: i have update my jsbin link example + a new screenshot to see the shifted:

Edited by olorin

Share this post

Link to post
Share on other sites

use sprite.addChild(secondsprite);


Thanks for your response ! 
But it is possible to remove child after? I want to drag my second sprite and in this case it stops to follow the first sprite (and when I stop to drag, the sprite become again a child). Drag doesn't work with relative positions.
AddChild() is not in the Phaser.Sprite documentation…

Share this post

Link to post
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.