【问题标题】:libGDX animation on and off开启和关闭 libGDX 动画
【发布时间】:2014-12-14 19:39:40
【问题描述】:

我有这个动画,我每秒在屏幕上生成并每秒删除它。问题出在我的计算上。我使用下面的条件每 1.26 秒生成一次:

if (TimeUtils.nanoTime() - longSpawn > 1260000000) spawnAnt();

要删除此代码:

if (TimeUtils.nanoTime() - longRemoveAnt > 1000000000) IteratorCircle.remove();

问题是longRemoveAnt不能高于或等于longSpawn,否则不会移除。但更重要的是,我应该如何计算它以便动画完全运行,然后将被删除并再次生成?

动画代码:

TextureRegion[] antArr = {a, b, c, d, e, f, g, h, i};
animAnt = new Animation(0.07f, antArr);
animAnt.setPlayMode(Animation.PlayMode.LOOP_PINGPONG);

【问题讨论】:

    标签: java android animation libgdx


    【解决方案1】:

    我认为您需要跟踪其中每一个的经过时间(或者在我的示例中,倒计时时间)。如果您不需要纳秒的精度,您可以使用Gdx.graphics.getDeltaTime(),但您可以像这样跟踪自己经过的纳秒。

    从您的问题中不清楚您在做什么,因此您可能需要修改下面的数学以使其按照您想要的方式运行。我让它每 1.26 秒生成一个动画,每 1.0 秒删除一个动画,但显然如果你删除它们的速度比生成它们的速度快,就会出现问题,你的问题暗示你正在这样做。

    //member variables
    static final long SPAWN_TIME_NANOS = 1260000000;
    static final long REMOVE_TIME_NANOS = 1000000000;
    long timeToNextSpawn = SPAWN_TIME_NANOS;
    long timeToRemoveAnt = SPAWN_TIME_NANOS + REMOVE_TIME_NANOS;
    long previousTimeNanos= -1;
    
    void spawnAnt(){
        Ant ant = Pools.obtain(Ant.class);
        ant.position.set(...); //however you are doing this.
        ant.age = 0;
    }
    
    //In render():
    long currentTimeNanos = TimeUtils.nanoTime();
    long deltaNanos = (previousNanoTime==-1) ? 0 : currentTimeNanos - previousTimeNanos;
    previousTimeNanos = currentTimeNanos;
    
    timeToNextSpawn -= deltaNanos;
    while (timeToNextSpawn <= 0){ //using while instead of if in case of long frame
        spawnAnt();
        timeToNextSpawn += SPAWN_TIME_NANOS;
    }
    
    timeToRemoveAnt -= deltaNanos;
    while (timeToRemoveAnt <= 0){
        IteratorCircle.remove();
        timeToRemoveAnt += REMOVE_TIME_NANOS;
    }
    

    我上面的回答是基于对你问题的字面理解,但如果你有一个可以跟踪自己年龄的“Ant”类,我觉得它更简单,更不容易出错,如下所示:

    public class Ant {
        float age;
        Vector2 position = new Vector2();
    }
    
    //member variables
    static final float SPAWN_TIME = 1.26f;
    static final float REMOVE_TIME = 1.0f;
    float timeToNextSpawn = SPAWN_TIME;
    LinkedList<Ant> ants = new LinkedList<Ant>(); //LinkedList makes it low cost to remove oldest
    
    void spawnAnt(){
        Ant ant = Pools.obtain(Ant.class); //Use pooling to reduce GC activity
        ant.age = 0;
        ant.position.set(.......);//however you do this
        ants.add(ant);
    }
    
    void removeOldestAnt(){
        Ant ant = ants.removeFirst();
        Pools.free(ant);
    }
    
    //In render():
    float deltaTime = Gdx.graphics.getDeltaTime();
    for (Ant ant : ants) {
        ant.age += deltaTime;
    }
    while (ants.size()>0 && ants.getFirst().age >= REMOVE_TIME) 
        removeOldestAnt(); //Assumes ants are ordered by age, which they should be in this code
    
    timeToNextSpawn -= deltaTime;
    while (timeToNextSpawn <= 0){ 
        spawnAnt();
        timeToNextSpawn += SPAWN_TIME;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多