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

2020/8/26

Making an actor follow a set route instead of transporting to final location

adrian.trimarchi adrian.trimarchi

2020/8/26

#
Hi all, I'm very new to this and I have a workshop using the ladybug world. The assignment calls for the ladybug to start at a set location, walk to and eat two leaves then end up at the final location. I can achieve this by using this code
    public void act()
    {  
        move();
        move();
        move();
        turnLeft();
        move(); 
        move();
        move();
        eatLeaf();
        turnLeft();
        turnLeft();
        turnLeft();
        move();
        move();
        turnLeft();
        move();
        move();
        eatLeaf();
        turnLeft();
        turnLeft();
        turnLeft();
        move();
        turnLeft();
        move();

      
    }
    
when I click 'act' the ladybug moves from the start position to the final position immediately. I'd like to see the ladybug walk the steps one-by-one so when I press play, it will walk instead of transport. How would I change my public void act() method to reflect this? Thanks, Adrian
danpost danpost

2020/8/26

#
adrian.trimarchi wrote...
when I click 'act' the ladybug moves from the start position to the final position immediately. I'd like to see the ladybug walk the steps one-by-one so when I press play, it will walk instead of transport. How would I change my public void act() method to reflect this?
The act method is for what you want the actor to do between each frame of the animation. You will need to employ some if statements to determine what action should be done at any point in the animation.
adrian.trimarchi adrian.trimarchi

2020/8/27

#
Thanks for the reply danpost. I know this isn't correct as I keep getting a syntax error but would it be something like this;
 {
        if (getLocation(X=1, Y=7))
        then setLocation(getX() + 1, getY());
        
    }
or
 if (getLocation(X=1, Y=7))
        then (move())
Then continue that for every movement?
danpost danpost

2020/8/27

#
Well ... there is no getLocation method in the Actor class. Otherwise, you would be on the right track. Helpful hints: (1) you can combine conditions within a single if conditional using conditional operators (middle of page) ; (2) you could get the existence of leaves in the world or even get a count of how many there are; furthermore you can get references to them and get their location coordinates; You may not be able to make use of (2) as I do not know what methods you may be restricted to (I am thinking your Ladybug class consists of two classes extending from Actor -- one provided with methods you can use and the one in which you add code into the act method). You may even have some extra fields that I am not aware of in that intermediate class.
adrian.trimarchi wrote...
Then continue that for every movement?
You could use one if statement for each line of code; however, you should try to find ways to reduce the amount of code that would produce.
adrian.trimarchi adrian.trimarchi

2020/8/28

#
I had a play around for a while but couldn't quite grasp it yet. I still don't know enough to understand it. I'll try it again over the next few weeks. Thanks again.
danpost danpost

2020/8/28

#
adrian.trimarchi wrote...
I had a play around for a while but couldn't quite grasp it yet. I still don't know enough to understand it. I'll try it again over the next few weeks.
As a starter, you could use:
if (getX() < 3) move();
else if ...
Hopefully, you can make use of getRotation (that is, the direction the actor is facing) as part of the condition for turning.
RcCookie RcCookie

2020/8/28

#
If you really only want to see the movement going on, you can put "Greenfoot.delay(10);" after each statement and adjust the number inside to your favour. However, this is a pretty lazy and bad way to achive this, and you should ONLY use it for stuff that does not run while the simulation, because this will pause everything and will stop other things from doing stuff, too.
RcCookie RcCookie

2020/8/28

#
A more complex but later on more conveniant way of doing this would be like this:
//globally, creates an array with numbers
//each number stands for a certain kind of option, in my example 0 stands for up, 1 for left, 2 for down and 3 for right
int[] steps = {0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3};

//The number of step you are currently at
int index = 0;

public void act(){
    //check weather there is another step availably
    if(index < steps.length){

        //do a specific thing depending on the number of the current step
        switch(steps[index]){
            case 0://up
                setLocation(getX() - 1, getY());
                break;
            case 1://left
                setLocation(getX(), getY() - 1);
                break;
            case 2://down
                setLocation(getX() + 1, getY());
                break;
            case 3://right
                setLocation(getX(), getY() + 1);
                break;
        }
    }

    //count one step up
    index++;
}
This way of doing it will move the object one tile per frame. You can easily add another option by adding a "case" the same way that they are right now and add the number you chose into the array at the position you want.
danpost danpost

2020/8/28

#
RcCookie wrote...
A more complex ...
I do not think this is basic enough for a beginner (using arrays and switch statements).
If you really only want to see the movement going on, you can put "Greenfoot.delay(10);" after each statement
This will also prevent the user from stopping the scenario before the ladybug reaches its final location That is, if using delay and Pause is clicked on, the act method will complete, skipping the delays (which is probably an unwanted behavior).
RcCookie RcCookie

2020/8/28

#
danpost wrote...
I do not think this is basic enough for a beginner (using arrays and switch statements).
I know it's not the easiest thing. Arrays are pretty basic though. Also, I think it is better to show straight away that you should not hard-code stuff like this.
danpost danpost

2020/8/28

#
RcCookie wrote...
I know it's not the easiest thing. Arrays are pretty basic though.
Its like pushing trig on someone who barely knows geometry; or calculus on someone who barely knows algebra. I am just saying -- take it easy, man.
Also, I think it is better to show straight away that you should not hard-code stuff like this.
The code you have given IS a hard-coding; just in a more advanced style.
RcCookie RcCookie

2020/8/28

#
danpost wrote...
The code you have given IS a hard-coding; just in a more advanced style.
Well...
You need to login to post a reply.