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

2014/5/14

getWorld Error

kdbailey kdbailey

2014/5/14

#
I'm having an issue with the getWorld error. I'm trying to have spikes randomly spawn throughout the map at set intervals, and I set it up accordingly. However, whenever I compile my code I get an error stating that Greenfoot cannot find symbol - method getWorld() Here is my code:
public class world extends World
{
private int counter;
static int randomNumber;
    /**
     * Constructor for objects of class world.
     * 
     */
    public world()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        addSpikes();
        int randomNumber = 1+ Greenfoot.getRandomNumber(24);
    }

    public void addSpikes()
    {
        if (randomNumber > 0)
        { randomNumber --;
            if (randomNumber == 0)
            {

                int x = Greenfoot.getRandomNumber(getWorld().getWidth());
                int y = Greenfoot.getRandomNumber(getWorld().getWidth());
                for (int count = 0; count < 2; count = count + 1)
                {
                    if (getObjects(Spike.class, x, y).isEmpty())

                        setLocation(x,y);

        
                }

            }
        }
    }
}
Super_Hippo Super_Hippo

2014/5/14

#
Since you are in a World class, you can just delete the "getWorld().". It is only needed when you want to use methods of the World class from an Actor class. The World class already has the World methods available. ;)
kdbailey kdbailey

2014/5/14

#
Super_Hippo wrote...
Since you are in a World class, you can just delete the "getWorld().". It is only needed when you want to use methods of the World class from an Actor class. The World class already has the World methods available. ;)
Thanks, that solved that error. Another one now is whenever I compile it, it says: method getObjects in class greenfoot.World cannot be applied to given types; required" java.lang.Class; found: java.lang.Class<Spike>,int,int; reason: actual and formal argument differ in length. Is this because the image im using for spike is too large? I made everything myself and they don't completely fit when I manually put them into the world. If so, is there also a command that allows me to resize objects put into the world?
Super_Hippo Super_Hippo

2014/5/14

#
I think you should use the method 'getObjectsAt(x, y, Spike.class)'. The method getObjects() returns a list of ALL objects of the class you put in or all objects, if you put 'null' in. It only has one argument, the class, no coordinates. That is what the error message told you. Am not sure, if you did it on purpose, but probably you should change 'Width' in line 25 to 'Height'. Oh and you should actually add a spike instead of line 30. The world can't change its location. Only actors can change their position on the world.
danpost danpost

2014/5/14

#
The code given will never add spikes into the world, as is. Line 19 compares the value of 'randomNumber' with '0' and will find it never to be greater than '0'. The static field, 'randomNumber' has an initial value of '0' and it is never assigned a different value before the 'addSpikes' method is called in the world constructor. In fact, it is never assigned a different value EVER. Line 14 is what I call a 'hanger' line -- it has no purpose. It creates and assigns a variable that is local to the constructor (its scope is bound to the constructor itself); then, it is ignored; that is, it is not ever referred to or utilized later within the constructor. Maybe you intended that this line assign a value to your static field with the same name. If that be the case, you could just remove the 'int' at the beginning of the line; thereby, not declaring a local field, but using the static field declared above. Still, assigning it after calling 'addSpikes', when the method needs it assigned first, is kind of pointless. Your initial statement used the phrase 'at set intervals' when referring to the random spawning of spikes throughout your map. That is ambiguous, as it could be interpreted as being in context with time or with space (locations throughout the map). I did notice the somewhat strange use of the 'for' loop using a 'counter' variable. Again the 'counter' variable is declared within the 'for' statement and its scope is limited to the 'for' loop. It is not the same 'counter' field that is declared near the top of the class.
kdbailey kdbailey

2014/5/15

#
danpost wrote...
The code given will never add spikes into the world, as is. Line 19 compares the value of 'randomNumber' with '0' and will find it never to be greater than '0'. The static field, 'randomNumber' has an initial value of '0' and it is never assigned a different value before the 'addSpikes' method is called in the world constructor. In fact, it is never assigned a different value EVER. Line 14 is what I call a 'hanger' line -- it has no purpose. It creates and assigns a variable that is local to the constructor (its scope is bound to the constructor itself); then, it is ignored; that is, it is not ever referred to or utilized later within the constructor. Maybe you intended that this line assign a value to your static field with the same name. If that be the case, you could just remove the 'int' at the beginning of the line; thereby, not declaring a local field, but using the static field declared above. Still, assigning it after calling 'addSpikes', when the method needs it assigned first, is kind of pointless. Your initial statement used the phrase 'at set intervals' when referring to the random spawning of spikes throughout your map. That is ambiguous, as it could be interpreted as being in context with time or with space (locations throughout the map). I did notice the somewhat strange use of the 'for' loop using a 'counter' variable. Again the 'counter' variable is declared within the 'for' statement and its scope is limited to the 'for' loop. It is not the same 'counter' field that is declared near the top of the class.
As I said, i'm very new to coding. I'm trying to make it that the world will spawn spikes within itself at random coordinates every 6 seconds. Could you give any specific directions on how to fix this error, along with making the code do what I want it to?
Super_Hippo Super_Hippo

2014/5/15

#
If you want to do something over timer, you need an 'act' method. If you play at a 'normal' speed, this 300 will be about 5-6 seconds or something. If you want exactly 6 seconds, you can also use the milliseconds from the system which are unaffected by the Greenfoot Speed. But then, the game will be different if you change the speed. So I would suggest to just adjust the 300 to the value you want and try this code:
    private int spikeTimer = 0;
    public void act()
    {
        spikeTimer++;
        if (spikeTimer > 300)
        {
            spikeTimer = 0;
            addObject(new Spike(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        }
        
    }
You need to login to post a reply.