android - Problem ending my game (UI Thread won't let me open a new Activity) -
i'm having trouble tying off program (ending up). have surfaceview in activity, , in surfaceview when lose 3 lives game supposed end. have tried opening new activity keydispatchingtimedout error. know has uithread. have been told create handler in activity , accessible surfaceview. can send message handler , can update ui(or launch new activity) in main ui thread. however...i have no idea how this! activity create handler in? 1 want launch new activity from? i'm lost here, , last piece need make program cohesive. if nothing else, how close activity? ui thread causing lot of problems :(
surface view
public class boardview extends surfaceview implements surfaceholder.callback{ context mcontext; // thread initialization private boardthread thread; thread timer; thread timer2; // box variables bitmap box = (bitmapfactory.decoderesource (getresources(), r.drawable.box)); private int box_x = 140; private int box_y = 378; private int boxwidth = box.getwidth(); private int boxheight = box.getheight(); // storage private vector<blossom> blossomvector = new vector<blossom>(); iterator<blossom> dataiterator = blossomvector.iterator(); // counters private int blossomnum = 0; private string score; private int currentscore = 0; private int lives = 3; boolean mode = false; boolean game = false; outputstreamwriter out = null; fileoutputstream fout = null; private static final string tag = "debug"; final paint scorepaint = new paint(); public boardview(context context){ super(context); scorepaint.setcolor(color.black); scorepaint.settextsize(12); scorepaint.settypeface(typeface.monospace); //surfaceholder provides canvas draw on getholder().addcallback(this); //set read/write data file root = environment.getexternalstoragedirectory(); // controls drawings thread = new boardthread(getholder(),this, blossomvector, dataiterator, box_x, box_y, boxwidth, boxheight); timer2 = new thread(){ public void run(){ while(game == false){ uicallback.sendemptymessage(0); try{ thread.sleep(5000); // change random } catch (interruptedexception e){ e.printstacktrace(); } } } }; timer = new thread(){ public void run(){ //makes sure player still has 3 lives left while(game == false){ uicallback.sendemptymessage(0); try { thread.sleep(2000); // wait 2 seconds before drawing next flower } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } //sleep 2 seconds } } }; timer.start(); timer2.start(); //intercepts touch events setfocusable(true); } @override public void ondraw(canvas canvas){ canvas.drawcolor(color.white); score = "" + currentscore; //note: pay attention order draw things //don't change order or else blossoms fall //on top of box, not "into" it. //display scoreboard canvas.drawtext("score: " + score,240,420,scorepaint); // uses synchronized method prevent concurrent modification drawblossoms(canvas); canvas.drawbitmap(box, box_x, box_y, null); } @override public boolean ontouchevent(motionevent event){ //handles movement of box if(event.getaction() == motionevent.action_down){ if(event.getx() > box_x & event.gety() > box_y & event.getx() < box_x + boxwidth & event.gety() < box_y + boxheight) { mode = true; } } if(event.getaction() == motionevent.action_move) { if(event.getx() > box_x & event.gety() > box_y & event.getx() < box_x + boxwidth & event.gety() < box_y + boxheight) { mode = true; } if(mode == true){ box_x = (int)event.getx(); } } if(event.getaction() == motionevent.action_up){ mode = false; } invalidate(); return true; } @override public void surfacechanged(surfaceholder holder, int format, int width, int height ){ log.v(tag, "surface changed"); //somehow these don't seem working } @override public void surfacecreated(surfaceholder holder){ thread.startrunning(true); thread.start(); } @override public void surfacedestroyed(surfaceholder holder){ log.v(tag, "surface destroyed"); try { thread.join(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } private handler uicallback = new handler(){ public synchronized void handlemessage(message msg){ //add new blossom blossom vector!! blossomvector.add(new blossom( (bitmapfactory.decoderesource (getresources(), r.drawable.blossom)))); dataiterator = blossomvector.iterator(); blossomnum++; log.v(tag, "number of blossoms =" + blossomnum); } }; private synchronized void drawblossoms(canvas c) // method draw flowers on screen , test collision { canvas canvas = c; dataiterator = blossomvector.iterator(); while (dataiterator.hasnext()) { blossom tempblossom = dataiterator.next(); tempblossom.draw(canvas); if (tempblossom.hit(box_x,box_y, box_x + boxwidth, box_y + boxheight, blossomvector) == true) { log.v(tag, "iterator works!"); dataiterator.remove(); currentscore += 100; } if (tempblossom.dropped() == true) { dataiterator.remove(); log.v(tag, "blossom dropped"); lives--; } if (lives == 0) { // stop thread makes blossoms game = true; //save highscore try { file root = environment.getexternalstoragedirectory(); if(root.canwrite()){ file highscoresfile = new file(root, "score.txt"); filewriter writer = new filewriter(highscoresfile, true); bufferedwriter out = new bufferedwriter(writer); //out.newline(); out.append(score); out.newline(); out.close(); } } catch (filenotfoundexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { thread.join(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } } }
}
thread
public class boardthread extends thread { private surfaceholder surfaceholder; private boardview boardview; private vector<blossom> blossomvector; private int boxx; private int boxy; private int boxwidth; private int boxheight; private boolean mrun =false; private iterator<blossom> iterator; private static final string tag = "debug"; public boardthread(surfaceholder holder, boardview boardview2, vector<blossom> blossomvector1, iterator<blossom> dataiterator, int box_x, int box_y, int boxw, int boxh) { surfaceholder = holder; boardview=boardview2; blossomvector = blossomvector1; iterator = dataiterator; boxx = box_x; boxy = box_y; boxw = boxwidth; boxh = boxheight; } public void startrunning(boolean run) { mrun=run; } @override public void run() { super.run(); canvas canvas; while (mrun) { canvas=null; try { canvas = surfaceholder.lockcanvas(null); synchronized (surfaceholder) { //update position //position(blossomvector, boxx, boxy, boxwidth, boxheight); // draw flowers boardview.ondraw(canvas); } } { if (canvas != null) { surfaceholder.unlockcanvasandpost(canvas); } } } }
which activity create handler in?
from activity showing surfaceview or boardview in case.
modify constructor of boardview to
public boardview(context context, handler mhandler)
now can thing using handler
Comments
Post a Comment