【发布时间】:2012-05-18 18:00:59
【问题描述】:
我的程序被设计成一个滑动益智游戏。我目前正在研究随机播放按钮,该按钮将使用随机数移动瓷砖一定的时间,以便玩家可以解决棋盘。
目前我遇到了一条错误消息:当我按下随机播放按钮时,“无法执行活动的方法”。当它在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);
}
}
【问题讨论】:
-
我认为您不应该在 ui 线程中使用 while 循环 - 它确实占用大量内存。事实上,我不明白你为什么首先使用 while 循环。
-
我用它来洗牌,但现在我想起来了,我可能会在不改变任何用户界面的情况下运行它,然后将所有的瓷砖交换到它们计算出的位置,会这样吗?超载吗?
-
什么是操纵computerMoves?
-
computerMoves 是我实现求解按钮时自动生成(无需人工交互)的所有移动的计数记录