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

2020/3/7

overriding methods and dropping markers???

Wigglewiggle99 Wigglewiggle99

2020/3/7

#
I need help dropping an actor subclass (marker) at the current location of my other subclass (turtle) if there is not a marker at the turtles current location. Here is a snippet of my method and I have no clue how to fix it
1
2
3
4
5
6
7
8
9
public void move()
    {       
        while (this.isClear(HERE))
        {
            Marker marker = new Marker();
            this.add(marker);
        }
        super.move(1);
    }
This is not running the first 3 lines, I know HERE is not in the appropriate location but I can't figure out how to indicate that it must drop a marker at the turtles current location.
danpost danpost

2020/3/7

#
Wigglewiggle99 wrote...
I need help dropping an actor subclass (marker) at the current location of my other subclass (turtle) if there is not a marker at the turtles current location. << Code Omitted >> This is not running the first 3 lines, I know HERE is not in the appropriate location but I can't figure out how to indicate that it must drop a marker at the turtles current location.
What do your isClear and add(Marker) methods look like? Is the code given in your turtle class? One thing that I notice is the loop. I am not sure that it should be there at all. It might need to be changed from while to if.
Wigglewiggle99 Wigglewiggle99

2020/3/7

#
I'd like to drop a marker to the location where my turtle object is currently at. Is getObjects(Marker.class).isEmpty the correct way to reference the current location of the turtle? Here's my updated code, feedback says that it cannot find symbol. I'm very new to coding so I'm sorry if I am not understanding.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    /**
     * Drops marker at current location if empty
     * then moves one block forward
     */
    public void move()
    {       
        if (this.getWorld().getObjects(Marker.class).isEmpty())
        {
            Marker marker = new Marker();
            getWorld().addObject(marker, getX(), getY());
        }
        super.move(1);
    }
}
danpost danpost

2020/3/8

#
Wigglewiggle99 wrote...
I'd like to drop a marker to the location where my turtle object is currently at.
So ... this actor (a turtle) is to leave a trail of markers behind it by filling in gaps where none are currently located.
Is getObjects(Marker.class).isEmpty the correct way to reference the current location of the turtle?
No. That will get a list of all markers in the world regardless of location. Use the following for line 7:
1
if ( ! isTouching(Marker.class) )
Wigglewiggle99 Wigglewiggle99

2020/3/8

#
Yes I have 7 turtle actors in the world. isTouching produces an error cannot find symbol. Will I be able to use getIntersectingObject like so instead
1
2
3
4
5
6
7
8
9
10
11
12
13
public void move()
    {  
        int x = getWorld().getWidth();
        int y = getWorld().getHeight();
        Actor bill= getOneIntersectingObject(Marker.class);
        if (bill== null);
        {
            Marker marker = new Marker();
            getWorld().add(marker, x, y);
            //getWorld().addObject(marker);
        }
        super.move(1);
    }
danpost danpost

2020/3/8

#
Wigglewiggle99 wrote...
Yes I have 7 turtle actors in the world. isTouching produces an error cannot find symbol. Will I be able to use getIntersectingObject like so instead << Code Omitted >>
Yes -- you could use that. I cannot believe, however, that you would get a "cannot find" on isTouching (unless you are using an old version of greenfoot -- and I mean OLD).
Wigglewiggle99 Wigglewiggle99

2020/3/8

#
I am running greenfoot sofia if that makes any difference. I have one more question if you'd be so kind to help. How would I go about overriding the move() method that takes an integer parameter? Do I use public void move(int) or is there special code to override?
danpost danpost

2020/3/8

#
Wigglewiggle99 wrote...
I am running greenfoot sofia if that makes any difference.
It might -- I really do not know.
How would I go about overriding the move() method that takes an integer parameter? Do I use public void move(int)
That should do it.
You need to login to post a reply.