This site requires JavaScript, please enable it in your browser!
Greenfoot back
Zweeg
Zweeg wrote ...

2018/12/14

How to scroll through an arrayList like a conveyer belt?

Zweeg Zweeg

2018/12/14

#
I want to scroll forwards and through an arrayList every time I press the ">" key. I can do this no problem but the problem is when I reach the end of the arrayList. When I reach the end, I want to go back to the front. This sounds confusing but really the concept is simple. For example, let's say we are displaying the arrayList slots 1,2,3,4 of an arrayList size 5. When I press the key ">" I want to display the slots 0,1,2,3. When I press the key ">" again, I want to display slots 1,2,3,4. Hopefully this example clears things up.
danpost danpost

2018/12/14

#
Zweeg wrote...
I want to scroll forwards and through an arrayList every time I press the ">" key. I can do this no problem but the problem is when I reach the end of the arrayList. When I reach the end, I want to go back to the front. This sounds confusing but really the concept is simple. For example, let's say we are displaying the arrayList slots 1,2,3,4 of an arrayList size 5. When I press the key ">" I want to display the slots 0,1,2,3. When I press the key ">" again, I want to display slots 1,2,3,4. Hopefully this example clears things up.
For one at a time, use:
if (">".equals(Greenfoot.getKey()) slot = (slot+1)%aList.size();
For your example of showing 4 of 5, use;
if (">".equals(Greenfoot.getKey()) slot = (slot+1)%(aList.size()-3);
The 3 is there because you are showing 3 more than 1 at a time.
You need to login to post a reply.