gpt4 book ai didi

java - Android:关于AndEngine updateThread和uiThread

转载 作者:行者123 更新时间:2023-12-01 05:06:12 24 4
gpt4 key购买 nike

我最近探索了AndEngine。我编写了一个简单的测试演示,其中实现了 IOnSceneTouchListener,场景注册了一个 TimerHandler 来更改一个 ChangeableText。我设置了引擎 runOnUpdateThread true

所以问题是:当我触摸场景一段时间时, Activity 暂停并崩溃。 Logcat 显示与之前相同的文本:“org.anddev.andengine.util.pool.PoolUpdateHandler$1 已耗尽,其中 1 项尚未回收。又分配了 1 项。”强>

如果有人能解决我的问题,我将非常感激!

PS:我的代码

    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;
}

}

定时器处理程序:

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负责绘制,UpdateThread每一帧都会执行如果您需要更改文本值,请在 UpdateThread 上执行此操作[通常远离 UIThread]

关于java - Android:关于AndEngine updateThread和uiThread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12633675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com