android - startActivity in Thread -
this code :
package gal.doron.ballinthehole; import android.content.intent; public class ballmover extends thread { private ball[] balls; private paddle paddle; private gamelevels levels; private gameview gameview; private enum directions {top, right, bottom, left, none}; public ballmover(ball[] b, paddle p, gamelevels levels, gameview view) { this.balls = b; this.paddle = p; this.levels = levels; this.gameview = view; } @override public void run() { while(true) { if(levels.isfinishlevel()) { levels.setcurrentlevel(levels.getcurrentlevel()+1); for(int i=1;i<balls.length;i++) { balls[i].setvisible(false); balls[i].restart(); } balls[0].restart(); try { sleep(1000); } catch (interruptedexception e) { e.printstacktrace(); } } for(int i=0;i<balls.length;i++) { if(this.balls[i].isvisible()) { this.balls[i].moveball(); checkhitpaddle(i); checkhitbrick(i); } } this.gameview.postinvalidate(); try { sleep(1); } catch (interruptedexception e) { e.printstacktrace(); } } } public void checkhitpaddle(int balli) { ball ball = balls[balli]; if(ball.left()<=paddle.right() && ball.right()>=paddle.left()) { if(ball.bottom()>=paddle.top()) { ball.bounceup(); } } else if(ball.bottom()>=paddle.bottom()+10){ intent gameoverscreen = new intent(ballmover.this, gameoverscreen.class); startactivity(gameoverscreen); } //ball.restart(); } public void checkhitbrick(int balli) { ball ball = balls[balli]; string direction = gethitbricksdirection(balli).name(); if(direction.compareto("top")==0) ball.bounceup(); if(direction.compareto("bottom")==0) ball.bouncedown(); if(direction.compareto("left")==0) ball.bounceleft(); if(direction.compareto("right")==0) ball.bounceright(); } public directions gethitbricksdirection(int balli) { ball ball = balls[balli]; bricks bricks = levels.getlevelbricks(); brick brick; for(int i=0;i<bricks.rows();i++) { for(int j=0;j<bricks.cols();j++) { brick = bricks.getbrick(i, j); if(brick.gettype()!=0) { if(ball.left()<=brick.right() && ball.right()>=brick.left()) { if(ball.bottom()-brick.top()>=0 && ball.bottom()-brick.top()<=1) { brick.ballhitbrick(); return directions.top; } if(ball.top()-brick.bottom()<=0 && ball.top()-brick.bottom()>=-1) { brick.ballhitbrick(); return directions.bottom; } } if(ball.top()<=brick.bottom() && ball.bottom()>=brick.top()) { if(ball.right()-brick.left()>=0 && ball.right()-brick.left()<=1) { brick.ballhitbrick(); return directions.left; } if(ball.left()-brick.right()<=0 && ball.left()-brick.right()>=-1) { brick.ballhitbrick(); return directions.right; } } } } } return directions.none; } }
i wanna make function send me gameoverscreen activity.
as can see problematics lines
intent gameoverscreen = new intent(ballmover.this, gameoverscreen.class); startactivity(gameoverscreen);
i know ballmover class thread , need activity..
does have solution?
the first parameter of intent
constructor object of context
class of of it's subclasses. 1 of ways resolve issue pass context
@ ballmover
constructor:
public ballmover(ball[] b, paddle p, gamelevels levels, gameview view, context context) { this.balls = b; this.paddle = p; this.levels = levels; this.gameview = view; this.context = context; }
by context
can pass application context, example. after can create intent
following:
intent myintent = new intent(context, gameoverscreen.class);
Comments
Post a Comment