我的程序被设计成一个滑动益智游戏。我目前正在研究随机播放按钮,该按钮将使用随机数移动瓷砖一定的时间,以便玩家可以解决棋盘。
目前我遇到了一条错误消息:当我按下随机播放按钮时导致“无法执行活动的方法”。当它在while循环之外时,我可以继续按下按钮,但我必须在每次点击之间留出一定的时间。我在想我如何让程序超载……但我不确定如何减少负载……
public void shuffleTiles(View v){ //Shuffles the tiles using random numbers
int tileToMove;
while (computerMoves < 5){
tileToMove = randomNumbers.nextInt(12);
moveTile(tileToMove, false);
}
程序的主要症结是围绕这个方法工作的,它交换图像以显示瓷砖已经移动:
private void moveTile(int button, boolean playerMove) { //The main method which causes a tile to be moved
int tileToReplace = movePossibleTest(button); //Run the test to see if there is a blank square next to it
if (tileToReplace != 0){
Drawable originalImage = buttons[button-1].getDrawable(); //Saves a drawable of the original image
Drawable blankImage = buttons[tileToReplace-1].getDrawable(); //Saves a drawable of the blank tile
buttons[button-1].setImageDrawable(blankImage); //Swaps the images
buttons[button-1].setTag("blank");
buttons[tileToReplace-1].setImageDrawable(originalImage); //Swaps the tags that identify which is the blank tag
buttons[tileToReplace-1].setTag(null);
if (playerMove){ //If the move was initiated by a player click
moveCount(true);
}
else moveCount(false);
}
}