android - Libgdx for loop lag -
i have problems game. game runs ok until time have reposition objects, happens every frame. example here have reposition parked cars (around 6 cars on each side of road), cars moving down in screen, reset , reposition @ top again. have used poolable save resources. however, game still jumpy when comes running these lines of code. there way improve code prevent lag? maybe new thread? if how right way of creating new thread , updating every frame.
private void updateparkedvehicles(float delta){ (int = 0; < parkedcarleft1array.size; i++){ parkedcarleft1array.get(i).update(delta); (int c = 0; c < parkedcarleft1array.size; c++){ if (c != i){ if (intersector.overlaps(parkedcarleft1array.get(i).bounds, parkedcarleft1array.get(c).bounds)){ parkedcarleft1array.get(i).reset(); } } } } (int = 0; < parkedcarright1array.size; i++){ parkedcarright1array.get(i).update(delta); (int c = 0; c < parkedcarright1array.size; c++){ if (c != i){ if (intersector.overlaps(parkedcarright1array.get(i).bounds, parkedcarright1array.get(c).bounds)){parkedcarright1array.get(i).reset(); } } } } }
one way handle items in game through use of scene2d. scene2d allows step out having move items. instead give them instructions (as director would) if , when need move them instead of having handle yourselve every frame update.
in nutshell, add action this:
if(somecondition){ movetoaction movecar = new movetoaction(); movecar.setposition(anx,any); movecar.setduration(aduration); mycaractor.addaction(movecar); }
that way, thing need check occurence of event , call function move car once (when event occurs).
libgdx take care of updating actor each frame. thing need in loop check collision. if occur assign new action actor.
another thing keep in mind, if use scene2d can use camera. means if need scroll, instead of moving each item, move camera (which has convenience method need zoom etc).
for more information on scene2d check out: https://github.com/libgdx/libgdx/wiki/scene2d
for more information on actions check out: https://github.com/libgdx/libgdx/wiki/scene2d#actions
hope helps.
Comments
Post a Comment