首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在游戏结束时启动对话框而不单击按钮

如何在游戏结束时启动对话框而不单击按钮
EN

Stack Overflow用户
提问于 2014-03-19 21:48:18
回答 1查看 171关注 0票数 0

我对android非常陌生。我做了一个非常基本的乒乓球游戏。我想在球出界时启动对话框。这段代码表示球出了。

代码语言:javascript
复制
////// end game
        if (ball1.getEndGame()&&ball2.getEndGame()){
            Log.e(TAG, "END of game");
        }

这是我的活动:

代码语言:javascript
复制
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类,它是从视图扩展的

代码语言:javascript
复制
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

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-21 06:16:45

你试过了吗

代码语言:javascript
复制
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。您还可以根据需要向构建器添加其他选项。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22518850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档