這是我第一次真正涉足Android編程,但我覺得我有更多的Java問題 - 對ScheduledThreadPoolExecutor如何工作的一些重大誤解(雖然我願意以表示這是AnimatedSprites的AndEngine問題)。實質上,我希望精靈在發生碰撞之前一動不動。精靈動畫,並在一秒鐘後,再次停止。用戶必須保持Flinging以「游泳」水中的精靈。我遇到的問題是動畫。如果您在1秒的時間範圍內有多個Fling,則動畫僅在停止前顯示一幀或兩幀,感覺像「停止動畫」任務正在堆疊。我會檢查並確保在我完成所有這些工作後Flings足夠長並且朝着正確的方向。當我第一次實例化ScheduledThreadPoolExecutor時,我提供的池大小爲1,這對我來說意味着它一次不能有超過一個任務在隊列中。最重要的是,在我調用.schedule()之前,我將.remove()任務,以確保沒有任何內容。我確信這是簡單的,只是我對如何正確使用這個問題有一些誤解。我將在這裏粘貼相關代碼部分:使用ScheduledThreadPoolExecutor重複安排AndEngine的任務
...
private ScheduledThreadPoolExecutor shed = new ScheduledThreadPoolExecutor(1);
private Runnable slowDown = new Runnable(){
public void run(){
if (eelSprite.isAnimationRunning())
eelSprite.stopAnimation(0);
};
};
...
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
shed.remove(slowDown);
shed.schedule(slowDown, 1000, TimeUnit.MILLISECONDS);
if (!(eelSprite.isAnimationRunning()))
eelSprite.animate(frame_duration, 0, 12, true);
return false;
}
哇,這麼多隻是一個簡單的誤解。我希望有一個「哦,你忘了......」這是一個很好的解釋,我很欣賞你投入的時間。看起來我有一些閱讀要做 - 執行者顯然不像我以爲他們做的那樣工作。 – NickGlowsinDark 2013-03-07 19:39:53