2017-02-16 64 views
0

我是Libgdx的新手,我正在開發一個菜單屏幕有聲音按鈕的遊戲。這是默認情況下打開,我想更改爲其他聲音關閉PNG時觸摸意味着我想改變紋理的精靈,但下面的代碼似乎並沒有工作。如何更改屏幕上的雪碧

@Override  
public void show(){ 
    s = new Sprite(new Texture(Gdx.files.internal("soundon.png"))); 
} 

@Override 
public void render(float delta) { 
    camera.update(); 
    Gdx.gl.glClearColor(1, 1, 1, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    batch.setProjectionMatrix(camera.combined); 

    batch.begin(); 
    s.draw(batch); 
    s.setBounds(w-w/7,h*asp-w/7, w/10,w/10); 
    batch.end(); 
} 

public boolean touchUp (int x, int y, int pointer, int button) {  
    if(s.getBoundingRectangle().contains(x,y)){ 
      s.setTexture(new Texture(Gdx.files.internal("soundoff.png")); 
    } 
    return true; 
} 

回答

1

見爲什麼雪碧未檢測到觸摸: https://stackoverflow.com/a/42233113/3445320

嘗試這種方式:

private Vector3 vector3=new Vector3(); 

public boolean touchUp (int x, int y, int pointer, int button) { 
    vector3.set(x,y,0); 
    camera.unproject(vector3); 
    if(s.getBoundingRectangle().contains(vector3.x,vector3.y)){ 
     s.setTexture(new Texture(Gdx.files.internal("soundoff.png")); 
    } 
    return true; 
} 

建議:

不要箱子匿名紋理對象,保持你的紋理的參考,以便您可以稍後處理該紋理。

+1

:)它像魅力一樣。感謝兄弟。 –