繁体   English   中英

LibGDX播放器的动作

[英]LibGDX player movement

我正在开发2D太空射击游戏,玩家只能左右移动。 我在屏幕的左侧和右侧绘制了一个按钮,并不断检查是否被触摸。 问题是您必须抬起手指才能按下屏幕另一侧的按钮。 我希望飞船朝屏幕的最后触摸部分移动(即使您实际上没有抬起第一次触摸按钮的手指)。

    public void keyListener(float delta){

    //right movement
    if(Gdx.input.isKeyPressed(Keys.RIGHT) || (Gdx.input.isTouched() && game.cam.getInputInGameWorld().x >= arrowMoveX - 40) && !isPlayerHit)
        x+=SPEED*Gdx.graphics.getDeltaTime();


    //left movement
     if(Gdx.input.isKeyPressed(Keys.LEFT) || (Gdx.input.isTouched() && game.cam.getInputInGameWorld().x < arrowMoveWhite.getWidth() + 40) && !isPlayerHit)
        x-=SPEED*Gdx.graphics.getDeltaTime();

我尝试在这些语句中放置另一个if语句来检查第二个动作,但是这种方式只能在一个方向上起作用。 你能帮我么?

您需要了解的是,Android会按触摸屏幕的顺序来索引每个单独的触摸,该索引称为“指针”。 例如,当您仅用一根手指触摸屏幕时,触摸指针为0,第二个触摸指针为1。libGDX注册的最高指针为20。

对于您的特定情况,您只想读取当前正在读取触摸的最高指针上的输入,而让int读取最高的触摸。 您可以遍历指针,并将int设置为实际上是最高指针的任何触摸事件,最高指针指的是如下所示的最新印刷机:

int highestpointer = -1; // Setting to -1 because if the pointer is -1 at the end of the loop, then it would be clear that there was no touch 
for(int pointer = 0; pointer < 20; pointer++) {
     if(Gdx.input.isTouched(pointer)) { // First check if there is a touch in the first place
          int x = Gdx.input.getX(pointer); // Get x position of touch in screen coordinates (far left of screen will be 0)
          if(x < arrowMoveWhite.getWidth() + 40 || x >= arrowMoveX - 40) {
               highestpinter = pointer; 
          } // Note that if the touch is in neither button, the highestpointer will remain what ever it was previously
     }
} // At the end of the loop, the highest pointer int would be the most recent touch, or -1

// And to handle actual movement you need to pass the highest pointer into Gdx.input.getX()
if(!isPlayerHit) { // Minor improvement: only check this once
     if(Gdx.input.isKeyPressed(Keys.RIGHT) || (highestpointer > -1 && Gdx.input.getX(highestpointer) >= arrowMoveX - 40)) {
        x+=SPEED*Gdx.graphics.getDeltaTime();
     } else if(Gdx.input.isKeyPressed(Keys.LEFT) || (highestpointer > -1 && Gdx.input.getX(highestpointer) < arrowMoveWhite.getWidth() + 40)) {
          x-=SPEED*Gdx.graphics.getDeltaTime();
     }
}

请注意,您可能需要一台单独的摄像机来绘制按钮(或任何平视元素),因为您不必担心将屏幕坐标转换为世界坐标,因为x的方向相同。

让我知道它是如何工作的,如果您需要任何更改!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM