2014-12-24 2 views
0

function slideTiles (String s)의 모든 노드 작업이 완료된 후에 만 ​​updateTiles() 함수를 어떻게 실행할 수 있습니까? while 루프를 추가하려고했지만 전체 프로그램을 정지시킵니다. 기능을 하나씩 실행 한 후에는 애니메이션이 나타나지 않습니다.Cocos2d에서 모든 작업이 완료된 후에 만 ​​함수를 실행하는 방법

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; 
} 

//////

public void slideTiles(String move) 
{ 
// Increment the moves label and animate the tile 
CCBitmapFontAtlas moveslabel = (CCBitmapFontAtlas) getChildByTag(MOVES_LABEL_TAG); 
moves++; 

    moveslabel.runAction(CCSequence.actions(
      //CCDelayTime.action(0.25f), 
      CCScaleTo.action(0.2f, 6/5f), 
      //CCDelayTime.action(0.25f), 
      CCScaleTo.action(0.2f, 5/6f) 
      )); 
    moveslabel.setString("Moves:\n " + CCFormatter.format("%03d", moves)); 

    if (move.equals("up")) 
    { 
     for (int start=NUM_COLUMNS; start<2*NUM_COLUMNS; start++) 
     for (int y=start; y<NUM_COLUMNS*NUM_ROWS; y+=NUM_COLUMNS) 
     { 
      if (tileNumbers[y]!=EMPTY) 
      { 
       for (int f=start-NUM_COLUMNS; f<y; f+=NUM_COLUMNS) 
       { 
        if (tileNumbers[f]==EMPTY) 
        { 
         //y->f 
         CGPoint moveTo = tilesNode.getChildByTag(f).getPosition(); 
         CCMoveTo movetile = CCMoveTo.action(0.25f, moveTo); 
         CCSequence movetileSeq = CCSequence.actions(movetile);//CCCallFunc.action(this,"stopAction")); 
         tilesNode.getChildByTag(y).runAction(movetileSeq); 

         tileNumbers[f]=tileNumbers[y]; 
         tileNumbers[y]=EMPTY; 

         break; 
        } 
       } 
      } 
     } 
    } 

편집 : 이러한 유형의 솔루션은 정확합니까? 놀랍게도 잘 작동합니다.

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; 

} 

답변

0

일반적으로 UI 스레드에서 루프 대기 중에는 사용하지 말아야합니다. 큰 문제가 발생할 것입니다.

이 대기 작업을 수행하려면 Thread을 사용해야합니다.

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(); 
관련 문제