2017-06-22 64 views

回答

0

您可以使用InputProcessor从用户点击其中的用户基础处理输入。关于这方面的一个引导件被示出here

还是我优选的方法是使用scene2d并使用其在该LibGDX simple button with image

下面解释该代码是一个单一的按钮上点击时,改变图像的一个例子的的ImageButton:

package com.mygdx.gtest; 

import com.badlogic.gdx.ApplicationAdapter; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.Color; 
import com.badlogic.gdx.graphics.Pixmap; 
import com.badlogic.gdx.graphics.Pixmap.Format; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 
import com.badlogic.gdx.scenes.scene2d.Stage; 
import com.badlogic.gdx.scenes.scene2d.ui.Button; 
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle; 
import com.badlogic.gdx.scenes.scene2d.ui.Table; 
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; 

public class Test extends ApplicationAdapter{ 

    private Stage stage; 

    @Override 
    public void render() { 
     super.render(); 
     stage.act(); 
     stage.draw(); 
    } 

    @Override 
    public void create() { 
     //create images 
     Pixmap pmap = new Pixmap(100,100,Format.RGBA4444); 
     pmap.setColor(Color.RED); 
     pmap.fill(); 
     Texture buttonUpRed = new Texture(pmap); 
     pmap.setColor(Color.BLUE); 
     pmap.fill(); 
     Texture buttonDownBlue = new Texture(pmap); 
     pmap.dispose(); 

     //make button style (usually done with skin) 
     ButtonStyle tbs = new ButtonStyle(); 
     tbs.down = new TextureRegionDrawable(new TextureRegion(buttonDownBlue)); 
     tbs.up = new TextureRegionDrawable(new TextureRegion(buttonUpRed)); 

     // make button 
     Button btn = new Button(tbs); 

     //make stage 
     stage = new Stage(); 

     // ad button to layout table 
     Table table = new Table(); 
     table.add(btn); 

     //add table to stage 
     stage.addActor(table); 

     //set the stage to be the input processor so it responds to clicks 
     Gdx.input.setInputProcessor(stage); 

    } 

    @Override 
    public void dispose() { 
     super.dispose(); 
     stage.dispose(); 
    } 
} 
+0

我不喜欢[链接] https://gamedev.stackexchange.com/问题/ 121115/libgdx-simple-button-with-image。但是如何将该位置设置为舞台? –

+0

您可以使用Table,Stack,VerticalGroup等布局组件。关于如何使用它们以及它们在LibGdx wiki上做什么的一个很好的描述https://github.com/libgdx/libgdx/wiki/Scene2d.ui #布局 – dfour

0

您应该使用SpriteImage代替纹理的触摸检测。

Sprite类描述了一个纹理区域,它将被绘制的几何体以及它将被绘制的颜色。

如果你想使用scene2d,你可以使用Image,Image只显示一个drawable。可提拉可以是质感,纹理区域,ninepatch,雪碧等

您可以通过这种方式检测触摸:

public class TouchSystem extends ApplicationAdapter implements InputProcessor { 

    private SpriteBatch batch; 

    Texture up,down; 
    Sprite upSprite,downSprite; 
    OrthographicCamera camera; 
    Vector3 temp; 

    @Override 
    public void create() { 

     temp=new Vector3(); 
     camera=new OrthographicCamera(); 
     camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); 

     batch=new SpriteBatch(); 
     up=new Texture("top_arrow.png"); 
     down=new Texture("down_arrow.png"); 

     upSprite=new Sprite(up); 

     downSprite=new Sprite(down); 
     downSprite.setPosition(200,100); 

     Gdx.input.setInputProcessor(this); 
    } 

    @Override 
    public void render() { 

     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     batch.setProjectionMatrix(camera.combined); 
     batch.begin(); 
     upSprite.draw(batch); 
     downSprite.draw(batch); 
     batch.end(); 
    } 

    @Override 
    public void dispose() { 

     up.dispose(); 
     down.dispose(); 
     batch.dispose(); 
    } 

    @Override 
    public void resume() { 
     super.resume(); 
    } 

    @Override 
    public boolean keyDown(int keycode) { 
     return false; 
    } 

    @Override 
    public boolean keyUp(int keycode) { 
     return false; 
    } 

    @Override 
    public boolean keyTyped(char character) { 
     return false; 
    } 

    @Override 
    public boolean touchDown(int screenX, int screenY, int pointer, int button) { 

     temp.set(screenX,screenY,0); 
     camera.unproject(temp); 

     if(upSprite.getBoundingRectangle().contains(temp.x,temp.y)) 
      System.out.println("Touch on upSprite"); 

     if(downSprite.getBoundingRectangle().contains(temp.x,temp.y)) 
      System.out.println("Touch on downSprite"); 

     return false; 
    } 

    @Override 
    public boolean touchUp(int screenX, int screenY, int pointer, int button) { 
     return false; 
    } 

    @Override 
    public boolean touchDragged(int screenX, int screenY, int pointer) { 
     return false; 
    } 

    @Override 
    public boolean mouseMoved(int screenX, int screenY) { 
     return false; 
    } 

    @Override 
    public boolean scrolled(int amount) { 
     return false; 
    } 
} 
相关问题