我的程序被设计成滑动益智游戏。我目前正在研究随机播放按钮,该按钮将使用随机数将瓷砖移动一定时间,以便玩家可以解决棋盘。
目前,当我按下随机播放按钮时,我遇到了一条错误消息:“无法执行 Activity 的方法”。当它在 while 循环之外时,我可以继续按下按钮,但我必须在每次点击之间留出一定的时间。我在考虑如何使程序重载...但我不确定如何减少负载...
public void shuffleTiles(View v){ //Shuffles the tiles using random numbers
int tileToMove;
while (computerMoves < 5){
tileToMove = randomNumbers.nextInt(12);
moveTile(tileToMove, false);
}
该程序的主要症结在于此方法,该方法交换图像以显示图 block 已移动:
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);
}
}
最佳答案
如果您有其他 Activity 使您的应用停滞不前,您可以确保使用 activity lifecycle 关闭它们。 .这帮助我解决了一个特定项目中的一些卡住问题。
关于java - 重载Android应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10657567/