这是我的代码:
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;
}
}
我想这样做,以便我的功能会将我发送给GameOverScreen Activity。
你可以看到问题行是
Intent GameOverScreen = new Intent(BallMover.this, GameOverScreen.class);
startActivity(GameOverScreen);
我知道BallMover类是Thread,它实际上需要是Activity ..
有人有解决方案吗?
答案 0 :(得分:1)
Intent
构造函数的第一个参数是任何子类的Context
类的任何对象。因此,解决此问题的方法之一是在Context
构造函数中传递BallMover
:
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;
}
例如,按Context
您可以传递应用程序上下文。之后,您可以创建Intent
,如下所示:
Intent myIntent = new Intent(context, GameOverScreen.class);