我最近在上探索AndEngine。我寫了一個簡單的測試演示,其中我執行IOnSceneTouchListener
,場景註冊了一個TimerHandler
以更改一個ChangableText
。 而我設置引擎runOnUpdateThread
true。Android:關於AndEngine updateThread和uiThread
所以問題是:當我碰到場景一會兒,活動暫停並崩潰。 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;
}
}
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和UpdateThread有什麼不同,任何人都知道它plz te我會。非常感謝! –
您需要從LogCat發佈更多輸出,以便我們知道錯誤是什麼。 – JohnEye
@JohnEye謝謝你的回覆。我修改了代碼「engineOptions.getTouchOptions()。setRunOnUpdateThread(true);」之前得到「 exausted with 1 not recycled,alloc 4 more ......」到「engineOptions.getTouchOptions()。setRunOnUpdateThread(false);」 –