我想在触摸硬币对象时收集硬币。所以我喜欢这样。
if (Gdx.input.justTouched()) {
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
game.camera.unproject(touchPos);
for (int i = 0; i < coins.length; i++) {
Rectangle textureBounds = new Rectangle(coins[i].getX(), coins[i].getY(), coins[i].getWidth(),coins[i].getHeight());
if (textureBounds.contains(touchPos.x, touchPos.y) && !coins[i].isBreakBool() && coins[i].isCoinVisible()) {
//after touch something happens
}
}
}
}
我增加了分数,一切都是正确的,除了触摸。我想在触摸后立即使硬币不可见/移动。这不会发生在justTouched()。
所以我想为此使用输入处理器触摸事件。 我的输入处理器类有这样的触地事件。
public class MyInputProcessor implements InputProcessor,GestureListener {
public static boolean isTouchDown=false;
public static boolean isTouchUp=false;
public static boolean isTap=false;
public static boolean isLongPress=false;
public static boolean isFling=false;
public static boolean isSwipeDown=false;
public static boolean isSwipeUp=false;
public static boolean isSwipeLeft=false;
public static boolean isSwipeRight=false;
public static boolean isZoomed=false;
public static float zoomInitDist=0;
public static float zoomDist=0;
public MyInputProcessor() {
// TODO Auto-generated constructor stub
System.out.println("My Input Processor Created..");
}
public InputMultiplexer returnInput() {
// TODO Auto-generated method stub
InputMultiplexer im = new InputMultiplexer();
GestureDetector gd = new GestureDetector(this);
im.addProcessor(gd);
im.addProcessor(this);
return im;
}
@Override
public boolean touchDown(float x, float y, int pointer, int button) {
// TODO Auto-generated method stub
this.isTouchDown=true;
return true;
}
@Override
public boolean tap(float x, float y, int count, int button) {
// TODO Auto-generated method stub
this.isTap=true;
return true;
}
@Override
public boolean longPress(float x, float y) {
// TODO Auto-generated method stub
this.isLongPress=true;
return true;
}
@Override
public boolean fling(float velocityX, float velocityY, int button) {
// TODO Auto-generated method stub
if(Math.abs(velocityX)>Math.abs(velocityY)){
if(velocityX>0){
this.isSwipeRight=true;
}else{
this.isSwipeLeft=true;
}
}else{
if(velocityY>0){
this.isSwipeDown=true;
}else{
this.isSwipeUp=true;
}
}
return true;
}
@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean panStop(float x, float y, int pointer, int button) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean zoom(float initialDistance, float distance) {
// TODO Auto-generated method stub
this.isZoomed=true;
this.zoomInitDist=initialDistance;
this.zoomDist=distance;
return true;
}
@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
// TODO Auto-generated method stub
return true;
}
@Override
public void pinchStop() {
// TODO Auto-generated method stub
}
@Override
public boolean keyDown(int keycode) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean keyUp(int keycode) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return true;
}
}
在gameScreen类中,
MyInputProcessor myInputProcessor = new MyInputProcessor();
InputMultiplexer im = myInputProcessor.returnInput(stage);
Gdx.input.setInputProcessor(im);
但我对如何在输入处理器的触地事件中获得此触摸位置感到困惑。
答案 0 :(得分:1)
您的MyInputProcessor
应该是这样的。
public class MyInputProcessor implements InputProcessor {
Vector3 touchPos;
GameScreen gameScreen;
public static InputMultiplexer getMux(GameScreen gameScreen){
InputMultiplexer im = new InputMultiplexer();
im.addProcessor(gameScreen.stage);
im.addProcessor(new MyInputProcessor(gameScreen));
return im;
}
public MyInputProcessor(GameScreen gameScreen){
touchPos=new Vector3();
this.gameScreen=gameScreen; // keep reference to access data member of GameScreen
}
@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 touchDown(int screenX, int screenY, int pointer, int button) {
touchPos.set(screenX,screenY,0);
gameScreen.game.camera.unproject(touchPos);
Coin coins[]=gamescreen.coins;
for (int i = 0; i < coins.length; i++) {
Rectangle textureBounds = new Rectangle(coins[i].getX(), coins[i].getY(), coins[i].getWidth(),coins[i].getHeight());
if (textureBounds.contains(touchPos.x, touchPos.y) && !coins[i].isBreakBool() && coins[i].isCoinVisible()) {
//after touch something happens
}
}
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;
}
}
在InputProcessor
课程中将其设为GameScreen
。
Gdx.input.setInputProcessor(MyInputProcessor.getMux(this));