在我的Tile类中,我使用spritesTextureAtlas's createSprite()分配给每个图块。现在从我的screenLauncher类中,我想在单击tile时更改Tile的sprite,即图像。 Tile类将TweenEngine用于动画的瓦片。

我的瓷砖课:

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。

最佳答案

如果您将磁贴扩展为Actor并将其添加到Stage中,而不是手动调用draw(),则对您来说会容易得多。


位置变量将已经存在。您仍然可以以相同的方式对其进行动画处理,并且外观将完全相同。
此外,由于现在有演员,您可以向他们添加ClickListener。单击Tile时,可以更改Sprite成员的值并重新加载。


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

祝好运。

09-11 18:47
查看更多