[英]libGDX: smooth movement if justTouched
I want to check if the touchscreen is touched and move the position of my texture. 我想检查触摸屏是否被触摸并移动纹理的位置。 With the check input.isTouched()
it works well and i get a smooth movement of my texture while my touchscreen is touched. 使用check 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();
}
Now I want to implement the movement of my texture when input.justTouched()
. 现在,我想在input.justTouched()
时实现纹理的移动。 In this case my texture will move in only one frame when i'm doing x += 600;
在这种情况下,当我执行x += 600;
时,我的纹理将仅移动一帧x += 600;
. 。 My idea is a second render method in my render method, but i think that's not efficient and honestly i don't really know how it works. 我的想法是我的render方法中的第二个render方法,但是我认为这样效率不高,说实话我真的不知道它是如何工作的。
if(Gdx.input.isTouched()){ // condition true when screen is currently touched.
x += 60 * Gdx.graphics.getDeltaTime();
}
So somehow we need to maintain condition/flag true for required number of frame to reach destination that is 600 in your case. 因此,在某种情况下,我们需要为到达目的地的所需帧数保持条件/标志为true(在您的情况下为600)。
Having many possible solution to achieve this, and the simplest is : 有许多解决方案可以实现这一目标,最简单的方法是:
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.