【问题标题】:Libgdx touchDragged() methodLibgdx touchDragged() 方法
【发布时间】:2015-05-01 16:59:09
【问题描述】:

我现在非常需要帮助。 我正在使用 android 中的 LIBGDX 制作类似于糖果迷恋的游戏。我很难使用接口InputProcessortouchDragged() 方法。该代码应该能够检测向上滑动、向左滑动、向下滑动和向右滑动而不解除触摸。但它有点不同,向左滑动意味着,我触摸糖果(touchCandy)然后拖动到触摸糖果左侧糖果的 1 位置等。 我有一个class Candy extends Actor。这是我的输入处理器的代码:

Actor hitActor;
boolean touched;
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    touched = true;
    touchVector = new Vector3(screenX,screenY,0);
    game.getCamera().unproject(touchVector);
    hitActor = stage.hit(touchVector.x,touchVector.y,false);
    }

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    // TODO Auto-generated method stub
    if(touched==true)
    {
     Actor hitDraggedActor;
     Vector3 touchVectorDragged = new Vector3(screenX,screenY,0);
     game.getCamera().unproject(touchVectorDragged);
     hitDraggedActor = stage.hit(touchVectorDragged.x,touchVectorDragged.y,false);
     if(hitDraggedActor!=hitActor) //<--i tried !hitDraggedActor.equals(hitActor)
     {
       Candy hitDraggedCandy = (Candy) hitDraggedActor;
       Candy hitCandy = (Candy) hitActor;
       //print attributes of hitDraggedCandy and hitCandy
      // result is similar when in fact, it should not
       //do something here
     }
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    touched=false;
    return false;
}

此外,当我将 hitActor 和 hitDraggedActor 投射到我的 Candy 类时,它是一样的,就像我打印属性时它打印的一样。 如果有什么我需要澄清的,请发表评论。 如果有人能启发我,那就太棒了。提前致谢。

编辑 对不起,我的错,我正在打印同样的东西。上面的代码没有错误。 gdx.app.log("调试","x="+x+",y="+x); 应该是:gdx.app.log("DEBUG","x="+x+",y="+y);

【问题讨论】:

  • 对不起,我的错。上面的代码没有错误。我正在打印这个 Gdx.app.log("LOG","x="+x+",y="+x);而不是 Gdx.app.log("LOG","x="+x+",y="+y);

标签: java android libgdx


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多