2013-12-12 2 views
2

Я пытаюсь сделать вещь типа рогатки (как у гнездящихся птиц) с использованием LIBGDX (в настоящее время обучения). Итак, как мне получить координаты точки, в которой пользователь коснулся экрана и перетащил палец в другую точку n, выпущенную. Я хочу, чтобы обе координировали начальную и выпущенную точку.Получить координаты до и после касания выпущен

Я также хочу знать, как достичь вышеуказанного в Android SDK/NDK.

ответ

2

Я не проверял это, но это должно быть вокруг о том, что вам нужно

@Override 
Public boolean onTouchEvent (MotionEvent event) { 
int downx; 
int downy; 
int upx; 
int upy; 
switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
     downy = (int)event.gety(); 
     downx = (int)event.getx(); 
    case MotionEvent.ACTION_MOVE: 
    case MotionEvent.ACTION_UP: 
     upy = (int)event.gety(); 
     upx = (int)event.getx(); 
} 
return false; 
} 
+0

РАСЧ может иметь широкий спектр классов, так что вы можете изменить их внутри метода –

+0

это один для родной андроида? – Vineet

+0

Я не делал андроид через год или 2, но из памяти это должно работать –

0
public class TempScreen extends Screen 
{ 

Game game; // the class which is implementing the InputProcessor 
SpriteBatch batcher; 
Vector3 touchPoint; 
OrthographicCamera cam; 
InputMultiplexer multiplexer; 
GestureListener listener=new GestureListener() 
{ 

    @Override 
    public boolean zoom(float initialDistance, float distance) 
    { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean touchDown(float x, float y, int pointer, int button) 
    { 
     cam.unproject(touchPoint.set(x, y, 0)); 
     System.out.println(touchPoint); 
     return false; 
    } 

    @Override 
    public boolean tap(float x, float y, int count, int button) 
    { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, 
      Vector2 pointer1, Vector2 pointer2) 
    { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean panStop(float x, float y, int pointer, int button) 
    { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean pan(float x, float y, float deltaX, float deltaY) 
    { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean longPress(float x, float y) 
    { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean fling(float velocityX, float velocityY, int button) 
    { 
     // TODO Auto-generated method stub 
     return false; 
    } 
}; 

GestureDetector detector=new GestureDetector(listener) 
{ 
    public boolean touchUp(float x, float y, int pointer, int button) 
    { 
     cam.unproject(touchPoint.set(x, y, 0)); 
     System.out.println(touchPoint); 

     return false; 
    } 
}; 
public TempScreen(Game game,SpriteBatch batcher) 
{ 
    super(game); 
    this.batcher=batcher; 
    this.game=game; 
    touchPoint = new Vector3(); 
    cam = new OrthographicCamera(GameConstants.CAMERA_WIDTH,GameConstants.CAMERA_HEIGHT); 
    cam.position.set(GameConstants.CAMERA_WIDTH/2,GameConstants.CAMERA_HEIGHT/2, 0); 
    multiplexer = new InputMultiplexer(); 
    multiplexer.addProcessor(game); 
    multiplexer.addProcessor(detector); 
    Gdx.input.setInputProcessor(multiplexer); 

} 

@Override 
public void render(float deltaTime) 
{ 
    update(deltaTime); 
    GLCommon gl = Gdx.gl; 
    gl.glClearColor(0, 0f, 1f, 0.1f); 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
    cam.update(); 
    batcher.setProjectionMatrix(cam.combined); 
    batcher.enableBlending(); 
    batcher.begin(); 
    draw(deltaTime); 
    batcher.end(); 
} 

@Override 
public void draw(float deltaTime) 
{ 
    // TODO Auto-generated method stub 

} 

@Override 
public void update(float deltaTime) 
{ 
    // TODO Auto-generated method stub 

} 

@Override 
public void backKeyPressed() 
{ 
    // TODO Auto-generated method stub 

} 

}

+0

отправил вам индивидуальный код для вас, он работает для вас, я знаю, что я отредактирую свой ответ здесь –

+0

:: Это сработало ... Thanx – Vineet

0

Оформить InputProcessor и Vector3:

MyInputprocessor myInputprocessor; 
Vector3 touchPoint = new Vector3(); 

Определить InputProcessor например:

public class MyInputprocessor implements InputProcessor{ 

    @Override 
    public boolean touchDown(int screenX, int screenY, int pointer, int button){ 
     guicam.unproject(touchPoint.set(screenX, screenY, 0)); //Initial point coordinates 
     return false; 
    } 

    @Override 
    public boolean touchDragged(int screenX, int screenY, int pointer){ 
     guicam.unproject(touchPoint.set(screenX, screenY, 0)); //current point coordinates (when you are dragging it) 
     return false; 
    } 

    @Override 
    public boolean touchUp(int screenX, int screenY, int pointer, int button){ 
     guicam.unproject(touchPoint.set(screenX, screenY, 0)); //final/release point coordinates 
     return false; 
    } 

    @Override public boolean keyTyped(char character){return false;} 
    @Override public boolean mouseMoved(int screenX, int screenY){return false;} 
    @Override public boolean scrolled(int amount){return false;} 
    @Override public boolean keyDown(int keycode){return false;} 
    @Override public boolean keyUp(int keycode){return false;} 
} 

Создайте его и установить его в ини код:

myInputprocessor = new MyInputprocessor(); 
Gdx.input.setInputProcessor(myInputprocessor); 
+0

Спасибо, сэр .. это сработало ... – Vineet

Смежные вопросы