简体   繁体   English

演员位置

[英]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) . 当我调用setActorPosition方法时,在特定位置的精灵绘制很好,但actor位置仍为(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. 我希望使用此方法将actor和sprite绘制在同一位置,因为我想在另一个类中控制该位置。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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