简体   繁体   中英

Change Sprite dynamically using Universal Tween Engine

In my Tile class, I assign sprites to each tile using TextureAtlas's createSprite() . Now from my screenLauncher class , I want to change the Tile's sprite ie image when I click on the tile.How can I achieve this? Tile class makes use of TweenEngine for animation of the tiles.

My Tile Class :

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;
}
}

I want to know if it is possible in LibGdx using universal tween engine to change Sprites dynamically.

It would be much easier for you if you make your tiles extend Actor and add them to Stage in stead of calling draw() manually.

  • Position variables will already be there. You can still animate it in the same way and it'll look exactly same.
  • Additionally, as you have actors now, you can add ClickListener s to them. When the Tile is clicked, you can change the value of Sprite member and reload it.

You can even refer to How to detect when an actor is touched in libgdx?

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM