2017-12-09 19 views
0

머리말 : 바탕 화면의 모든 것이 완벽합니다. 전화로 프로젝트를 테스트 할 때 문제가 발생했습니다.libgdx : 잘못된 터치 처리

게임을 시작합니다. 화면의 일부 버튼은 button_1, button_2, button_3입니다. 예를 들어, 나는 button_1을 만지고있다. button_1을 다시 만지면 잘 동작합니다. -> touchDown (버튼 이미지가 10px로 내려갑니다) then touchUp (버튼 이미지가 10px 및 버튼 코드 실행으로 올라갑니다). 하지만 대신 button_2와 같은 다른 버튼을 터치하면 button_1의 touchDown 만 발생합니다 (button_1 이미지는 10 픽셀 이상 올라갑니다). 모든 버튼에 대해 발생하므로 버튼을 두 번 눌러 작동시켜야합니다.

Button 클래스 :

public class myButton { 

private float x, y, width, height; 
private Texture buttonUp; 
public Rectangle bounds; 

private boolean isPressed = false; 

public myButton(float x, float y, float width, float height, Texture buttonUp) { 
    this.x = x; 
    this.y = y; 
    this.width = width; 
    this.height = height; 
    this.buttonUp = buttonUp; 

    bounds = new Rectangle(x, y, width, height); 
} 

public boolean isClicked(int screenX, int screenY) { 
    return bounds.contains(screenX, screenY); 
} 

public void draw(SpriteBatch batch) { 
    if (isPressed) { 
     batch.draw(buttonUp, x, y - 10, width, height); 
    } else { 
     batch.draw(buttonUp, x, y, width, height); 
    } 
} 

public boolean isTouchDown(int screenX, int screenY) { 
    if (bounds.contains(screenX, screenY)) { 
     isPressed = true; 
     return true; 
    } 
    return false; 
} 

public boolean isTouchUp(int screenX, int screenY) { 
    if (bounds.contains(screenX, screenY) && isPressed) { 
     isPressed = false; 
     return true; 
    } 
    isPressed = false; 
    return false; 
} 

} 

InputHandlerer :

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

     screenX = (int) touchPos.x; 
     screenY = (int) touchPos.y; 

     playButton.isTouchDown(screenX, screenY); 

     return true; 
    } 

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

     screenX = (int) touchPos.x; 
     screenY = (int) touchPos.y; 

     if (playButton.isTouchUp(screenX, screenY)) { 
      start(); 
      return true; 
     } 
} 

에서이 (생성) 내가있어 방법 :

touchPos = new Vector3(); 

Gdx.input.setInputProcessor(new InputHandlerer()); 

camera = new OrthographicCamera(); 
viewport = new FitViewport(1080, 1920, camera); 

가에서 렌더링() 메소드가 난이있어 :

touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); 
camera.unproject(touchPos); 

batch.setProjectionMatrix(camera.combined); 

내가 반복 - 바탕 화면에 모든 버튼이 제대로 작동하지만 전화. 무엇이 문제 일 수 있습니까?

+0

는'playButton으로 무엇인가 : 그럼 당신은 당신의 tochDown/업 방법에 대한 포인터를 관리해야

private int button1pointer = -1; private boolean button1isPressed = false; 

을 : 당신의 InputHandlerer 클래스에

당신은 initilize해야 InputHandler에서? – icarumbas

+0

@icarumbas create() 메서드에서 내 버튼입니다. menuButtons = new ArrayList (); playButton = new myButton (0, 560, 602, 180, playButtonUp); menuButtons.add (playButton); ' – ErosLove

+0

Scene2D 라이브러리를 사용하고 싶지 않은 이유는 무엇입니까? Stage를 사용하여 InputProcessor로 설정하고 버튼을 추가하십시오. 자신의 Button 클래스를 구현할 이유가 없습니다. https://github.com/libgdx/libgdx/wiki/Scene2d.ui – Arctic45

답변

0

멀티 터치를 사용하지 않으려는 경우에도 포인터를 관리해야하는 대신 전화 대신 한 번에 한 번만 클릭 할 수 있기 때문에 데스크톱에있을 수 있습니다.

@Override 
public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
for (int i = 0; i < 2; i++) { //here you can choose how many touches you can manage 
    if (Gdx.input.isTouched(i)) { 

     screenX = (int) touchPos.x; 
     screenY = (int) touchPos.y; 

     if (playButton.isTouchDown(screenX, screenY) && (button1pointer != i) && (!button1isPressed)) { 
      button1pointer = i; 
      button1isPressed = true; 
     } 
     return true; 
    } 
} 

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

    if (!(Gdx.input.isTouched(pointer)) && (button1pointer == pointer) && (button1isPressed)) { 
     button1pointer = -1; 
     button1isPressed = false; 
     start(); 
     return true; 
    } 

} 
+0

이 작동하지 않습니다. 아직도 나는 터치 스크린 두 번째 시간이 필요하다. 하지만 이제는 두 번째로 만날지라도 버튼 코드가 ​​작동합니다. – ErosLove