머리말 : 바탕 화면의 모든 것이 완벽합니다. 전화로 프로젝트를 테스트 할 때 문제가 발생했습니다.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);
내가 반복 - 바탕 화면에 모든 버튼이 제대로 작동하지만 전화. 무엇이 문제 일 수 있습니까?
는'playButton으로 무엇인가 : 그럼 당신은 당신의 tochDown/업 방법에 대한 포인터를 관리해야
을 : 당신의 InputHandlerer 클래스에
당신은 initilize해야 InputHandler에서? – icarumbas@icarumbas create() 메서드에서 내 버튼입니다. menuButtons = new ArrayList(); playButton = new myButton (0, 560, 602, 180, playButtonUp); menuButtons.add (playButton); ' –
ErosLove
Scene2D 라이브러리를 사용하고 싶지 않은 이유는 무엇입니까? Stage를 사용하여 InputProcessor로 설정하고 버튼을 추가하십시오. 자신의 Button 클래스를 구현할 이유가 없습니다. https://github.com/libgdx/libgdx/wiki/Scene2d.ui – Arctic45