Java 使用通用Tween引擎动态更改精灵

Java 使用通用Tween引擎动态更改精灵,java,libgdx,tile,tween,Java,Libgdx,Tile,Tween,在我的平铺类中,我使用TextureAtlas的createSprite()为每个平铺指定sprite。现在,在我的screenLauncher类中,我想在单击互动程序时更改互动程序的精灵,即图像。我如何实现这一点?Tile类使用TweenEngine为Tile制作动画 我的瓷砖类: public class Tile { private final float x, y; public String title; Sprite sprite; private final Sprite inte

在我的平铺类中,我使用
TextureAtlas的createSprite()
为每个平铺指定
sprite
。现在,在我的screenLauncher类中,我想在单击互动程序时更改互动程序的精灵,即图像。我如何实现这一点?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;
}
}
公共类平铺{
私人最终浮动x,y;
公共字符串标题;
雪碧雪碧;
私人最终精灵互动;
私人最后的精灵面纱;
私人最终正交摄影机;
专用最终位图字体;
私人二等经理二等经理;
私有最终可变浮动textOpacity=新可变浮动(1);
公共磁贴(浮动x、浮动y、浮动w、浮动h、字符串标题、字符串spriteName、TextureAtlas、TextureAtlas launcherAtlas、正交摄影机、位图字体、TweenManager TweenManager){
这个.x=x;
这个。y=y;
this.title=标题;
这个。照相机=照相机;
this.font=font;
this.tweenManager=tweenManager;
this.sprite=launcheatlas.createSprite(spriteName);
this.interactiveIcon=atlas.createSprite(“交互式”);
this.veil=atlas.createSprite(“白色”);
雪碧。设定尺寸(w,h);
雪碧.刚毛起源(w/2,h/2);
sprite.setPosition(x+camera.viewportWidth,y);
interactiveIcon.setSize(w/10,w/10*interactiveIcon.getHeight()/interactiveIcon.getWidth());
设置位置(x+w-interactiveIcon.getWidth()-w/50,y+h-interactiveIcon.getHeight()-w/50);
setColor(1,1,1,0);
设置尺寸(w,h);
面纱。原始面纱(w/2,h/2);
设置位置(x,y);
setColor(1,1,1,0);
}
公共作废取款(SpriteBatch批量){
雪碧.抽(批);
float wrapW=(sprite.getWidth()-sprite.getWidth()/10)*Gdx.graphics.getWidth()/camera.viewportWidth;
setColor(1,1,1,textOpacity.floatValue());
字体。drawWrapped(批次、标题、,
sprite.getX()+sprite.getWidth()/20,
sprite.getY()+sprite.getHeight()*19/20,
wrapW);
如果(veil.getColor().a>0.1f)面纱绘制(批次);
}
公共无效输入(浮动延迟){
Timeline.createSequence()
.推(推到(雪碧,雪碧加速器位置,0.7f)。目标(x,y)。放松(立方英寸)
.推送暂停(0.1f)
.推送(到(交互控制,SpriteAccessor.OPACITY,0.4f)。目标(1))
.延迟(延迟)
.启动(tweenManager);
}
公共布尔值isOver(浮点x,浮点y){

return sprite.getX()如果您将平铺扩展并添加到中,而不是手动调用
draw()
,那么对您来说会容易得多

  • 位置变量已经存在了。你仍然可以用同样的方式设置它的动画,它看起来会完全一样
  • 此外,由于您现在有了演员,您可以向演员中添加。单击
    互动程序
    时,您可以更改
    精灵
    成员的值并重新加载它
你甚至可以参考

祝你好运