How to render a sprite 2 seconds after a collision

邮差的信 提交于 2019-12-25 06:42:22

问题


I want to draw/render my sprite 2 seconds after a collision.

How can I make it ?

If i do in that way the render add a new ButtonOrange immediately.

@Override
public void render(SpriteBatch batch)
{
    relation.add(new ButtonOrange(coordinates,text);
    relation.get(0).update();
    relation.get(0).draw(batch);

    if(relation.get(0).collission() == true)
        relation.remove(0);
}

回答1:


You can use a temporary timer variable to store time elapsed since collision and if its greater than 2 second, draw that sprite.

boolean flag = false;
float time = 0; //timer for 2 sec

@Override
public void render(SpriteBatch batch)
{
    if(body.collision == true)   //just a dummy code to check for collision
      flag = true;               //set flag to true if collided

    if(flag == true)
    {
      //keep track how much time has elapsed
      time += Gdx.graphics.getDeltaTime();     

      if(time > 2)           //if more than 2 sec
        sprite.draw(batch);       //draw sprite
    }

}


来源:https://stackoverflow.com/questions/32667282/how-to-render-a-sprite-2-seconds-after-a-collision

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!