Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Cocos2d中完成所有操作后才执行函数_Java_Android_Cocos2d Android - Fatal编程技术网

Java 如何在Cocos2d中完成所有操作后才执行函数

Java 如何在Cocos2d中完成所有操作后才执行函数,java,android,cocos2d-android,Java,Android,Cocos2d Android,你们能告诉我,只有在函数slideTiles(字符串)中的所有节点操作完成后,我才能执行函数updateTiles()吗?我尝试添加一些while循环,但它冻结了整个程序。在一个接一个地运行函数之后,动画就不会出现 public boolean ccTouchesEnded(MotionEvent event) { CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.

你们能告诉我,只有在函数slideTiles(字符串)中的所有节点操作完成后,我才能执行函数updateTiles()吗?我尝试添加一些while循环,但它冻结了整个程序。在一个接一个地运行函数之后,动画就不会出现

public boolean ccTouchesEnded(MotionEvent event)
{
CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY()));
x2=location.x; y2=location.y;

 float diffY = y1-y2;
 float diffX = x1-x2;

 if (Math.abs(diffX)>Math.abs(diffY)&&diffX<0)
     move="right";
 else if (Math.abs(diffX)>Math.abs(diffY)&&diffX>=0)
     move="left";
 else if (Math.abs(diffX)<=Math.abs(diffY)&&diffY<0)
     move="up";
 else if (Math.abs(diffX)<=Math.abs(diffY)&&diffY>=0)
     move="down";

 int row=(int)(y1/TILE_SQUARE_SIZE);
 int column=(int)(x1/TILE_SQUARE_SIZE);

 slideTiles(move);

 //wait for animations to finish
 int sum=0;
 boolean actionDone=false;
 while (!actionDone)
 {
     for (CCNode c : tilesNode.getChildren())
     {
         sum+=c.numberOfRunningActions();
     }

     //String s=sum+"";
     //statusLabel.setString(s);

     if (sum==0){
         actionDone=true;
         break;
     }

     sum=0;
 }

 updateTiles();




 return true;
 }

一般来说,您不应该使用while循环在UI线程中等待,这将导致您看到的大问题

您应该使用
线程
来执行此等待作业

new Thread(new Runnable() {
        @Override
        public void run() {
             while(true){
                 //here you wait forever doesn't matter the UI thread.
                 if(everythingDone) {//here to add your condition
                      new Handler().post(new Runable() {
                          @Override
                          public void run() {
                               //Here you can do what you want and will be done in UI Thread
                          }
                      });
                 }
             }
        }
}).start();
 public boolean ccTouchesEnded(MotionEvent event)
 {   

 ...
 ...

 slideTiles(move);

 float time = 0.25f+0.05f; //0.25f is time of moveTo action for each tile
 CCDelayTime delay = CCDelayTime.action(time);
 CCCallFunc cA = CCCallFunc.action(this, "updateTiles");
 CCSequence se = CCSequence.actions(delay, cA);
 runAction(se);

 return true;

 }
new Thread(new Runnable() {
        @Override
        public void run() {
             while(true){
                 //here you wait forever doesn't matter the UI thread.
                 if(everythingDone) {//here to add your condition
                      new Handler().post(new Runable() {
                          @Override
                          public void run() {
                               //Here you can do what you want and will be done in UI Thread
                          }
                      });
                 }
             }
        }
}).start();