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

2018/3/18

How to create a method in java that works like the act()-method in Greenfoot

bravdwal bravdwal

2018/3/18

#
I was wondering how the act()-method is coded in Java. I know that Greenfoot is an IDE using Java, so everything that already is programmed in Greenfoot is written for you in Java. I can imagine that in pseudocode it might be something like:
if (run-button is clicked) {
    while(pause-button is not clicked) {
        [...] //all the code that you'd write in the act()-method
    }
}
but I'm not quite sure... Is there someone who knows the Java code behind the act()-method?
Super_Hippo Super_Hippo

2018/3/18

#
Basically while it runs, execute the act method of the active world and then the act method of all actors in the active world based on the set act order. Here is some of the relevant code:
    private void runContent()
    {
        while (!abort) {
            try {
                maybePause();
                                
                if (worldHandler.hasWorld()) {
                    runOneLoop(worldHandler.getWorld());
                }

                delay();
            }
            //...
        }

        // The simulations has been aborted. But, we might still have to notify the world.
        synchronized (this) {
            if(isRunning) {
                World world = worldHandler.getWorld();
                if (world != null) {
                    worldStopped(world);
                }
                isRunning = false;
            } 
        }
    }
    private void runOneLoop(World world)
    {
        fireSimulationEvent(newActRoundEvent);
        
        // We don't want to be interrupted in the middle of an act-loop
        // so we remember the first interrupted exception and throw it
        // when all the actors have acted.
        ActInterruptedException interruptedException = null;
        
        List<? extends Actor> objects = null;

        // We need to sync to avoid ConcurrentModificationException
        try {
            ReentrantReadWriteLock lock = worldHandler.getWorldLock();
            lock.writeLock().lockInterruptibly();
            try {
                try {
                    actWorld(world);
                    if (world != worldHandler.getWorld()) {
                        return; // New world was set
                    }
                }
                catch (ActInterruptedException e) {
                    interruptedException = e;
                }
                // We need to make a copy so that the original collection can be
                // modified by the actors' act() methods.
                objects = new ArrayList<Actor>(WorldVisitor.getObjectsListInActOrder(world));
                for (Actor actor : objects) {
                    if (!enabled) {
                        return;
                    }
                    if (ActorVisitor.getWorld(actor) != null) {
                        try {
                            actActor(actor);
                            if (world != worldHandler.getWorld()) {
                                return; // New world was set
                            }
                        }
                        catch (ActInterruptedException e) {
                            if (interruptedException == null) {
                                interruptedException = e;
                            }
                        }
                    }

                }
                
                worldHandler.getKeyboardManager().clearLatchedKeys();
            }
            finally {
                lock.writeLock().unlock();
            }
        }
        //...
    }
    private static void actActor(Actor actor)
    {
        actor.act();
    }
    
    private static void actWorld(World world)
    {
        world.act();
    }
Super_Hippo Super_Hippo

2018/3/18

#
You can download the source code of Greenfoot here: https://www.greenfoot.org/site/download_source
bravdwal bravdwal

2018/3/18

#
Okay, thank you Super_Hippo, I'll have a look at it!
You need to login to post a reply.