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

标签 java android cocos2d-android

你们能告诉我如何仅在函数 slipTiles(String s) 中的所有节点操作完成后才执行函数 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;
 }

//////

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;

 }

最佳答案

一般来说,你不应该使用 while 循环在 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();

关于java - Cocos2d中如何仅在所有操作完成后才执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27642798/

相关文章:

java.lang.IllegalStateException : Circular dependencies cannot exist in RelativeLayout : XML

java - 如何在 Applozic 中通过用户 ID 获取联系人的显示名称?

android - 在哪里可以找到使用 Cocos2D-x 开始编程的好教程?

android - Android 启动黑屏,除非使用横向

cocos2d-android - Cocos2d 中的自定义字体

java - 无法停止 retrofit 发送

java - 需要帮助来理解此复选框代码在 Android 中的工作原理

java - "No persistence unit with name ' 产品 ' found"

java - 使用 (XStream) 将对象序列化/反序列化为 XML

android - 以编程方式单击时更改布局背景