I have this class:
public class MyActor extends Actor{
TextureRegion region;
public MyActor(TextureRegion region)
{
this.region = region;
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
batch.draw(region, 0, 0);
}
}
And I make use of it here like this:
public class MyGame implements ApplicationListener {
Stage stage;
MyActor myActor;
@Override
public void create() {
stage = new Stage(0, 0, false);
myActor = new MyActor(new TextureRegion(new Texture(
Gdx.files.internal("texture.png"))));
myActor.setPosition(200, 50);
stage.addActor(myActor);
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
}
In the MyActor
class I set the position of the actor to (0, 0)
. I just used this as some kind of default as I want to be able to change its position before it's actually drawn on the stage. The reason is I have a few MyActor
objects and I want to draw them all on the stage but at different positions.
So I did myActor.setPosition(200, 50);
, but it didn't work.
My question: How can I draw myActor
on the stage with position other than what is set in draw(Batch, float)
?
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
batch.draw(region, 0, 0);
}
Remove the super.draw
call here. Right now it shouldn't do anything, if it would do anything, it will probably cause problems.
Furthermore, you complained, that your actor position is not used. That's because you didn't use it.
Try something like this instead:
public void draw(Batch batch, float parentAlpha) {
batch.draw(region, getX(), getY());
}