Reputation: 175
This code is showing strange behavior. The BaseActor class just call in order: initPhysics, initGraphics and onCreate (only once) and then act (for each frame). The expected behaviors is that in onCreate method I hold the fixX
value (that actually is -6) to use the this value to set the X position of object each frame, for make the X axis of object static. I put a debug point in onCreate and act and he is called appropriately.
When
fixX = getBody().getPosition().x;
he get value 6, as expected, but in act the fixX
value back to 0.
When I change the fixX
variable to static, the things works like expected. I don't realize how. I put a System out on the constructor of BirdActor to be sure the object is create only once.
The methods setTransform of body is an Jni interface for C++ code of Libgdx with Box2d Engine.
public class BirdActor extends BaseActor {
private float fixX = 0;
public BirdActor() {
System.out.println("Created");
}
@Override
protected Body initPhysics() {
return Assets.scene.getNamed(Body.class, "bird").get(0);
}
@Override
protected void onCreate() {
fixX = getBody().getPosition().x;
}
@Override
public void act() {
getBody().setTransform(fixX, getBody().getPosition().y, 0);
super.act();
}
@Override
protected Sprite initGraphics() {
Sprite sprite = new Sprite(Assets.textureBird);
return sprite;
}
}
Upvotes: 1
Views: 203
Reputation: 1037
The problem could be because you call virtual method in constructor.
So the call list is:
1. BaseActor()
2. onCreate() of BirdActor
3. BirdActor() whitch inits the fixX = 0.
So initializing fixX
is called after onCreate()
method. Try remove the fixX
initialization (= 0
).
Upvotes: 1