简体   繁体   中英

Position of Actor

I had a problem with position of actor

Here is actor class:

public class Test extends Actor {
  Sprite sprite;
  Vector2 actorPosition;

  public Test() {
    Texture t = new Texture("img.png");
    sprite = new Sprite(t);
    sprite.setSize(t.getWidth(), t.getHeight());
    actorPosition = new Vector2();

    // set the size and position of actor
    setSize(sprite.getWidth(), sprite.getHeight());
    setPosition(actorPosition.x, actorPosition.y);

    debug();
  }

  public void setActorPosition(Vector2 actorPosition) {
    this.actorPosition = actorPosition;
  }

  @Override
  public void draw(Batch batch, float parentAlpha) {
    batch.draw(sprite, actorPosition.x, actorPosition.y, getWidth(), getHeight());
  }
}

When I call the setActorPosition method, the sprite drawing in a specific position is fine but the actor position is still at (0, 0) .

I want the actor and sprite to be draw at the same position using this method because I want to control this position in another class.

How can I do this?

You can set the Actor's position using:

setX(float x);
setY(float y);

You could do it like this:

public void setActorPosition(Vector2 actorPosition) {
    this.actorPosition = actorPosition;
    setX(actorPosition.x);
    setY(actorPosition.y);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM