My program is being designed as a sliding puzzle game. I’m currently working on the shuffle button that will use random numbers to move tiles a certain amount of time so that the board can be solved by the player.
Currently I’m stuck with a error message : ‘Could not execute method of the activity’ caused when I press the shuffle button. When it was outside of the while loop i could keep pressing the button, but I had to leave a certain amount of time between each click. Im thinking im overloading the program some how… but im not sure how to reduce the load…
public void shuffleTiles(View v){ //Shuffles the tiles using random numbers
int tileToMove;
while (computerMoves < 5){
tileToMove = randomNumbers.nextInt(12);
moveTile(tileToMove, false);
}
The main crux of the program is worked around this method, which swaps images to show that the tiles have moved:
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);
}
}
If you have other activities that are bogging down your app you can make sure that you close them by using the activity lifecycle . This helped me solve some freezes in one particular project.