我对android很新。我做了一个非常基本的乒乓球比赛。我想在球出局时启动对话框。此代码表示球已经出局
////// end game
if (ball1.getEndGame()&&ball2.getEndGame()){
Log.e(TAG, "END of game");
}
这是我的活动:
public class GameScreenActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
View gameview = new GameView(this);
setContentView(gameview);
}
}
这是我从视图
扩展的GameView类public class GameView extends View {
// declare variables
private static final String TAG = null; // for log.e
private Ball ball1; // ball1
private Ball ball2; // ball2
private GameContainer gameContainer; // frame for the game
private Racket racket; // controller
int viewWidth = 0; // width onchange
int viewHeight = 0; // height onchange
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels; // width of screen
int height = metrics.heightPixels; // height of the screen
int rectSize = 200; // width of the controller
int rectHeight = 40; // height of the controller
int rectL = (width/2 - (rectSize/2)); // x
int rectT = height-(rectHeight*3); // y
int rectR = (width/2 + (rectSize/2)); // x + rectSize
int rectB = rectT + rectHeight; // y + rectHeight
// random numbers for speed
Random r = new Random();
int randomNo1 = r.nextInt(20 - 3) + 3;
int randomNo2 = r.nextInt(20 - 3) + 3;
int randomNo3 = r.nextInt(20 - 3) + 3;
int randomNo4 = r.nextInt(20 - 3) + 3;
// random numbers for location
int randomXLocation1 = r.nextInt((width-10) - 1) + 1;
int randomYLocation1 = r.nextInt(height/2 - 1) + 1;
int randomXLocation2 = r.nextInt((width-10) - 1) + 1;
int randomYLocation2 = r.nextInt(height/2 - 1) + 1;
// getter for speed
float getSpeedX;
float getSpeedY;
// score
private Score score;
int currentScore = 0;
// constructor
public GameView(Context context) {
super(context);
randomNo1 = randomNo1 * -1;
randomNo2 = randomNo2 * -1;
randomNo3 = randomNo3 * -1;
randomNo4 = randomNo4 * -1;
ball1 = new Ball(Color.GREEN, 24, randomNo1, randomNo2); // colour, radius,speedx, speedy
ball1.setBallLocation(randomXLocation1, randomYLocation1); // x, y of ball
ball2 = new Ball(Color.RED, 24, randomNo3, randomNo4); // colour, radius,speedx, speedy
ball2.setBallLocation(randomXLocation2, randomYLocation2); // x, y of ball
gameContainer = new GameContainer(Color.GRAY);
racket = new Racket(Color.BLACK);
score = new Score(Color.WHITE);
}
@Override
public void onDraw(Canvas canvas){
//randomNo1 = r.nextInt(20 - 1) + 1;
//randomNo2 = r.nextInt(20 - 1) + 1;
//randomNo3 = r.nextInt(20 - 1) + 1;
//randomNo4 = r.nextInt(20 - 1) + 1;
gameContainer.container(1, 2, width-2, height-3);
gameContainer.display(canvas);
ball1.drawBall(canvas);
ball1.moveBall(gameContainer);
//ball.collides(racket);
if(ball1.collides(racket)){
String collided = "collided Yes";
Log.e(TAG, collided);
getSpeedX = ball1.getBallSpeedX();
getSpeedY = ball1.getBallSpeedY();
getSpeedY = getSpeedY * -1;
randomNo1 = (int) getSpeedX;
randomNo2 = (int) getSpeedY;
if (randomNo1 > 0) { randomNo1 += 1; }else{randomNo1 = randomNo1 + (-1); }
if (randomNo2 > 0) { randomNo2 += 1; }else{randomNo2 = randomNo2 + (-1); }
ball1.setBallSpeed(randomNo1, randomNo2);
currentScore = currentScore + 1;
}
ball2.drawBall(canvas);
ball2.moveBall(gameContainer);
if(ball2.collides(racket)){
String collided = "collided Yes";
Log.e(TAG, collided);
getSpeedX = ball2.getBallSpeedX();
getSpeedY = ball2.getBallSpeedY();
getSpeedY = getSpeedY * -1;
randomNo3 = (int) getSpeedX;
randomNo4 = (int) getSpeedY;
if (randomNo3 > 0) { randomNo3 += 1; }else{randomNo3 = randomNo3 + (-1); }
if (randomNo4 > 0) { randomNo4 += 1; }else{randomNo4 = randomNo4 + (-1); }
ball2.setBallSpeed(randomNo3, randomNo4);
currentScore = currentScore + 1;
}
//racket.setRacket((width/2)-60, height-220, (width/2)+60, height-160);
racket.setRacket(rectL, rectT, rectR, rectB);
racket.setLine(2, rectT+(rectHeight/2)-1, width-2, rectT+(rectHeight/2)+1);
racket.drawRacket(canvas);
score.drawScore(canvas, 20, 40,Integer.toString(currentScore));
////// end game
if (ball1.getEndGame()&&ball2.getEndGame()){
Log.e(TAG, "END of game");
}
// Delay
try {
Thread.sleep(50);
} catch (InterruptedException e) { }
invalidate(); // re-draw
}
}
答案 0 :(得分:1)
if (ball1.getEndGame()&&ball2.getEndGame()){
Log.e(TAG, "END of game");
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage("End of game");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
//do whatever u want here
}
});
AlertDialog mAlert = builder.create();
mAlert.show();
}
mContext必须是您的应用程序上下文。同时在函数外声明mAlert。 您还可以根据需要向构建器添加其他选项。