LibGDX 演员没有执行动作

LibGDX Actor doesn't perform Action

我无法让我的 Card Actor 执行动作(我可以在图像和纹理上添加和执行动作)。这是我的 Card.class 代码:

public class Card extends Actor {
final float CARD_WIDTH = 500 * cardScale;
final float CARD_HEIGHT = 726 * cardScale;
String face, suit;
public Texture cardFace = new Texture("CardTextures/" + face + suit + ".png");

Vector2 position = new Vector2(0, 0);

public Card(String face, String suit, Vector2 position) {
    this.face = face;
    this.suit = suit;
    this.position = position;
    setBounds(position.x, position.y, CARD_WIDTH, CARD_HEIGHT);
}

public Card(String face, String suit) {
    this.face = face;
    this.suit = suit;
    this.position = position;
    setBounds(position.x, position.y, CARD_WIDTH, CARD_HEIGHT);
}

@Override
public void act(float delta) {
    super.act(delta);

}

@Override
public void draw(Batch batch, float alpha) {
    batch.draw(cardFace, position.x, position.y, cardFace.getWidth() * cardScale, cardFace.getHeight() * cardScale);
}

public String getFace() {
    return face;
}

public String getSuit() {
    return suit;
}

public void setCardPosition(Vector2 position) {
    this.position = position;

}

public void setCardFaceTexture() {
    cardFace = new Texture(("CardTextures/" + this.getFace() + this.getSuit() + ".png"));
}

}

每当我尝试使用 Card Actor 执行操作时,它都不起作用。即使我将操作放在 Create() 方法中,它也不起作用。我试过了:

moveAction = new MoveToAction();
    moveAction.setPosition(300f, 0f);
    moveAction.setDuration(10f);
    Card card = new Card("two", "spades");
    card.addAction(moveAction);

这是我的渲染方法:

 @Override
public void render() {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
    updateActorBounds();

    camera.update();

    batch.setProjectionMatrix(camera.combined);

    batch.begin();
    font.draw(batch, "FPS:" + Gdx.graphics.getFramesPerSecond(), TABLE_WIDTH / 2 - 65, TABLE_HEIGHT / 2 - 10);
    batch.end();
}

您的 Card 正在使用自己的位置 Vector2 position。当您将 MoveAction 添加到 Card 时,操作会访问并改变 Actor class 中的 xy 变量 - 而不是您的.这意味着,如果您希望 Card 显示在操作指示的位置,而不是在 position.x, position.y 处绘制,您需要在 getX(), getY().

处绘制

您的构造函数将如下所示:

public Card(String face, String suit, Vector2 position) {
    this.face = face;
    this.suit = suit;
    setBounds(position.x, position.y, CARD_WIDTH, CARD_HEIGHT);
}

您的绘制方法将如下所示:

@Override
public void draw(Batch batch, float alpha) {
    batch.draw(cardFace, getX(), getY(), cardFace.getWidth() * cardScale, cardFace.getHeight() * cardScale);
}