android - LibGDX, intermittent lagging whilst moving an object -


i create little sprites of sheeps on top of screen, should go down , after crossing bottom line disappear. problem when going across screen noticable lag. milliseconds possible see it. happens absolutely randomly. change position gdx.graphics.getdeltatime();

    public void update (float deltatime) {         updatemotionx(deltatime);         updatemotiony(deltatime);          // move new position         position.x += velocity.x * deltatime;         position.y += velocity.y * deltatime; } 

here code of spawing them:

    private sheep spawnsheep(){     sheep sheep = new sheep();     sheep.dimension.set(dimension);      // select random cloud image     sheep.setregion(regsheeps.random());      // position     vector2 pos = new vector2();      pos.x = -0.19f; // position after end of level     pos.y = 5;      sheep.position.set(pos);      //speed     vector2 speed = new vector2();     speed.y = 3.5f;     sheep.terminalvelocity.set(speed);     speed.y *= -1;     sheep.velocity.set(speed);     return sheep;  } 

maybe had problem, have no idea why happens,

the lagging caused garbage collection because you're continuously allocating memory within game loop.

you want avoid calling new whenever possible in game loop, , if call new want reuse objects whenever possible.

look @ pools in libgdx http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/pool.html

if you're returning object method, such vector2, create static instance , reuse object return values. aware not thread safe , calling method again overwrite value first call.

if i'd search instances of word 'new' in code, , make sure none of them called within game loop. want aware of calling methods create instances of objects. simple things converting integer string waste memory, more obvious things creating copy of array.

you should find useful tracking memory allocation. https://visualvm.java.net/


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -