Andengine GLES2 - 后台线程阻止TimerUpdate

时间:2014-05-01 23:14:42

标签: java android multithreading andengine

我有fadeOut metod到fadeOut场景,在背景中加载另一个并且淡入它。对于fadeOut当前场景,我使用黑色矩形和TimerHandler来实现淡入淡出效果。要加载第二个场景,我使用另一个线程。

我的代码:

public void fadeOut(final BasicScene scene){

    final Thread backgroundLoad = new Thread(new Runnable() {
        @Override
        public void run() {
            if(!scene.isLoaded()) {
                scene.load();
            }
            threadSync("load",scene);
        }
    });

    TimerHandler backgroudLoadingDelay = new TimerHandler(0.1f,false,new ITimerCallback() {
        @Override
        public void onTimePassed(TimerHandler pTimerHandler) {
            mEngine.unregisterUpdateHandler(pTimerHandler);

        }
    });

    blackRectangle = new Rectangle(0,0,CAMERA_WIDTH,CAMERA_HEIGHT,this.getVertexBufferObjectManager());
    blackRectangle.setAnchorCenter(0, 0);
    blackRectangle.setColor(0, 0, 0, 0f);
    mEngine.getScene().attachChild(blackRectangle);
    PauseableTimerHandler fadeoutHandler = new PauseableTimerHandler(0.01f, new PauseableTimerHandler.ITimerCallback() {
        @Override
        public void onTick(PauseableTimerHandler pTimerHandle) {
            blackRectangle.setAlpha(blackRectangle.getAlpha() + 0.025f);
            if (blackRectangle.getAlpha() >= 1){
                blackRectangle.setAlpha(1f);
                mEngine.getScene().unregisterUpdateHandler(pTimerHandle);
                threadSync("transition",scene);
            }
        }
    });
    mEngine.getScene().registerUpdateHandler(fadeoutHandler);
    backgroundLoad.start();
}

private void threadSync(String thread, BasicScene scene) {
    if (thread.equals("load")){
        loadingDone = true;
    } else if (thread.equals("transition")){
        transitionDone = true;
    }
    if(loadingDone && transitionDone){
        loadingDone = transitionDone = false;
        mEngine.setScene(scene.showScene());
        scene.fadeIn();
        runOnUpdateThread(new Runnable() {
            @Override
            public void run() {
                blackRectangle.detachSelf();
            }
        });
    }

}

但是在我的两个设备上(SE wt19i和欲望500)我的褪色效果都很明显。当我没有加载任何东西时它们不是所以我想这是因为我开始另一个线程?我是对还是有另一种方式?

0 个答案:

没有答案