Reputation: 1665
I have an entity moving from right to left, and I want to reuse it, so I want the entity to appear on the right of the screen when it leaves the screen.
physicsWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, false)
{
@Override
public void onUpdate(float pSecondsElapsed)
{
super.onUpdate(pSecondsElapsed);
if (GameScene.isRunning)
body.setLinearVelocity(new Vector2(-100, body.getLinearVelocity().y));
Log.d("TAG","MY X IS: " + getX());
if (getX() <= -100) {
Log.d("TAG","I SHOULD RESPAWN");
setPosition(800, 100);
}
}
});
The log:
02-12 20:14:36.059: D/TAG(9293): MY X IS: 179.99994
02-12 20:14:36.069: D/TAG(9293): MY X IS: 126.6666
02-12 20:14:36.089: D/TAG(9293): MY X IS: 73.33327
02-12 20:14:36.109: D/TAG(9293): MY X IS: 19.999931
02-12 20:14:36.119: D/TAG(9293): MY X IS: -33.333405
02-12 20:14:36.139: D/TAG(9293): MY X IS: -86.66674
02-12 20:14:36.159: D/TAG(9293): MY X IS: -140.00008
02-12 20:14:36.159: D/TAG(9293): I SHOULD RESPAWN
02-12 20:14:36.169: D/TAG(9293): MY X IS: -193.3334
02-12 20:14:36.169: D/TAG(9293): I SHOULD RESPAWN
02-12 20:14:36.189: D/TAG(9293): MY X IS: -246.66675
02-12 20:14:36.189: D/TAG(9293): I SHOULD RESPAWN
02-12 20:14:36.209: D/TAG(9293): MY X IS: -300.0001
02-12 20:14:36.209: D/TAG(9293): I SHOULD RESPAWN
So it is ignoring me :(
Any help with this?
The entity is Kinetic, just in case it helps
Upvotes: 0
Views: 75
Reputation: 3864
You can't use methods from sprite when you use a body. Body controls the position of the sprite. Try applying the new position to the body.
Protip: if you face a similar problem in the future (and you are not using physics) you can use MoveModifier and LoopModifier with your sprite.
Upvotes: 1
Reputation: 1665
Fixed. SetPosition sets the position of the entity, however the body is still controlled by the physics, so you have to use setTransform
@Override
public void onUpdate(float pSecondsElapsed)
{
super.onUpdate(pSecondsElapsed);
if (GameScene.isRunning)
body.setLinearVelocity(new Vector2(-8, body.getLinearVelocity().y));
Log.d("TAG","MY X IS: " + getX());
if (getX() <= -100) {
Log.d("TAG","I SHOULD RESPAWN");
final float widthD2 = getWidth() / 2;
final float heightD2 = getHeight() / 2;
final float angle = body.getAngle(); // keeps the body angle
final Vector2 v2 = Vector2Pool.obtain((500 + widthD2) / PIXEL_TO_METER_RATIO_DEFAULT, (50 + heightD2) / PIXEL_TO_METER_RATIO_DEFAULT);
body.setTransform(v2, angle);
Vector2Pool.recycle(v2);
setPosition(500 + widthD2,50+ heightD2);
}
}
Upvotes: 0