java - libgdx & kryonet: threads -
i try develop game android platform, using libgdx library. network, use kryonet library.
i want change screen when i'm sure application connected server.
network part seems work have problem threads: it's kryonet's thread execute opengl, not libgdx thread:
public class mygdxgame extends game { public static int udp_port = 55555, tcp_port = 55556; private client client; @override public void create() { /* * client connection * etc ... */ client.addlistener(new listener() { private int nb = 0; @override public void received(connection connection, object data) { super.received(connection, data); nb++; if (nb == 5) { mygdxgame.this.setsecondscreen(); } } }); setscreen(new first(this, client)); } protected void setsecondscreen() { setscreen(new second(this, client)); //this part should executed libgdx thread ?! }
note first , second both screen class draw image.
i have exception when try launch second screen: exception in thread "client" java.lang.runtimeexception: no opengl context found in current thread.
can force libgdx thread execute instructions ? other approach possible?
thank's jonathan
in libgdx 1 thread (the thread executes app lifecycle callbacks) has valid opengl context , can invoke opengl calls. can post runnable
gdx thread other threads execute stuff on behalf of other threads. posted runnables executed before next render callback run. see gdx.app.postrunnable()
in case this:
@override public void received(connection connection, object data) { super.received(connection, data); nb++; if (nb == 5) { final mygdxgame g = mygdxgame.this; gdx.app.postrunnable(new runnable() { public void run() { g.setsecondscreen(); }); } }
depending on how happens, may want avoid allocating new runnable
on each callback, if make mygdxgame
runnable
or make custom listener
implements runnable
should able avoid allocation.
Comments
Post a Comment