如何在创建对象但不可见相机时更改定时器中的精灵?

时间:2016-07-25 19:33:58

标签: java android libgdx

如果对象不可见相机,我想在一段时间后更改其精灵。 我有一个创建对象的方法

  if (TimeUtils.nanoTime() - lastDropTime > 1000000000) {

            spawnRaindrop();

        }

这里我检查并比较时间并调用方法

Embed

OrthographicCamera相机;

2 个答案:

答案 0 :(得分:0)

你有没有想过制作一个类似于' raindropVisible'当你创建对象并生成它时,它将布尔值设置为true,当游戏处理它时,它会将其设置为false。然后让计时器检查布尔值是否为假,如果是,则更改精灵?

答案 1 :(得分:0)

如果您还没有完成它,您的Raindrop类应扩展libGDX Actor类,然后使用方法actdraw来处理所有内容。

此代码未经测试

class Raindrop extends Actor
{
    boolean onScreen = true;
    float offScreenTimer = 0.0f;
    final float OFFSCREENLLIMIT = 10.0f; // change as required

    // here you will also define all the other variables you need
    // such as movement speed, position information, sprite/image, etc

    public void act()
    {
        if ( onScreen )
        {
          if ( isOnScreen() ) // you'll need to create this method yourself!
          {
              // do all your on screen 'acting' here (movement, etc), but not the drawing
          }
          else
          {
              onScreen = false;
              setVisible(false);
          }
        }
        else
        {
            offScreenTimer += Gdx.gl.getDelta(); // <= this is most likely wrong, you'll need to double check the method name
            if ( offScreenTimer > OFFSCREENLLIMIT )
            {
                // here add all the code you need to change the image
                // and reset its movement and position values
                setVisible(true);
            }
        }
    }

    public void draw()
    {
        // add your code to handle the drawing of the image here
    }
}