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

2019/4/8

Is there a command to stop only 1 class for a bit then make it start running again?

EthanWasHere_ EthanWasHere_

2019/4/8

#
Is there a command to stop only 1 class for a bit then make it start running again? any help would be appreciated!
Super_Hippo Super_Hippo

2019/4/8

#
You can set a timer and as long as the timer is not 0, do nothing after decreasing it.
private int doNothingTimer = 0;

public void act()
{
    if (doNothingTimer>0)
    {
        doNothingTimer--;
        return;
    }
    
    //rest of act method
}

public void setDoNothingTimer(int newValue)
{
    doNothingTimer = newValue;
}
//in actice world subclass

//let all objects of class ABC idle for newValue act cycles
public void stunABC(int newValue)
{
    for (ABC abc : getObjects(ABC.class) //use "getWorld().getObjects" if not placed in world subclass
    {
        abc.setDoNothingTimer(newValue);
    }
}
This only affects objects in the world while the method is called. So if the method is called, all objects of class ABC are "stunned" for a while. If a new ABC object is added to the world, it will not stun this one. If this is important, it could look like this:
private static boolean doNothing = false;

public void act()
{
    if (doNothing)
    {
        return;
    }
    
    //rest of act method
}

public static void setDoNothing(boolean newValue)
{
    doNothing = newValue;
}
//active world subclass
private int abcStunTimer = 0;
public void act()
{
    if (abcStunTimer>0)
    {
        if (--abcStunTimer==0) stunABC(false);
    }
}

//let all objects of class ABC idle for newValue act cycles
public void stunABC(int newValue)
{
    ABC.setDoNothing(newValue);
}
You need to login to post a reply.