Libgdx touchDragged()方法

时间:2015-05-01 16:59:09

标签: java android libgdx

我现在非常需要帮助。 我在android中使用 LIBGDX 制作游戏就像糖果粉碎一样。我在接口touchDragged()的{​​{1}}方法中遇到了困难。该代码据称可以检测向上滑动,向左滑动,向下滑动,向右滑动而不会触摸。但它有点不同,向左滑动意味着,我触摸糖果(touchCandy)然后拖动到左边的1个位置触摸糖果等等。 我有InputProcessor。这是我输入处理器的代码:

class Candy extends Actor

此外,当我将hitActor和hitDraggedActor转换为我的Candy Class时,它是相同的,就像我打印它打印的属性一样。 如果我需要澄清一些内容,请对其进行评论。 如果有人能够启发我,那将是非常棒的。提前谢谢。

修改 对不起我的坏,我正在打印同样的东西。上面的代码没有错误。 Gdx.app.log(" DEBUG"" X =&#34 + X +",Y =&#34 + X); 应该是:Gdx.app.log(" DEBUG"," x =" + x +",y =" + y);

1 个答案:

答案 0 :(得分:0)

如果你想检测向上,向下,向左和向右滑动,那么这段代码会更好



public class SimpleDirectionGestureDetector extends GestureDetector {
	public interface DirectionListener {
		void onLeft();

		void onRight();

		void onUp();

		void onDown();
	}

	public SimpleDirectionGestureDetector(DirectionListener directionListener) {
		super(new DirectionGestureListener(directionListener));
	}
	
	private static class DirectionGestureListener extends GestureAdapter{
		DirectionListener directionListener;
		
		public DirectionGestureListener(DirectionListener directionListener){
			this.directionListener = directionListener;
		}
		
		@Override
        public boolean fling(float velocityX, float velocityY, int button) {
			if(Math.abs(velocityX)>Math.abs(velocityY)){
				if(velocityX>0){
						directionListener.onRight();
				}else{
						directionListener.onLeft();
				}
			}else{
				if(velocityY>0){
						directionListener.onDown();
				}else{                                  
						directionListener.onUp();
				}
			}
			return super.fling(velocityX, velocityY, button);
        }

	}

}




之后,您可以将输入处理器设置为该类的实例,如此



Gdx.input.setInputProcessor(new SimpleDirectionGestureDetector(new SimpleDirectionGestureDetector.DirectionListener() {
		
	@Override
	public void onUp() {
		// TODO Auto-generated method stub
	}

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

	}

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

	}

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

	}
}));




现在,您可以在侦听器的onUp,onDown,onLeft和onRight方法中实现自己的逻辑。此代码取自another website