简体   繁体   中英

Android:About AndEngine updateThread and uiThread

I recently explore on AndEngine . I write a simple test demo, in which I implement IOnSceneTouchListener and the scene registers a TimerHandler to change one ChangableText . And I set the engine runOnUpdateThread true .

So the problem is : when I touched the scene a while, the activity paused and crashed. And the Logcat showed the same text as before: "org.anddev.andengine.util.pool.PoolUpdateHandler$1 was exhausted, with 1 item not yet recycled. Allocated 1 more."

If anyone can solve my problem, so thankful I will be!

PS: my code

    public class TestActivity extends BaseGameActivity implements IOnSceneTouchListener, IOnMenuItemClickListener {
    ...
    ...
    private TimerHandler mTimeUpdateHandler = new TimerHandler(1.f, true, new ITimerCallback() {
            @Override
            public void onTimePassed(TimerHandler arg0) {
                runOnUpdateThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mElapsedText != null && mAttempts > 0) {
                            mElapsedText.setText("Time: "
                                    + (ParticlyActivity.this.mEngine.getSecondsElapsedTotal() - mCurrentTotalSeconds),
                                    false);
                        }
                    }
                });
            }
        });
    ...
    ...

    // @Override
        public Engine onLoadEngine() {
            this.mCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
            final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE,
                    new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera).setNeedsSound(true);
            engineOptions.getTouchOptions().setRunOnUpdateThread(true);
            this.mEngine = new Engine(engineOptions);
            return this.mEngine;
        }


    public Scene onLoadScene(){
    ...
                // Text
                mElapsedText = new ChangeableText(20, 12, this.mFont, "Time:00.00");
                mScene.getFirstChild().attachChild(mElapsedText);
                mScene.registerUpdateHandler(mTimeUpdateHandler);

    ...
    }

@Override
    public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
            if ((pSceneTouchEvent.isActionMove() || pSceneTouchEvent.isActionDown()) && mAttempts < MaxBullets) {
                double angle = 0;
                if ((pSceneTouchEvent.getX() - StartX) == 0) {
                    angle = 90;
                } else {
                    angle = Math
                            .abs(Math.atan((StartY - pSceneTouchEvent.getY()) / (StartX - pSceneTouchEvent.getX())) / 3.14 * 180);
                }
                if (angle > 90) {
                    angle = 90;
                } else if (angle < 0) {
                    angle = 0;
                }
                mGun.setRotation((float) -angle);
                mGun.setStrength(pSceneTouchEvent.getX());
            } else if (pSceneTouchEvent.isActionUp() && mAttempts < MaxBullets) {
...
            }
        }
        return true;
    }

    }

TimerHandler:

private TimerHandler mTimeUpdateHandler = new TimerHandler(1.f, true, new ITimerCallback() {
    @Override
    public void onTimePassed(TimerHandler arg0) {
        runOnUpdateThread(new Runnable() {
            @Override
            public void run() {
                if (mElapsedText != null && mAttempts > 0) {
                    mElapsedText.setText("Time: " + (ParticlyActivity.this.mEngine.getSecondsElapsedTotal() - mCurrentTotalSeconds), false);
                }
            }
        });
    }
});

UIThread是负责绘制的人,如果您需要更改Text值,则每帧执行UpdateThread,在UpdateThread上执行此操作[一般而言,请远离UIThread]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM