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

2015/9/16

How to use getWorld in Actor class.

1
2
Doaky Doaky

2015/9/16

#
I want to make a loop that goes until all the objects of a certain Actor subclass are removed from the world. I have tried: while ( !getWorld(getObjects(Actor.Flower).isEmpty()) ) But I get an error saying that the variable "Actor" can't be found. -------------------------------------------------------------- I also want to create a loop that goes until an Actor reaches -2, -2 of the world's height and width. I have tried: while ( !this.getWorld(getWidth() == -2) && !this.getWorld(getHeight() == -2) ) But I think this is probably very far off from what is supposed to be done.
danpost danpost

2015/9/16

#
What do you want to happen when all the flowers are gone? You can add code into your World subclass 'act' method for that. Then, you do not need to restrict actions of this actor to when flowers are in the world. It is impossible for you to create a world of width less than one (or zero); and, anyways, if you were changing the size of the world, your actor would have to be placed in each one as they are created. I think you want to check the x- and y-coordinate values of the location of the actor, in which case you would be using the 'getX' and 'getY' methods of the Actor class. The returned values of these will never be less than zero unless you are working in a world that is unbounded (using the 'super(int, int, int, boolean)' constructor with the boolean parameter set to 'true'. Then, the actor would need to be precisely at that location unless you check for 'less than or equal to' instead of 'equal to'.
Doaky Doaky

2015/9/16

#
I want the loop to stop when the flowers are gone. The assignment has constrictions and I cannot edit the world subclass. Also, I want the Actor to stop when it reaches the corner of the map, which is always 2 spaces from the very edge of the world. The levels are randomized
danpost danpost

2015/9/17

#
I think you want to compare the location coordinate values of the actor to the size of the world minus 2 then. The 'getX' and 'getY' methods return the coordinate values of the actor; the 'getWorld' method returns the world which you can use the World class methods 'getWidth' and 'getHeight' on to get its size:
1
2
3
World world = getWorld();
int worldWidth = world.getWidth();
int worldHeight = world.getHeight();
The lower-right corner of the world is always at the point (worldWidth-1, worldHeight-1).
Doaky Doaky

2015/9/17

#
I get an error that says "cannot find symbol - class World" when I put in 'World world = getWorld();'
danpost danpost

2015/9/17

#
Doaky wrote...
I get an error that says "cannot find symbol - class World" when I put in 'World world = getWorld();'
Please select all and copy/paste the class you get the error in here (read the page on 'Posting code? read this!' linked below the reply box).
Doaky Doaky

2015/9/17

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import sofia.micro.jeroo.*;
 
//-------------------------------------------------------------------------
/**
 *  MazeRunner runs through randomly generated mazes.
 *  The Jeroo does this until all flowers and nets are gone.
 *  Starts in North-West corner, ends in South-East Corner.
 *  Place Jeroo at 1,1 and run program.
 *
 *  @version 2015.09.09
 */
public class MazeRunner extends Jeroo
{
    //~ Fields ................................................................
 
    // Defines tim
    private MazeRunner tim;
 
    //~ Constructor ...........................................................
 
    // ----------------------------------------------------------
    /**
     * Creates a new MazeRunner object.
     * @ param x = 1
     * @ param y = 1
     * With 10 flowers
     */
    public MazeRunner()
    {
        super(1, 1, 10);
        World world = getWorld();
        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();
    }
 
    //~ Methods ...............................................................
    public void myProgram()
    {
        MazeRunner tim = new MazeRunner();
 
        this.runMaze();
        this.goToCorner();
 
        this.getWorld();
        //MazeIsland.getObjects();
    }
 
    public void runMaze()
    {
        //while ( !getWorld(getObjects(Actor.Flower).isEmpty()) )
        while ( this.hasFlower() )
        {
            if ( this.seesFlower(HERE) )
            {
                this.pick();
            }
            else
            {
                if ( this.seesNet(RIGHT) )
                {
                    this.turn(RIGHT);
                    this.toss();
                    this.hop();
                }
                if ( this.isClear(RIGHT) || this.seesFlower(RIGHT) )
                {
                    this.turn(RIGHT);
                    this.hop();
                }
                else
                {
                    if ( this.isClear(AHEAD) || this.seesFlower(AHEAD) )
                    {
                        this.hop();
                    }
                    else
                    {
                        if ( this.seesNet(AHEAD) )
                        {
                            this.toss();
                        }
                        else
                        if ( this.seesWater(AHEAD) && this.seesWater(LEFT) )
                        {
                            this.turn(RIGHT);
                        }
                        else
                        {
                            if ( this.seesWater(AHEAD) && this.seesWater(RIGHT) )
                            {
                                this.turn(LEFT);
                            }
                            else
                            {
                                if ( this.seesWater(AHEAD) )
                                {
                                    this.turn(RIGHT);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
 
    public void netRight()
    {
        this.turn(RIGHT);
        this.toss();
        this.hop();
    }
 
    public void mazeRight()
    {
        this.turn(RIGHT);
        this.hop();
    }
     
    public void goToCorner()
    {
        while ( !this.getX == worldWidth-2 && !this.getY == worldHeight-2 )
        {
            this.runMaze();
        }
    }
}
danpost danpost

2015/9/17

#
Alright. This, first off, does not look like a standard greenfoot project -- but a 'sofia' project, which is quite different. The code I gave was just a snippet to acquire certain values as if it was a greenfoot project. I would have to see the codes for the Jeroo class, the MazeIsland class and possibly other classes to see what these methods you are calling do and how to access specific data within those classes.
Doaky Doaky

2015/9/17

#
http://sofia.cs.vt.edu/api/reference/sofia/micro/jeroo/Jeroo.html The MazeIsland code is locked
danpost danpost

2015/9/17

#
It appears that the line I gave:
1
World world = getWorld();
could be changed to the following:
1
sofia.micro.World world = getWorld();
to work. I believe you could alternatively add the following import statement at the beginning of the class:
1
import sofia.micro.*;
Doaky Doaky

2015/9/17

#
That worked! But now "while ( !this.getX == worldWidth-2 && !this.getY == worldHeight-2 )" doesn't work
danpost danpost

2015/9/17

#
Doaky wrote...
That worked! But now "while ( !this.getX == worldWidth-2 && !this.getY == worldHeight-2 )" doesn't work
All methods in your code must be followed by a set of round brackets for parameters. If there are no parameter, then nothing would be inserted between them. Try this:
1
while (!this.getX() == worldWidth-2 || !this.getY() == worldHeight-2)
The conditions should be OR'ed not AND'ed. If either is not true, then you want your player to continue.
Doaky Doaky

2015/9/17

#
I got an error that says "operator ! cannot be applied to float"
danpost danpost

2015/9/17

#
Doaky wrote...
I got an error that says "operator ! cannot be applied to float"
I was hoping to edit my last post before you responded. The post should read as follows:
Doaky wrote...
That worked! But now "while ( !this.getX == worldWidth-2 && !this.getY == worldHeight-2 )" doesn't work
All methods in your code must be followed by a set of round brackets for parameters. If there are no parameter, then nothing would be inserted between them. Try this:
1
while (this.getX() != worldWidth-2 || this.getY() != worldHeight-2)
You cannot negate an 'int' (or 'float') value in a true/false fashion, so, the comparisons must check for inequality. Also, the conditions should be OR'ed not AND'ed. If either is not true, then you want your player to continue.
Doaky Doaky

2015/9/17

#
Thank you very much! I have learned a lot from this. I still have one other problem though.
1
while ( !getWorld(getObjects(Actor.Flower).isEmpty()) )
This gets the error "cannot find symbol - variable Actor" Am I using the wrong keyword for this?
There are more replies on the next page.
1
2