2014-07-08 1 views
0

그래서 나는 flappy 새 복제품을 만들고 있습니다. 것은 내가 자바와 libgdx로 프로그래밍 할 때 새롭다는 것이고 당신의 도움을 요청하고 싶습니다. 전 화면을 클릭하는 대신 특정 영역 (단순한 직사각형 모양)에서 터치 감지를하고 싶습니다.Flappy 새 클론의 특정 영역에서 터치 감지

public class InputHandler implements InputProcessor { 
private Bird myBird; 
private GameWorld myWorld; 

// Ask for a reference to the Bird when InputHandler is created. 
public InputHandler(GameWorld myWorld) { 
    // myBird now represents the gameWorld's bird. 
    this.myWorld = myWorld; 
    myBird = myWorld.getBird(); } 

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

    if (myWorld.isReady()) { 
     myWorld.start(); 
    } 

    myBird.onClick(); 

    if (myWorld.isGameOver() || myWorld.isHighScore()) { 
     // Reset all variables, go to GameState.READ 
     myWorld.restart(); 
    } 

    return true; 
} 

@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 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; 
} 

}

+0

누군가가 조각을 채우기를 기대하기보다는 자습서를 따라하면 더 나을 것입니다. 모두 제일 좋다. – nikhil

답변

1

터치는 사각형의 경계 내에있는 경우에 true를 반환하는 방법을 만들기 :

여기 InputHandler 클래스 내 현재 코드입니다. 사각형 : 당신은 그냥 사각형의 호출, 이런 식으로 뭔가를 할 수 있도록

public boolean ContainsPoint(Rectangle rectangle, Point touch) 
{ 
    if (touch.X > rectangle.X && touch.X < rectangle.X + rectangle.Width && 
    touch.Y > rectangle.Y && touch.Y < rectangle.Y + rectangle.Height) 
    return true; 
    else 
    return false; 
} 

는 또한 사각형 클래스 자체에 다음과 같은 방법을 추가 할 수 있습니다.

ContainsPoint (새 포인트 (TouchXCoord, TouchYCoord)));

0

당신은 screenX와 screenY가 주어 지므로 가장 쉬운 방법은 터치 좌표를 확인하는 직사각형의 하드 코드 좌표입니다. 실제로 육각형을 표시하려면 화면을 그릴 때 사각형을 표시해야합니다.