2013-03-04 39 views
0

我有一個程序用於播放歌曲並顯示圖像。目前,圖像存儲在文件系統中,並在將其從左側或右側滑入屏幕之前加載到內存中。由於圖像相當大,這需要時間並導致用戶界面延遲。我只能在設備上有限的視頻內存中存儲一​​些圖像。我想要做的就是創建一個數組,其中遠離屏幕顯示的圖像將從內存中卸載。但是,我想將數組視爲一個圓 - 當您點擊零時,您將轉到數組的另一端而不是停止。將一個數組當作一個圓形對待

[unloaded,unloaded,image,image,image (on screen),image,image image,unloaded,unloaded] 

如果我在上面的數組中間,我總是可以刪除-3和+3元素。如何將邏輯處理爲一個循環?我很難想象一個優雅的if/then結構來處理這個問題。如果它不被視爲一個循環,代碼會是這個樣子,如果移動到右:

if msg=event.right_button_pressed then 
    currentindex=currentindex+1 
    lowindex=currentindex-3 
    highindex=currentindex+3 
    array[lowindex]=invalid 
    array[highindex]=loadimagefromdisk() 
    screen.drawobject(array[currentindex]) 
end if 

有人建議可以簡單的方式來處理包裹裝卸指針數組時的兩端他們超出了數組的範圍?

if currentindex > array.count() -1 then currentindex=0 
if currentindex < 0 then currentindex=array.count() - 1 
+0

我:通過相同的環繞功能,可運行三個索引指針會使用兩個堆棧(一個用於上一個,另一個用於下一個),因爲我很懶 - 它在概念上簡化了問題,並且只需定義幾個操作就可以協調一切:如果需要,可以使用next - > prev,把當前 - >上一頁等 – 2013-03-04 06:17:47

+0

你能定義你的意思嗎?堆棧,也許作爲回答問題的一部分?你的意思是兩個數組?或者你的意思是某種LIFO結構? – alphablender 2013-03-04 06:20:06

+0

您可以使用鏈表嗎? – Kyle 2013-03-04 06:21:19

回答

1

好,感謝從評論者靈感,這裏是我的解決方案:

CURRENTINDEX被包裹

playindex = wrap(playindex,playlist.count() - 1) 
highindex = wrap(highindex,playlist.count() - 1) 
lowindex = wrap(lowindex,playlist.count() - 1) 

function wrap(p as integer, count as integer) as integer 
    if p > count then p = 0 
    if p < 0 then p = count 
    return p 
end function