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

2020/5/16

Act out loop over multiple act 'frames'?

whattheheck whattheheck

2020/5/16

#
I have the code:
public void act() {
        //on first act, get blocklist, size, and initialize positionZ int
        if (size <= 0) {
            list = ((SortingVisualization)getWorld()).getBlockList();
            size = list.size();
        } else if (size > 0) {
            Block temp;
            for (int i=0; i<size; i++) {
                for (int j=i; j>0; j--) {
                    if (((Block)list.get(j)).getValue() < ((Block)list.get(j-1)).getValue()) {
                        temp = (Block)list.get(j);
                        list.set(j, (Block)list.get(j-1));
                        list.set(j-1, temp);
                        updateList();
                    }
                }
            }
        }
    }
This code is for an insertion sort algorithm, which I want to visualize. I want to use my updateList() method to update my list of blocks to show what happens each step. Each step would be after one part of the nested for loop. Since this algorithm is under act, it occurs in one act step. Is it possible to change my list using updateList() every step of my sorting algorithm?
danpost danpost

2020/5/16

#
You can use:
Greenfoot.delay(20);
after invoking updateList.
You need to login to post a reply.