2017-02-16 63 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

:)它像魅力一样。感谢兄弟。 –