时间处理程序内的时间处理程序

时间:2015-02-12 12:54:29

标签: andengine

 TimerHandler timer =  new TimerHandler(.7f, true,  new ITimerCallback() {
                    private int grab_shell=0;
                    private int loop_count=0;

                    public void onTimePassed(TimerHandler pTimerHandler) {

                        if ((shell[0].getCurrentTileIndex()>0)&&(loop_count==0))
                        {
                            grab_shell=shell[0].getCurrentTileIndex();
                            shell[0].setCurrentTileIndex(0);
                        }
                        if(grab_shell>0)
                        {   
                            current_house++;
                            if(current_house==15)
                            {
                                current_house=0;
                            }
                            if((grab_shell==1)&&(current_house!=7)&&(shell[current_house].getCurrentTileIndex()!=0))
                            {

                                    grab_shell=2+shell[current_house].getCurrentTileIndex();
                                    shell[current_house].setCurrentTileIndex(0);

                                }
                            }

                            else if ((grab_shell==1)&&(current_house!=7)&&(shell[current_house].getCurrentTileIndex()==0))
                            {
                                shell[7].setCurrentTileIndex(shell[current_house].getCurrentTileIndex()+1);
                                shell[current_house].setCurrentTileIndex(0);
                            }
                            shell[current_house].setCurrentTileIndex(shell[current_house].getCurrentTileIndex()+1);
                            grab_shell--;
                        }

                        if(grab_shell==0)
                        {
                        unregisterUpdateHandler(pTimerHandler);
                        }
                        loop_count++;
                        }

我的问题是如何在此代码中创建延迟

    if((grab_shell==1)&&(current_house!=7)&&(shell[current_house].getCurrentTileIndex()!=0))
       {        grab_shell=2+shell[current_house].getCurrentTileIndex();
                                    shell[current_house].setCurrentTileIndex(0);
                            }

此代码位于timerhandler中。我怎样才能做到这一点?请给我看。 这部分代码与其他条件相比有两个动作,这就是为什么我必须使这部分稍微延迟1.4秒

1 个答案:

答案 0 :(得分:0)

可以嵌套定时器处理程序。这意味着你可以在timerhandler中有一个计时器处理程序。您唯一需要确保的是取消注册处理程序以避免不必要的行为。我还假设您希望使用您指出的if语句创建计时器处理程序。

TimerHandler timer =  new TimerHandler(.7f, true,  new ITimerCallback() {
                private int grab_shell=0;
                private int loop_count=0;

                public void onTimePassed(TimerHandler pTimerHandler) {

                    if ((shell[0].getCurrentTileIndex()>0)&&(loop_count==0))
                    {
                        grab_shell=shell[0].getCurrentTileIndex();
                        shell[0].setCurrentTileIndex(0);
                    }
                    if(grab_shell>0)
                    {   
                        current_house++;
                        if(current_house==15)
                        {
                            current_house=0;
                        }
                        if((grab_shell==1)&&(current_house!=7)&&(shell[current_house].getCurrentTileIndex()!=0))
                        {
                    //here
                    TimerHandler timer =  new TimerHandler(1.4f, true,  new ITimerCallback() {

                    @Override
                    public void onTimePassed(TimerHandler pTimerHandler) {
                    grab_shell=2+shell[current_house].getCurrentTileIndex();
                    shell[current_house].setCurrentTileIndex(0);
                   //unregister timer handler

                    }

                 });

                            }
                        }

                        else if ((grab_shell==1)&&(current_house!=7)&&(shell[current_house].getCurrentTileIndex()==0))
                        {
                            shell[7].setCurrentTileIndex(shell[current_house].getCurrentTileIndex()+1);
                            shell[current_house].setCurrentTileIndex(0);
                        }
                        shell[current_house].setCurrentTileIndex(shell[current_house].getCurrentTileIndex()+1);
                        grab_shell--;
                    }

                    if(grab_shell==0)
                    {
                    unregisterUpdateHandler(pTimerHandler);
                    }
                    loop_count++;
                    }