将数组视为圆形

时间:2013-03-04 06:15:03

标签: arrays

我正在编写一个程序来播放歌曲并显示图像。目前,图像存储在文件系统上并在从左或右滑动到屏幕上之前加载到存储器中。这需要时间并且由于图像非常大而导致UI的延迟。我只能在设备的有限视频内存中存储一​​些图像。我想要做的是有一个数组,从屏幕上显示的距离超过几步的图像从内存中卸载。但是,我想将数组视为一个圆 - 当你达到零时,你会转到数组的另一端而不是停止。

[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

有人建议使用简单的方法来处理包装加载和卸载指向数组末端的指针超出数组范围的时候吗?

currentindex包含在:

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

1 个答案:

答案 0 :(得分:1)

好的,感谢评论员的灵感,这是我的解决方案:通过相同的环绕功能运行所有三个索引指针:

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
相关问题