java - What is wrong with my Game Thread? -
i have been trying while implement game thread utilise loop implement logic. posted question here not long ago, hope no 1 minds follow up.
i have managed scrape code research:
public class gameview extends surfaceview implements surfaceholder.callback { class gamethread extends thread { //states public static final int state_lose = 1; public static final int state_pause = 2; public static final int state_ready = 3; public static final int state_running = 4; private paint m_paint; //canvas dimensions private int m_canvaswidth; private int m_canvasheight; private long m_lasttime; private boolean m_run = false; private int m_mode; public imageview ship; relativelayout.layoutparams shipparams; // handle surface manager private surfaceholder m_surfaceholder; public gamethread(surfaceholder surfaceholder, context context, handler handler) { m_surfaceholder = surfaceholder; } //initialise game public void dostart() { synchronized (m_surfaceholder) { resetgame(); m_lasttime = system.currenttimemillis() + 100; setstate(state_running); ship = (imageview) findviewbyid(r.id.imageview1); shipparams = (relativelayout.layoutparams)ship.getlayoutparams(); } } public void pause() { synchronized (m_surfaceholder) { if (m_mode == state_running) setstate(state_pause); } } @override public void run() { while (m_run) { canvas c = null; try { c = m_surfaceholder.lockcanvas(null); synchronized (m_surfaceholder) { if (m_mode == state_running) { updategame(); } dodraw(c); } } catch(exception e){} { if (c != null) { m_surfaceholder.unlockcanvasandpost(c); } } } } public void setrunning(boolean b) { m_run = b; } public void setstate(int mode) { synchronized (m_surfaceholder) { setstate(mode, null); } } public void setstate(int mode, charsequence message) { synchronized (m_surfaceholder) { m_mode = mode; } } public void setplayers(boolean oneplayer) { } public void setsurfacesize(int width, int height) { synchronized (m_surfaceholder) { m_canvaswidth = width; m_canvasheight = height; } } public void unpause() { synchronized (m_surfaceholder) { m_lasttime = system.currenttimemillis() + 100; } setstate(state_running); } private void dodraw(canvas canvas) { canvas.drawargb(255, 0, 0, 0); } private void updategame() { long = system.currenttimemillis(); if (m_lasttime > now) return; double elapsed = (now - m_lasttime) / 1000.0; m_lasttime = now; system.out.print("hello world"); shipparams.topmargin++; ship.setlayoutparams(shipparams); } private boolean collided(rect rectangle) { return false; } public boolean foundwinner() { return false; } public void resetgame() { } public void handleinput(motionevent event) { } } private context m_context; private gamethread m_thread; private handler m_handler; public gameview(context context, attributeset attrs) { super(context, attrs); surfaceholder holder = getholder(); holder.addcallback(this); m_handler = new handler() { @override public void handlemessage(message m) { bundle b = m.getdata(); motionevent e = b.getparcelable("event"); m_thread.handleinput(e); } }; m_thread = new gamethread(holder, context, m_handler); setfocusable(true); }; public gamethread getthread() { return m_thread; } @override public void onwindowfocuschanged(boolean haswindowfocus) { if (!haswindowfocus) m_thread.pause(); } public void surfacechanged(surfaceholder holder, int format, int width, int height) { m_thread.setsurfacesize(width, height); } public void surfacecreated(surfaceholder holder) { if(m_thread.getstate() == state.terminated) { m_thread = new gamethread(getholder(), m_context, m_handler); m_thread.setrunning(true); m_thread.start(); m_thread.dostart(); } else { m_thread.setrunning(true); m_thread.start(); } } public void surfacedestroyed(surfaceholder holder) { boolean retry = true; m_thread.setrunning(false); while (retry) { try { m_thread.join(); retry = false; } catch (interruptedexception e) { } } } @override public boolean ontouchevent(motionevent event) { return true; } }
i issue lies here , merely logical one. seem fine me, , in need of assistance.
i have attempted draw image @ line 47 , defined movement take place in update method @ line 153. have placed print line debug, line doesn't show.
i stumped.
any great, thanks.
here other codes, if neccessary:
edit: should note i'm not getting kind of errors within code, merely doesn't respond
you initializing m_run false,then in while cycle in run() method must have set true. change true , thread work normally.
Comments
Post a Comment