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

2022/5/27

hi

truck_hunt97 truck_hunt97

2022/5/27

#
Greetings, can you give me a code that when you touch an object, it slows down the enemy for a while, thanks
Spock47 Spock47

2022/5/28

#
Here is a source code that slows down all enemies by one movement point for 5 seconds when You collects a SlowDownObject. The SlowDownObject is consumed in the process.
public class SlowDownWorld extends World
{
    
    private int slowDownTimer = 0;

    public void slowDownEnemies() {
        slowDownTimer = 300; 
    }
    
    public boolean isSlowedDown() {
        return slowDownTimer > 0;
    }
    
    @Override
    public void act() {
        if (slowDownTimer > 0) {
            --slowDownTimer;
        }
    }

}
public class Enemy extends Actor
{
    @Override
    public void move(final int distance) { // positive value for distance is expected
        final SlowDownWorld world = (SlowDownWorld)getWorld();
        final int actualDistance
            = world != null && world.isSlowedDown() ? distance - 1
                                         /* else */ : distance;
        super.move(actualDistance);
    }
}
public class You extends Actor
{
    @Override
    public void act()
    {
        final Actor freezer = getOneIntersectingObject(SlowDownObject.class);
        final SlowDownWorld world = (SlowDownWorld)getWorld();
        if (freezer != null && world != null) {
            world.removeObject(freezer);
            world.slowDownEnemies();
        }
    }
}
You need to login to post a reply.