2017-02-16 7 views
0

私はLibgdxを初めて使っており、メニュー画面にサウンドボタンがあるゲームを開発中です。それはデフォルトでオンであり、触れたときにPNGの他のサウンドに変更したいのですが、そのスプライトのテクスチャを変更したいのですが、コードの下では動作しないようです。Touchでスプライトを変更する方法

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

が勧告:

、ないクレート匿名Textureオブジェクトを実行します。後でそのテクスチャを破棄できるように、テクスチャの参照を保持してください。

+1

:)それは魅力のように働いていました。 –

関連する問題