【问题标题】:LibGDX Actor doesn't perform ActionLibGDX Actor 不执行动作
【发布时间】:2017-02-21 13:56:04
【问题描述】:

我无法让我的 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();
}

【问题讨论】:

  • 你的卡片添加到舞台了吗?
  • 我做了,它被画在舞台上,它接收触摸输入,我可以根据触摸输入改变它的位置,但我不能让它用一个动作移动(我希望它平滑移动)
  • 您在哪里更新您的 Cards 位置变量?您调用 act,它调用 super.act() 方法,并在您覆盖的 draw 方法中使用 batch.draw(cardFace, position.x, position.y, cardFace.getWidth() ... 但 position.x 和 .y 是何时更新?动作作用于 Actor 的位置,(尝试将 getX() 和 getY() 方法放在覆盖的绘图中,看看是否有区别)
  • 我现在明白了。把 getX 和 getY 工作。但现在我有另一个问题。我将所有卡片放在一个数组中(卡片 [] 卡片组 = 新卡片 [52])。它们被初始化为:deck[0] = new Card("two", "spades") 等 ....当我尝试 deck[0].addAction(moveAction);它只是不起作用
  • 您必须为每个动作创建一个新动作。您应该使用 Action.moveToAction 而不是新的 MoveToAction 创建您的操作。这样,您使用的操作就会被合并。

标签: java libgdx action actor


【解决方案1】:

您的Card 正在使用自己的位置Vector2 position。当您将MoveAction 添加到您的Card 时,该操作会访问并改变Actor 类中的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);
}

【讨论】:

    猜你喜欢
    • 2016-12-25
    • 2015-12-08
    • 2014-11-10
    • 1970-01-01
    • 2018-06-04
    • 2014-01-21
    • 2017-07-24
    • 2018-01-28
    • 2017-01-22
    相关资源
    最近更新 更多