【问题标题】:How to change Sprite on Touch如何更改 Sprite on Touch
【发布时间】:2017-02-16 19:08:57
【问题描述】:

我是 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;
}

【问题讨论】:

    标签: android libgdx


    【解决方案1】:

    查看为什么在 Sprite 上未检测到您的触摸: 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;
     }
    

    建议:

    不要创建匿名纹理对象,保留纹理的引用,以便稍后处理该纹理。

    【讨论】:

      猜你喜欢
      • 2017-01-19
      • 2019-02-22
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      相关资源
      最近更新 更多