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

2014/6/14

how to delay a specific sprite/actor?

TheWelshGamer TheWelshGamer

2014/6/14

#
the title is quite self explanatory, but i need help with delaying the actions of a selected sprite without it affecting the other sprites. thanks.
danpost danpost

2014/6/14

#
Add an instance int field to the class that creates the objects you wish to delay and add the following methods below to the class:
// add field
private int delayCount;
// methods to add
public boolean paused()
{
    return delayCount > 0;
}

public void setDelay(int actCount)
{
    delayCount = actCount;
}
Then place the following code at the very beginning of the act method of the class:
if (paused)
{
    delayCount--;
    return;
}
To pause the actor, just call the 'setDelay' method giving the number of act cycles to skip as the argument.
TheWelshGamer TheWelshGamer

2014/6/14

#
thanks for the help mate, but just need help with getting the last paragraph working (i'm quite new to this and most of what you say went strait over my head :P
danpost danpost

2014/6/14

#
To have your actor pause, use:
setDelay( /* number of cycles to skip */ );
danpost danpost

2014/6/14

#
Maybe you should look into the Java tutorials; especially the trail concerning Classes and Objects.
TheWelshGamer TheWelshGamer

2014/6/15

#
ok thanks for the help
CooliMC CooliMC

2014/6/15

#
@danpost it is also possible if he uses a timer or ??
danpost danpost

2014/6/15

#
@CooliMC, yes; but (1) more fields are needed (2) more things need imported (3) more complexity is introduced. Also, by using time as the governing factor in the delay instead of act methods, changing the speed of the scenario will alter the play; where when using act cycles, changing the speed does not alter the play. That is, altering the speed of the scenario will make the act methods of other actors execute varying amount of times when using time as the governing factor in the delay. Hope that makes sense.
TheWelshGamer TheWelshGamer

2014/6/16

#
public class Collectable extends Actor
{
    /**
     * Act - do whatever the Collectable wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (setDelay)
        {
            delayCount--;
            return;
        }
        
        
        int RanNum = Greenfoot.getRandomNumber(50);
        // This tells the sprite to turn at diffrent angles between 90% and -90% (right and left)
        
        if(RanNum == 50)
        {
            turn(90);
        }
        else if(RanNum >= 45 && RanNum < 50)
        {
            turn(72);
        }
        else if(RanNum >= 40 && RanNum < 45)
        {
            turn(54);
        }
        else if(RanNum >= 35 && RanNum < 40)
        {
            turn(36);
        }
        else if(RanNum >= 30 && RanNum < 35)
        {
            turn(18);
        }
        else if(RanNum >= 25 && RanNum < 30)
        {
            turn(0);
        }
        else if(RanNum >= 20 && RanNum < 25)
        {
            turn(-18);
        }
        else if(RanNum >= 15 && RanNum < 20)
        {
            turn(-36);
        }
        else if(RanNum >= 10 && RanNum < 15)
        {
            turn(-54);
        }
        else if(RanNum >= 5 && RanNum < 10)
        {
            turn(-72);
        }
        else if(RanNum >= 0 && RanNum < 5)
        {
            turn(-90);
        }
        move(4);
        
        
    }   
    private int delayCount;
    public boolean paused()
    {
        return delayCount > 0;
    }
        
    public void setDelay(int actCount)
    {
        delayCount = actCount;
    }
}
this is what i've got so far and still having trouble getting it to work.
danpost danpost

2014/6/16

#
Change line 9 to
if (delayCount > 0)
// OR
if (paused())
TheWelshGamer TheWelshGamer

2014/6/18

#
ok thanks
You need to login to post a reply.