Libgdx ScaleToAction 未调整大小

Libgdx ScaleToAction not resizing

我正在尝试使用 MoveToAction、RotateToAction 和 ScaleToAction 使精灵移动、旋转和调整大小。前两个工作正常,但我对 ScaleToAction 有疑问。 我将动作添加到 Actor,就像我对两个有效的动作所做的那样。我认为问题可能出在@Override?当我 运行 代码时,精灵移动并旋转但没有缩放。

我尝试按照以下答案中的建议使用 sprite.setscale,但仍然没有成功。我在此处添加 class 中的代码:

public class Prizes extends Actor {
private LearnToRead game;
private TextureAtlas atlas;
private TextureRegion prizepic;
private Sprite sprite;
private RotateToAction rta;
private ScaleToAction sta;
private MoveToAction mta;

public Prizes(LearnToRead game) {
    this.game = game;
    atlas = new TextureAtlas("prizes.pack");
    prizepic = atlas.findRegion("haxhatt");

    sprite = new Sprite(prizepic);
    sprite.setPosition(450 - sprite.getWidth() / 2, 450 * Gdx.graphics.getHeight() / Gdx.graphics.getWidth() - sprite.getHeight() / 2);
    //setBounds(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());

    setTouchable(Touchable.enabled);

    rta = new RotateToAction();
    sta = new ScaleToAction();
    mta = new MoveToAction();

    rta.setRotation(180f);
    sta.setScale(2f);
    mta.setPosition(0, 0);
    mta.setDuration(5f);
    rta.setDuration(5f);
    sta.setDuration(5f);
    Prizes.this.addAction(rta);
    Prizes.this.addAction(sta);
    Prizes.this.addAction(mta);

}

@Override
public void draw(Batch batch, float parentAlpha) {
    sprite.draw(batch);
}

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

@Override
protected void positionChanged() {
    sprite.setPosition(getX(), getY());
}

@Override
protected void rotationChanged() {
    sprite.setRotation(getRotation());
}

@Override
protected void sizeChanged() {
    sprite.setScale(getScaleX(), getScaleY());
}

}

If 还尝试删除 sprite 并仅使用 TextureRegion,但没有成功。纹理已绘制,但未移动。我也 post 那个代码,但我承认我对这个代码很不确定:

public class Prizes extends Actor {
private LearnToRead game;
private TextureAtlas atlas;
private TextureRegion prizepic;
private RotateToAction rta;
private ScaleToAction sta;
private MoveToAction mta;

public Prizes(LearnToRead game) {
    this.game = game;
    atlas = new TextureAtlas("prizes.pack");
    prizepic = atlas.findRegion("haxhatt");

    Prizes.this.setBounds(450 - Prizes.this.getX(), Prizes.this.getY(), Prizes.this.getWidth(), Prizes.this.getHeight());

    setTouchable(Touchable.enabled);

    rta = new RotateToAction();
    sta = new ScaleToAction();
    mta = new MoveToAction();

    rta.setRotation(180f);
    sta.setScale(2f);
    mta.setPosition(0, 0);
    mta.setDuration(5f);
    rta.setDuration(5f);
    sta.setDuration(5f);
    Prizes.this.addAction(rta);
    Prizes.this.addAction(sta);
    Prizes.this.addAction(mta);

}

@Override
public void draw(Batch batch, float parentAlpha) {
    game.batch.begin();
    game.batch.draw(prizepic, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
    game.batch.end();
}

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

@Override
protected void positionChanged() {
    Prizes.this.setPosition(getX(), getY());
}

@Override
protected void rotationChanged() {
    Prizes.this.setRotation(getRotation());
}

@Override
protected void sizeChanged() {
    Prizes.this.setScale(getScaleX(), getScaleY());
}

}

也许有人知道我做错了什么?

使用 sprite.setScale 而不是 sprite.scale。不同之处在于您将其设置为特定值,而不是将当前比例乘以某个值。

但是将 Sprite 与 Actor 一起使用是多余的,因为两者 class 都存储位置、旋转、缩放和颜色。将 TextureRegion 与 Actor 结合使用更有意义。或者您可以使用图像 class,它已经为您处理了这个问题。

编辑:

我看到了问题的另一部分。您正在覆盖 sizeChanged,但您要使用 ScaleToAction 更改的是比例,而不是大小。 Actor 没有比例变化的回调。您可以覆盖 setScale 方法以将您的更改应用到 Sprite,但正如我上面所说,为此使用 Sprite 没有意义。您应该引用一个 TextureRegion,并在 draw() 方法中使用所有适当的参数绘制它。