2016-07-05 3 views
0

텍스트 (LibGdx 사용)에 대한 클릭을 어떻게 감지 할 수 있습니까? 사용자가 텍스트를 클릭 할 때 글꼴 색을 변경하려고합니다. 그래서 이것은 내가 지금까지 가지고있는 것입니다.텍스트 감지 방법 libGdx

public class MainMenuScreen implements Screen,InputProcessor { 

final MainClass game; 
String playButton = "PLAY"; 
private int screenWidth; 
private int screenHeight; 

public MainMenuScreen(final MainClass gam){ 
    game=gam; 
    Gdx.input.setInputProcessor(this); 

    screenHeight = Gdx.graphics.getHeight(); 
    screenWidth = Gdx.graphics.getWidth(); 
} 

@Override 
public void render(float delta) { 

    Gdx.gl.glClearColor(0, 0.2f, 0, 10); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

    game.batch.begin(); 
    game.font.draw(game.batch,playButton,screenWidth/3,screenHeight/2); 
    game.batch.end(); 
} 

@Override 
public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
    //here, how to detect that the user clicked on the text?? 

    return true; 
} 

나는 화면의 왼쪽 중앙 근처에 텍스트를 표시했습니다. 사용자가 해당 텍스트를 클릭했는지 확인하는 방법 ??

답변

1

텍스트의 범위로 직사각형을 만들어야합니다. 이 경우 텍스트의 높이와 너비를 가져 오려면 GlypLayout이 필요합니다.

public class MainMenuScreen implements Screen,InputProcessor { 

final MainClass game; 
String playButton = "PLAY"; 
private int screenWidth; 
private int screenHeight; 

Rectangle recPlayButton; 
GlyphLayout layout; 

public MainMenuScreen(final MainClass gam){ 
    game=gam; 
    Gdx.input.setInputProcessor(this); 

    screenHeight = Gdx.graphics.getHeight(); 
    screenWidth = Gdx.graphics.getWidth();\ 

    layout = new GlyphLayout(); 
    recPlayButton = new Rectangle(); 

    layout.setText(game.font, playButton); 
    recPlayButton.set(screenWidth/3, screenHeight/2, layout.width, layout.height); 
} 

@Override 
public void render(float delta) { 

    Gdx.gl.glClearColor(0, 0.2f, 0, 10); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

    game.batch.begin(); 
    game.font.draw(game.batch,playButton,screenWidth/3,screenHeight/2); 
    game.batch.end(); 
} 

@Override 
public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
    Vector3 touchPos = new Vector3(screenX, screenY, 0); 

    if (recPlayButton.contains(touchPos.x, touchPos.y)) { 
     Gdx.app.log("Test", "Button was clicked"); 
     return true; 
    } 
    else 
     return false; 

    return true; 
} 
+0

감사합니다. –