使用通用补间引擎动态更改Sprite

时间:2014-04-04 11:18:32

标签: java libgdx tile tween

在我的Tile课程中,我使用sprites为每个图块分配TextureAtlas's createSprite()。现在从我的screenLauncher类中,我想在我点击tile时更改Tile的精灵,即图像。我怎么能实现这个目标? Tile类使用TweenEngine来生成tile的动画。

我的瓷砖类:

public class Tile {
private final float x, y;
public String title;
Sprite sprite;
private final Sprite interactiveIcon;
private final Sprite veil;
private final OrthographicCamera camera;
private final BitmapFont font;
private final TweenManager tweenManager;
private final MutableFloat textOpacity = new MutableFloat(1);

public Tile(float x, float y, float w, float h, String title, String spriteName, TextureAtlas atlas, TextureAtlas launcherAtlas, OrthographicCamera camera, BitmapFont font, TweenManager tweenManager) {
    this.x = x;
    this.y = y;
    this.title = title;
    this.camera = camera;
    this.font = font;
    this.tweenManager = tweenManager;
    this.sprite = launcherAtlas.createSprite(spriteName);
    this.interactiveIcon = atlas.createSprite("interactive");
    this.veil = atlas.createSprite("white");

    sprite.setSize(w, h);
    sprite.setOrigin(w/2, h/2);
    sprite.setPosition(x + camera.viewportWidth, y);

    interactiveIcon.setSize(w/10, w/10 * interactiveIcon.getHeight() / interactiveIcon.getWidth());
    interactiveIcon.setPosition(x+w - interactiveIcon.getWidth() - w/50, y+h - interactiveIcon.getHeight() - w/50);
    interactiveIcon.setColor(1, 1, 1, 0);

    veil.setSize(w, h);
    veil.setOrigin(w/2, h/2);
    veil.setPosition(x, y);
    veil.setColor(1, 1, 1, 0);
}

public void draw(SpriteBatch batch) {
    sprite.draw(batch);

    float wrapW = (sprite.getWidth() - sprite.getWidth()/10) * Gdx.graphics.getWidth() / camera.viewportWidth;

    font.setColor(1, 1, 1, textOpacity.floatValue());
    font.drawWrapped(batch, title,
        sprite.getX() + sprite.getWidth()/20,
        sprite.getY() + sprite.getHeight()*19/20,
        wrapW);

    if (veil.getColor().a > 0.1f) veil.draw(batch);
}

public void enter(float delay) {
    Timeline.createSequence()
        .push(Tween.to(sprite, SpriteAccessor.POS_XY, 0.7f).target(x, y).ease(Cubic.INOUT))
        .pushPause(0.1f)
        .push(Tween.to(interactiveIcon, SpriteAccessor.OPACITY, 0.4f).target(1))
        .delay(delay)
        .start(tweenManager);
}
public boolean isOver(float x, float y) {
    return sprite.getX() <= x && x <= sprite.getX() + sprite.getWidth()
        && sprite.getY() <= y && y <= sprite.getY() + sprite.getHeight();
}

public String getTitle()
{
    return this.title;
}
}

我想知道在LibGdx中是否可以使用通用补间引擎动态更改Sprites。

1 个答案:

答案 0 :(得分:2)

如果您将图块展开Actor并将其添加到Stage而不是手动调用draw(),则会更容易。

  • 位置变量已存在。您仍然可以以相同的方式为其设置动画,并且它看起来完全相同。
  • 此外,由于您现在有演员,您可以向他们添加ClickListener。单击Tile后,您可以更改Sprite成员的值并重新加载。

您甚至可以参考How to detect when an actor is touched in libgdx?

祝你好运。