如何停止绘制在libgdx一个SpriteBatch动画? [英] How to stop drawing a SpriteBatch animation in libgdx?

查看:275
本文介绍了如何停止绘制在libgdx一个SpriteBatch动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个Libgdx动画spritebatch。只播放一次,当它完成我想要停止绘制它,所以它不会出现在屏幕上。我有一个检查一个布尔值,如果它仍然是活的,即动画,当动画使用完它被设置为false isAnimationFinished但是它停留在屏幕上,我不知道为什么即使日志打印显示其更改为false完成。有没有人有任何想法,这是为什么?感谢你的帮助。下面code是从我的渲染方法。

  Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); //这神秘的线清除屏幕。
     布尔活着= TRUE;
     stateTime + = Gdx.graphics.getDeltaTime(); //#15
        设置currentFrame = walkAnimation.getKeyFrame(stateTime,FALSE); //#16
        spriteBatch.begin();
        spriteBatch.draw(菜单,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
        如果(活着)
        spriteBatch.draw(设置currentFrame,50,50);
        如果(walkAnimation.isAnimationFinished(stateTime))
        活着= FALSE;
        的System.out.println(活的);
        spriteBatch.end();
        stage.draw();


解决方案

我看到的问题是,您总是是在年初宣布一个新的布尔变量活着,并设置活着真:

 布尔活着= TRUE;

,然后所有的绘图发生和所有的你,如果你的动画完成活着设置为false:

 如果(walkAnimation.isAnimationFinished(stateTime))
    活着= FALSE;

==>这是为时已晚,因为在这之后你不这样做什么了的本地的变量。


例如,你可以检查是否活着应该在一开始是真或假的权利,并相应设置的值,例如像:

 布尔活着= TRUE;
 如果(walkAnimation.isAnimationFinished(stateTime)){
    活着= FALSE;
 }
 //绘图code以下..

另外,您可以让活着的字段,而不是一个局部变量。

Hi I have a spritebatch animation in Libgdx. It only plays once and when it is finished I want to stop drawing it so it doesn't appear on the screen. I have a boolean that checks if it is still alive i.e. animating and it is set to false when the animation is finished using isAnimationFinished however it stays on the screen and I am not sure why even though a log print shows it changing to false on finish. Does anyone have any idea why this is? Thanks for your help. The following code is from my render method.

Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
     boolean alive = true;
     stateTime += Gdx.graphics.getDeltaTime();           // #15
        currentFrame = walkAnimation.getKeyFrame(stateTime, false);  // #16
        spriteBatch.begin();
        spriteBatch.draw(menu, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        if (alive)
        spriteBatch.draw(currentFrame,50,50);
        if(walkAnimation.isAnimationFinished(stateTime))
        alive = false;
        System.out.println(alive);
        spriteBatch.end();
        stage.draw();

解决方案

The problem I see is that you always declare a new boolean variable "alive" and set the alive to true at the beginning:

boolean alive = true;

and then all your drawing is happening and after all that you set alive to false if your animation is finished:

if(walkAnimation.isAnimationFinished(stateTime))
    alive = false;

==> That is too late, because after this you don't do anything anymore with this local variable.


You could for example check if "alive" should be true or false right at the beginning and set its value accordingly, like for example:

 boolean alive = true;
 if(walkAnimation.isAnimationFinished(stateTime)) {
    alive = false;
 }
 //your drawing code below..

alternatively you could make "alive" a field instead of a local variable.

这篇关于如何停止绘制在libgdx一个SpriteBatch动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆