【问题标题】:libgdx - how does the inputprocessor get it's coordinates?libgdx - 输入处理器如何获得它的坐标?
【发布时间】:2014-06-12 15:25:24
【问题描述】:

特别是,我试图让 touchdown 方法首先确定触摸是在屏幕的左侧还是右侧。我认为 screen 方法将屏幕宽度定义为 136 像素,但我的代码似乎将大约 1/4 的屏幕宽度解释为左键单击。

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    touchX=screenX;
    touchY=screenY;
    if(screenX<= 136/2) {
        myToken.onLeftClick();
    }else{
        myToken.onRightClick();
    }
    return true;
}

【问题讨论】:

    标签: android libgdx


    【解决方案1】:

    你需要使用camera.unproject来获取世界坐标。

    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        Vector3 vec=new Vector3(screenX,screenY,0);
        camera.unproject(vec);
    
        touchX=vec.x;
        touchY=vec.y;
        if(screenX<= 136/2) {
            myToken.onLeftClick();
        }else{
            myToken.onRightClick();
        }
        return true;
    }
    

    【讨论】:

    • 我收到 unproject 的错误消息:无法从静态上下文引用非静态方法 unproject。我现在在想:我应该在游戏初始化时获取坐标并让输入处理程序获取这些坐标吗?
    • 你的相机是静态的吗?
    • 我不这么认为,但它是我用来渲染游戏的完全不同类中的私有 OrthographicCamera。那么在那个类中,我应该在一组公共变量中获取屏幕坐标并让 inputhandler 读取它吗?
    • 我会在你有你的 touchdown 方法的类中添加一个私有的 OrthographicCamera,并添加一个带摄像头的类的构造函数。公共类名(OrthographicCamera 相机){ this.camera=camera; } 你明白吗?是一种将相机传递给其他班级的简单方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    相关资源
    最近更新 更多