2014-04-04 134 views
0

在我的Tile類中,我使用TextureAtlas's createSprite()sprites分配給每個圖塊。現在從我的screenLauncher類中,當我點擊tile時,我想要更改Tile的精靈,即圖像。如何實現這一點?瓷磚類使用TweenEngine來動畫瓷磚。使用通用吐溫引擎動態更改Sprite

我的瓷磚類:

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。

回答

2

如果您將瓷磚擴展爲Actor並將它們添加到Stage而不是手動撥打draw(),對您而言將會更容易。

  • 位置變量已經存在。您仍然可以以相同的方式對其進行動畫處理,並且看起來完全相同。
  • 此外,現在您有演員,您可以將ClickListener添加到他們。單擊Tile時,您可以更改Sprite成員的值並重新加載它。

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

好運。