如何顺序执行android函数?

时间:2014-02-24 07:35:41

标签: android

我使用A *算法在AI上制作了一个关于8Puzzle的游戏。 一切正常,但有一个问题,有一些功能是并行执行的。 我想要的是在另一个功能完成后执行该功能。

这是代码:

if(AItype.equals("A*"))
{
    DisableButton();
    DisableClickImageView();
    AStarSolver as = new AStarSolver();
    as.solvePuzzle(arr, GOAL); //Solve the puzzle
    displayResult(as.solutionPath); //display animation
    as = null;

    Toast.makeText(getApplicationContext(), "Finished", Toast.LENGTH_LONG).show();
    copySTARTtoArray();
    setImageResource();
    EnableButton();             
}

我希望文字“已完成”在功能displayResult()完成后显示,但文字“已完成” 与函数displayResult()同时显示。

如何解决这个问题?

编辑:

以下是displayResult();

的代码
public void displayResult(final Stack<Node> solutionPath)
{
    Handler handler = new Handler(); 
    for (int i = 0; i < solutionPath.size(); i++)
    {
        handler.postDelayed(new Runnable() { 
            public void run() { 
                    Node solNode = solutionPath.pop();
                    //solNode.NodeState.
                    tile00.setImageResource(solNode.getImageResourceBasedNodeState(0));
                    tile01.setImageResource(solNode.getImageResourceBasedNodeState(1));
                    tile02.setImageResource(solNode.getImageResourceBasedNodeState(2));
                    tile10.setImageResource(solNode.getImageResourceBasedNodeState(3));
                    tile11.setImageResource(solNode.getImageResourceBasedNodeState(4));
                        tile12.setImageResource(solNode.getImageResourceBasedNodeState(5));
                    tile20.setImageResource(solNode.getImageResourceBasedNodeState(6));
                        tile21.setImageResource(solNode.getImageResourceBasedNodeState(7));
                    tile22.setImageResource(solNode.getImageResourceBasedNodeState(8));  
            } 
        }, 800 * (i + 1));  

    }   
}

显示动画结果(例如:tile0到tile1)

3 个答案:

答案 0 :(得分:1)

试试这个。

public void displayResult(final Stack<Node> solutionPath)
{
    Handler handler = new Handler(); 
    for (int i = 0; i < solutionPath.size(); i++)
    {
        handler.postDelayed(new Runnable() { 
            public void run() { 
                    Node solNode = solutionPath.pop();
                    //solNode.NodeState.
                    tile00.setImageResource(solNode.getImageResourceBasedNodeState(0));
                    .
                    .
                    .
                    tile22.setImageResource(solNode.getImageResourceBasedNodeState(8));  
            } 
        }, 800 * (i + 1));  

    }  
// Put following code
 handler.postDelayed(
    new Runnable() { 
      public void run() { 
       Toast.makeText(getApplicationContext(), "Finished", Toast.LENGTH_LONG).show(); 
     }
     },800*(solutionPath.size()+1)
    );

}

答案 1 :(得分:0)

您的代码应如下所示。

solvePuzzle(...){
    // solved #1
    displayResult(...){
    }

    // solved #2
    displayResult(...){
    }
}

或使用线程。


as.solvePuzzle(arr, GOAL); //Solve the puzzle
displayResult(as.solutionPath); //display animation

表示solvePuzzle全部完成,然后调用displayResult。

答案 2 :(得分:0)

function(){
//code of this function

 function1();
}
function1(){
//code of this function

 function2();
}


function2(){
}
相关问题