更新android中的视图

时间:2014-03-19 12:30:54

标签: java android view observable

对于学校我正在尝试创建一个基于2D的网格游戏,你可以再次玩AI或其他玩家。但是我们遇到了人工智能问题。如果单击网格中的空图块,则会触发一个名为onEmptyTileClicked的方法,其中包含图块的X和Y坐标。我们使用这种方法在场上放置石头(每个玩家都有自己的石头,所以我们说你是蓝色的,AI是黄色的。)

您可以在此处看到一个基本示例:

public void onEmptyTileClicked(int x, int y) {
        GameBoard board = getGameBoard();
        int counter = 0;
        //check if the blue player needs to set or the yellow
        if (player1) {

            //sets the amount of diamonds that will be taken over in the var counter, if this is bigger than 0 we can place it
            counter = board.countTakeovers(x,y, true);
            if (counter > 0) {
                //add the diamond to the board
                board.addGameObject(new Blue(), x, y);

                //add a message in logcat and in the statistic log
                Log.d("CoolGameBoard", "Adding blue diamond");  
                activity.updateStatisticLog(activity.getString(R.string.blue_placed)+" "+(x+1)+", "+(y+1)+ " + " + counter + " " + activity.getString(R.string.conquered)); 

                //change all the diamonds that are taken over with this set
                board.takeover(x,y,true);

                //turn is over give the other player a turn and update the gameboard
                setDone();

            } else {

                //player clicked a wrong point, give them a error message
                Log.d("CoolGameBoard", "Can't place the blue diamond at this location.");
                activity.updateStatisticLog(activity.getString(R.string.cant_blue) + " "+(x+1)+", "+(y+1)); 
            }

        } else {

            counter = board.countTakeovers(x,y,false);
            if (counter > 0) {
                board.addGameObject(new Yellow(), x, y);
                Log.d("CoolGameBoard", "Adding yellow diamond");                
                activity.updateStatisticLog(activity.getString(R.string.yellow_placed)+" "+(x+1)+", "+(y+1)+ " + " + counter + " " + activity.getString(R.string.conquered));                                   
                board.takeover(x,y,false);              
                setDone();
            } else {
                Log.d("CoolGameBoard", "Can't place the yellow diamond at this location.");
                activity.updateStatisticLog(activity.getString(R.string.cant_yellow) + " "+(x+1)+", "+(y+1));       
            }
        }
    }

如果玩家放置了石头,将调用setDone()方法来处理一些视觉资料,例如更新游戏板和其他一些东西。此方法如下所示:

public void setDone() {
    GameBoard board = getGameBoard();
    player1 = !player1;

    activity.updateBeurt(player1);
    activity.updateBlueDiamonds(board.countBlueDiamonds());
    activity.updateYellowDiamonds(board.countYellowDiamonds());
    board.updateView();

    if (board.possibilities(player1) == 0) {
        player1 = !player1;
        activity.updateBeurt(player1);
        if (board.possibilities(player1) == 0) {
            Log.d("FDGame","GAME OVER!!");
            activity.gameOver();
        }
    }

    if(AI && !player1){
        computerPlayer.move();
    }
}

正如您在此方法结束时所看到的,如果布尔AI设置为true,我们将调用computerPlayer.move();这个方法将基本上让AI设置一个新的石头,我们再次使用onEmtpyTileClicked()方法。

我们希望在你作为一名玩家制作的套装和你的AI所做的套装之间保持睡眠,否则你将无法看到AI对场地的影响。但是,方法board.updateView()似乎是在整个onEmtpyTileClicked()循环结束时执行的。这个方法看起来像这样:

public void updateView() {
    Log.d(TAG, "Updating game view");

    setChanged();
    notifyObservers();
}

有人可能解释为什么notifyObservers()仅在某个线程似乎完成时触发?因为如果我们在AI移动之前添加一个睡眠,它看起来就像没有任何东西被放置一秒钟然后放置两个宝石,导致与之前相同,只是在它发生之前只有一段时间。

0 个答案:

没有答案