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

2014/11/17

Hey i need some help! :)

Erdrag1 Erdrag1

2014/11/17

#
Hey. so im making a game where turtles eat food (i know its not that advanced :P anyway) and i want the food to respawn after they have eaten it, this is the code i typed in World Class private void addWorms() { if(getObjects(Lettuce.class).size() ==0 ) addLettuce();} } but it says "illegal start of expression" can anyone help me? please //erdrag
danpost danpost

2014/11/17

#
Remove the curly bracket at the end of the 'if' line; then, make sure your other brackets are paired properly. (hint: pressing Ctrl-Alt-I, auto-indenting, could help).
Erdrag1 Erdrag1

2014/11/17

#
okey i changed everything but it still says "Illegal start of expression" at the "private void addLettuce" line this is the whole code in "World class" public Cell() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Lettuce lettuce = new Lettuce(); addObject(lettuce, 495, 231); Lettuce lettuce2 = new Lettuce(); addObject(lettuce2, 179, 334); Lettuce lettuce3 = new Lettuce(); addObject(lettuce3, 76, 171); Lettuce lettuce4 = new Lettuce(); addObject(lettuce4, 341, 95); Lettuce lettuce5 = new Lettuce(); addObject(lettuce5, 426, 311); Lettuce lettuce6 = new Lettuce(); addObject(lettuce6, 100, 286); Lettuce lettuce7 = new Lettuce(); addObject(lettuce7, 242, 172); Lettuce lettuce8 = new Lettuce(); addObject(lettuce8, 178, 72); Lettuce lettuce9 = new Lettuce(); addObject(lettuce9, 508, 63); Turtle turtle = new Turtle(); addObject(turtle, 349, 248); private void addLettuce() { if getObjects(Lettuce.class).size() ==0 ) addLettuce(); } } }
danpost danpost

2014/11/17

#
Your brackets are not quite correct. You have a closing curly bracket extra at the bottom that needs to be at the end of the 'prepare' method. Also, you are missing an open round bracket after 'if' in the 'addLettuce' method.
Erdrag1 Erdrag1

2014/11/17

#
Okey the code works now, but the food does not respawn, is there anything else i need to do (like set a time when they respawn or random time, and how?) i've only done the food code in work class
davmac davmac

2014/11/17

#
Firstly, please use code tags when you post code here. Otherwise copy/pasting your code, and referring to parts of it by line number, is difficult. Second, the method looks bad:
    private void addLettuce()
    {
        if getObjects(Lettuce.class).size() ==0 ) addLettuce();
    }
The method is called 'addLettuce'; it checks if there are no Lettuce objects in the world, and if there are none, it calls 'addLettuce', i.e. it calls itself. This will make it run again, and there will still be no lettuce in the world, so it will call itself again, and so on until there is a stack overflow. So, first you need to correct the fucntion of this method. I'd suggest that instead of calling itself, it should actually add a new Lettuce object in to the world. Second, to make the method do anything at all, you need to call it periodically. In Greenfoot the best way to do this is to call it from act() method. Since this is a World subclass, it won't have an existing act method, but you can add one in yourself.
danpost danpost

2014/11/17

#
Well, there are a couple things still wrong with the code. (1) you are calling 'addLettuce' from within the 'addLettuce' method; this will cause your program to crash when no lettuce is detected in your world (the method will continually call itself); (2) there is no code in your class that causes the 'addLettuce' to run to begin with; You can change the name of the 'addLettuce' method to 'act' so the condition within the method can be checked during run-time; and you can write a new 'addLettuce' method that actually adds lettuce into the world.
Erdrag1 Erdrag1

2014/11/18

#
how do you you add an act method if world class doenst have it?
davmac davmac

2014/11/18

#
how do you you add an act method if world class doenst have it?
The same way you add any method. You added an 'addLettuce' method; you can add an 'act' method in precisely the same way. (However, note that the 'act' method must be public rather than private).
You need to login to post a reply.