【问题标题】:libGDX: smooth movement if justTouchedlibGDX:如果只是触摸则平滑移动
【发布时间】:2017-06-07 18:14:26
【问题描述】:

我想检查触摸屏是否被触摸并移动纹理的位置。通过检查input.isTouched(),它运行良好,并且在触摸触摸屏时我的纹理平滑移动。

public void update() {
   if(input.isTouched()){
      x += 60 * Gdx.graphics.getDeltaTime()
   }
}

public void render(SpriteBatch batch){
   batch.begin();
   batch.draw(texture, x, y); 
   batch.end();
} 

现在我想在input.justTouched() 时实现纹理的移动。在这种情况下,当我在做x += 600; 时,我的纹理只会移动一帧。我的想法是我的渲染方法中的第二种渲染方法,但我认为这不是有效的,老实说我真的不知道它是如何工作的。

【问题讨论】:

  • 不确定我是否理解这个问题,你想达到什么目的?当您触摸屏幕时,justTouched() 只会被调用一次,因此 x+=600 只会被调用一次。这才是重点。您是否希望它被重复调用?
  • 是的,我想要一个平滑的移动,例如 20 帧,而不是一帧 600 个单位的移动。
  • 可能有几种不同的方法来做到这一点,具体取决于您的应用程序的设置方式。快速破解可能是在 justTouched() 时为纹理设置状态/状态并将计数器设置为 20。然后后续 update() 调用将增加位置并从计数器减少 1。计数器命中 0,停止重新定位。如果您将场景 2d 与演员一起使用,请查看动作(如 moveBy)。或“介于”这两个极端之间的其他选项。

标签: android libgdx textures


【解决方案1】:
if(Gdx.input.isTouched()){    // condition true when screen is currently touched.
    x += 60 * Gdx.graphics.getDeltaTime();
}

因此,我们需要以某种方式保持条件/标志为真,以达到到达目的地所需的帧数,在您的情况下为 600。

有很多可能的解决方案来实现这一点,最简单的是:

public class GdxTest extends ApplicationAdapter {

   Texture texture;
   SpriteBatch spriteBatch;
   float x,y,endX;

   @Override
   public void create() {

      texture=new Texture("badlogic.jpg");
      spriteBatch=new SpriteBatch();
      y=20;
      x= endX=10;
   }

   @Override
   public void render() {

      Gdx.gl.glClearColor(1,1,1,1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      spriteBatch.begin();
      spriteBatch.draw(texture,x,y);
      spriteBatch.end();

      if(Gdx.input.justTouched()){   // when new touch down event just occurred.           
        endX=600;  // give destination
      }

      if(x<endX){    // this condition will true unless x reaches endX or crossed
        x+=60 * Gdx.graphics.getDeltaTime();
      }
   }

   @Override
   public void dispose() {
       texture.dispose();
       spriteBatch.dispose();
   }
}

【讨论】:

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