AndEngine:UI未在onAreaTouched调用中更新

时间:2012-09-14 13:31:54

标签: android andengine

我正在使用AndEngine,我有自定义单选按钮的代码:

    blackRadioNotSelected = new Sprite(0, 0, blackRadio, vertexBufferObjectManager) {
        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
            if (pSceneTouchEvent.isActionUp()) {
                activity.runOnUpdateThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG,"touch");
                        unregisterTouchArea(blackRadio);
                        detachChild(whiteRadio);
                        detachChild(blackRadio);
                        whiteRadio = whiteRadioNotSelected;
                        blackRadio = blackRadioSelected;
                        registerTouchArea(whiteRadio);
                        attachChild(whiteRadio);
                        attachChild(blackRadio);
                    }
                });


                isWhiteSelected = !isWhiteSelected;
            }
            return false;
        }
    };

当我触摸radiobutton时,我可以看到“触摸”消息,并且在第二次触摸后它没有显示 - 所以,unregisterTouchArea(blackRadio);被执行了。但UI仍然相同:whiteRadio看起来像选中,而blackRadio - 未选中。它可能是什么?

UPD:我尝试将此代码放入其他方法中,一切正常。这就是它现在的样子:

    blackRadioNotSelected = new Sprite(0, 0, blackRadio, vertexBufferObjectManager) {
        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
            if (pSceneTouchEvent.isActionUp()) {
                swapColor();
            }
            return false;
        }
    };

swapColor();

private void swapColor() {
    if (isWhiteSelected) {
        activity.runOnUpdateThread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG,"blackNotSelected");
                unregisterTouchArea(blackRadio);
                detachChild(whiteRadio);
                detachChild(blackRadio);
                whiteRadio = whiteRadioNotSelected;
                blackRadio = blackRadioSelected;
                registerTouchArea(whiteRadio);
                attachChild(whiteRadio);
                attachChild(blackRadio);
            }
        });
    } else {
        [...]
    }


    isWhiteSelected = !isWhiteSelected;
}

但我仍然不知道出了什么问题,因为之前的版本在其他项目中有效。有人可以向我解释一下吗?

1 个答案:

答案 0 :(得分:0)

我认为您尚未将精灵触摸区域注册到主场景中。 做到这一点。   。getEngine()getScene()registerTouchArea(blackRadioNotSelected);