Libgdx : Sprite 想要停止移动;
Libgdx : Sprite want stop moving;
我想让精灵走到鼠标位置。但是,当鼠标被点击,精灵走到鼠标所在的位置时,它不会停下来,一直往同一个方向移动。
这是我的代码:
public void render(float delta) {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
if(Gdx.input.isTouched()){
projected= new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
cam.unproject(projected);
if(position.x != projected.x || position.y != projected.y){
pathX = projected.x - position.x;
pathY = projected.y - position.y;
distance = (float) Math.sqrt(pathX * pathX + pathY * pathY);
directionX = pathX / distance;
directionY = pathY / distance;
}
}
position.x += directionX * Speed * delta;
position.y += directionY * Speed * delta;
}
请帮忙,非常感谢。
这基本上是 How to stop drawing a SpriteBatch animation in libgdx? || how do I pause the SpriteAnimation in libGDX with a button to be able to view the current frame when pause button is pressed? 的副本,请查看这篇文章,看看它是否有帮助。
简而言之,您想跟踪玩家的移动,当他们到达鼠标的 x、y 坐标时,将移动设置为 false。让渲染中的逻辑仅在布尔值为真时渲染。
我想让精灵走到鼠标位置。但是,当鼠标被点击,精灵走到鼠标所在的位置时,它不会停下来,一直往同一个方向移动。
这是我的代码:
public void render(float delta) {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
if(Gdx.input.isTouched()){
projected= new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
cam.unproject(projected);
if(position.x != projected.x || position.y != projected.y){
pathX = projected.x - position.x;
pathY = projected.y - position.y;
distance = (float) Math.sqrt(pathX * pathX + pathY * pathY);
directionX = pathX / distance;
directionY = pathY / distance;
}
}
position.x += directionX * Speed * delta;
position.y += directionY * Speed * delta;
}
请帮忙,非常感谢。
这基本上是 How to stop drawing a SpriteBatch animation in libgdx? || how do I pause the SpriteAnimation in libGDX with a button to be able to view the current frame when pause button is pressed? 的副本,请查看这篇文章,看看它是否有帮助。
简而言之,您想跟踪玩家的移动,当他们到达鼠标的 x、y 坐标时,将移动设置为 false。让渲染中的逻辑仅在布尔值为真时渲染。