检测手指何时离开屏幕并更新指针
detect when a finger leaves the screen and update pointers
我正在制作一个游戏,我用两根手指在屏幕上加速,一个手指在屏幕的左半部分,一个手指在屏幕的右半部分。
如果你松开你的手指并放下另一个手指,无论是什么我都必须根据手指的位置弯曲车辆。如果您位于右半边 (Gdx.grapchis.getWidth/2),那么我向右弯……然后向左弯。
部分输入处理器:
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(pointer < 2)
{
CoordinateTouch tmp = new CoordinateTouch();
tmp.x = screenX;
tmp.y = screenY;
coordinates.add(tmp);
}
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
coordinates.clear();
return false;
}
我的坐标数组:
public class CoordinateTouch{
float x;
float y;
}
List<CoordinateTouch> coordinates;
渲染方法中的控制指针(组是我的纹理):
if(coordinates.size() > 1)
{
group.addAction(parallel(moveTo(realDest.x, realDest.y, (float) 15)));
}
else
{
group.addAction(delay((float)1.5));
group.clearActions();
if(Gdx.input.isButtonPressed(0)) {
if (Gdx.input.getX() < Gdx.graphics.getWidth() / 2) {
group.addAction(parallel(rotateBy(velocityRotazionShip, (float) 0.03)));
} else {
group.addAction(parallel(rotateBy(-velocityRotazionShip, (float) 0.03)));
}
}
}
我的问题是,如果我离开一根手指检测到它,到目前为止一切顺利,但如果我只是向后靠着他的手指拉开,我不会更新指针并且不会产生这段代码group.addAction.
我也试过 isButtonPressed 和 isKeypressed, isTouched(index) ,结果是一样的
抱歉英语不好,我希望已经清楚了。
如果我正确地理解你,你有 3 个案例:
- 显示两侧都被按下 -> 移动
- 按下左侧显示屏 -> 向左倾斜
- 按下右侧显示屏 -> 向右倾斜
如果这个假设是正确的,你只需要 boolean
s:
boolean touchLeft, touchRight
在 touchDown
中你可以做这样的事情:
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if (screenX < Gdx.graphics.getWidth()/2)
touchLeft = true;
else
touchRight = true;
}
并且在 touchUp
:
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if (screenX < Gdx.graphics.getWidth()/2)
touchLeft = false;
else
touchRight = false;
}
现在在render
里面你可以说:
if (touchLeft && touchRight)
// move
else if (touchLeft)
// lean left
else if (touchRight)
// leanRight
else
// do nothing or something else
如果要支持多个fingers/side,可以将boolean
改为int
,给出fingers/side的个数。在 touchDown
中递增它,在 touchUp
中递减它并在渲染中询问它是否 > 0.
我正在制作一个游戏,我用两根手指在屏幕上加速,一个手指在屏幕的左半部分,一个手指在屏幕的右半部分。 如果你松开你的手指并放下另一个手指,无论是什么我都必须根据手指的位置弯曲车辆。如果您位于右半边 (Gdx.grapchis.getWidth/2),那么我向右弯……然后向左弯。
部分输入处理器:
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(pointer < 2)
{
CoordinateTouch tmp = new CoordinateTouch();
tmp.x = screenX;
tmp.y = screenY;
coordinates.add(tmp);
}
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
coordinates.clear();
return false;
}
我的坐标数组:
public class CoordinateTouch{
float x;
float y;
}
List<CoordinateTouch> coordinates;
渲染方法中的控制指针(组是我的纹理):
if(coordinates.size() > 1)
{
group.addAction(parallel(moveTo(realDest.x, realDest.y, (float) 15)));
}
else
{
group.addAction(delay((float)1.5));
group.clearActions();
if(Gdx.input.isButtonPressed(0)) {
if (Gdx.input.getX() < Gdx.graphics.getWidth() / 2) {
group.addAction(parallel(rotateBy(velocityRotazionShip, (float) 0.03)));
} else {
group.addAction(parallel(rotateBy(-velocityRotazionShip, (float) 0.03)));
}
}
}
我的问题是,如果我离开一根手指检测到它,到目前为止一切顺利,但如果我只是向后靠着他的手指拉开,我不会更新指针并且不会产生这段代码group.addAction.
我也试过 isButtonPressed 和 isKeypressed, isTouched(index) ,结果是一样的
抱歉英语不好,我希望已经清楚了。
如果我正确地理解你,你有 3 个案例:
- 显示两侧都被按下 -> 移动
- 按下左侧显示屏 -> 向左倾斜
- 按下右侧显示屏 -> 向右倾斜
如果这个假设是正确的,你只需要 boolean
s:
boolean touchLeft, touchRight
在 touchDown
中你可以做这样的事情:
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if (screenX < Gdx.graphics.getWidth()/2)
touchLeft = true;
else
touchRight = true;
}
并且在 touchUp
:
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if (screenX < Gdx.graphics.getWidth()/2)
touchLeft = false;
else
touchRight = false;
}
现在在render
里面你可以说:
if (touchLeft && touchRight)
// move
else if (touchLeft)
// lean left
else if (touchRight)
// leanRight
else
// do nothing or something else
如果要支持多个fingers/side,可以将boolean
改为int
,给出fingers/side的个数。在 touchDown
中递增它,在 touchUp
中递减它并在渲染中询问它是否 > 0.