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

2011/12/23

how do I make things respawn?

1
2
programmer22 programmer22

2011/12/23

#
like if my crab eats all the worms on the screen how can i get it to make the worms respawn?
danpost danpost

2011/12/23

#
Make a method in the world class that adds worms to the world (in random places, of course). In the world's act method add
if (getObjects(Worm.class).length == 0) addWorms();
Of course, I named the method 'addWorms()'. You can decide what number, or in what range the number of worms to be added is. You could even call the method from the constuctor to initially add the worms.
programmer22 programmer22

2011/12/23

#
um probably a silly question but which actor would i put this in ?
danpost danpost

2011/12/23

#
None, read what I had written, word for word.
programmer22 programmer22

2011/12/23

#
im really sorry must be irritating but im not sure what you mean by method but anyways what im about to show you has an error says cannot find symbol length probly an easy fix but here it is import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class CrabWorld here. * * @author (your name) * @version (a version number or a date) */ public class CrabWorld extends World { /** * Constructor for objects of class CrabWorld. * */ public CrabWorld() { super(560, 560, 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() { Crab crab = new Crab(); addObject(crab, 257, 300); Worm worm = new Worm(); addObject(worm, 392, 50); Worm worm2 = new Worm(); addObject(worm2, 462, 264); Worm worm3 = new Worm(); addObject(worm3, 425, 501); Worm worm4 = new Worm(); addObject(worm4, 413, 227); Worm worm5 = new Worm(); addObject(worm5, 427, 460); Worm worm6 = new Worm(); addObject(worm6, 179, 488); Worm worm7 = new Worm(); addObject(worm7, 246, 495); Worm worm8 = new Worm(); addObject(worm8, 54, 443); Worm worm9 = new Worm(); addObject(worm9, 88, 269); Worm worm10 = new Worm(); addObject(worm10, 105, 193); Worm worm11 = new Worm(); addObject(worm11, 103, 52); Worm worm12 = new Worm(); addObject(worm12, 223, 109); Worm worm13 = new Worm(); addObject(worm13, 280, 71); Worm worm14 = new Worm(); addObject(worm14, 339, 195); Worm worm15 = new Worm(); addObject(worm15, 525, 105); Worm worm16 = new Worm(); addObject(worm16, 480, 400); Worm worm17 = new Worm(); addObject(worm17, 130, 390); Lobster lobster = new Lobster(); addObject(lobster, 112, 474); Lobster lobster2 = new Lobster(); addObject(lobster2, 94, 112); Counter counter = new Counter(); addObject(counter, 46, 53); counter.setLocation(23, 22); Worm worm18 = new Worm(); addObject(worm18, 336, 509); Worm worm19 = new Worm(); addObject(worm19, 447, 143); Worm worm20 = new Worm(); addObject(worm20, 490, 472); if (getObjects(Worm.class).length == 0) addWorms(); } }
danpost danpost

2011/12/23

#
Sorry, should be
if (getObjects(Worm.class).size() == 0) addWorms(); 
programmer22 programmer22

2011/12/23

#
after switching that it says cannot find symbol method addWorms()
danpost danpost

2011/12/23

#
addWorms() is a method that YOU have to put within the class. Go to the end of the world class code, down to the last '}'. Just before the last '}', insert your new method. Start with
private void addWorms()
{
    // code to add worms here
}
You already have the code to add worms to the world in the world class. Find it, and copy that specific code to where my comment is (code to add worms here).
programmer22 programmer22

2011/12/23

#
ok so after addign in that i played the game ate all the worms and nothing no worms were added so i was like ok and went ahead and put the number 20 in the parentheses after add worms thinkign that would fix it but just came up with an error saying method addworms in class CrabWorld cannot be applied to given types; required:no arguments fount int reason: actual and formal argument lists differ in length any ideas on what to do
danpost danpost

2011/12/23

#
Post the code for the addWorms() method.
programmer22 programmer22

2011/12/23

#
here ill show you what i have import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class CrabWorld here. * * @author (your name) * @version (a version number or a date) */ public class CrabWorld extends World { /** * Constructor for objects of class CrabWorld. * */ public CrabWorld() { super(560, 560, 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() { Crab crab = new Crab(); addObject(crab, 257, 300); Worm worm = new Worm(); addObject(worm, 392, 50); Worm worm2 = new Worm(); addObject(worm2, 462, 264); Worm worm3 = new Worm(); addObject(worm3, 425, 501); Worm worm4 = new Worm(); addObject(worm4, 413, 227); Worm worm5 = new Worm(); addObject(worm5, 427, 460); Worm worm6 = new Worm(); addObject(worm6, 179, 488); Worm worm7 = new Worm(); addObject(worm7, 246, 495); Worm worm8 = new Worm(); addObject(worm8, 54, 443); Worm worm9 = new Worm(); addObject(worm9, 88, 269); Worm worm10 = new Worm(); addObject(worm10, 105, 193); Worm worm11 = new Worm(); addObject(worm11, 103, 52); Worm worm12 = new Worm(); addObject(worm12, 223, 109); Worm worm13 = new Worm(); addObject(worm13, 280, 71); Worm worm14 = new Worm(); addObject(worm14, 339, 195); Worm worm15 = new Worm(); addObject(worm15, 525, 105); Worm worm16 = new Worm(); addObject(worm16, 480, 400); Worm worm17 = new Worm(); addObject(worm17, 130, 390); Lobster lobster = new Lobster(); addObject(lobster, 112, 474); Lobster lobster2 = new Lobster(); addObject(lobster2, 94, 112); Counter counter = new Counter(); addObject(counter, 46, 53); counter.setLocation(23, 22); Worm worm18 = new Worm(); addObject(worm18, 336, 509); Worm worm19 = new Worm(); addObject(worm19, 447, 143); Worm worm20 = new Worm(); addObject(worm20, 490, 472); } private void addWorms() { if (getObjects(Worm.class).size() == 0) addWorms(20); } }
danpost danpost

2011/12/23

#
The statement you put within the addWorms() method goes in the act() method, not here. This statement is what initiates the addWorms() method if no worms are found in the world. (do not forget to remove the '20'). The prepare() method contains the code that adds all of the worms to the world, copy all relavent code in prepare() to addWorms() (if it has the word 'worm' in the statement, it is relavent).
programmer22 programmer22

2011/12/23

#
so ummm can u simplify that like take my code and put what u said in
danpost danpost

2011/12/23

#
You are asking a lot -- code to add twenty worms to the world individually? Just copy everything in your prepare() method into the addWorms() method; then delete any statement that does not have the word 'worm'.
programmer22 programmer22

2011/12/23

#
ok thanks if i have any troubles ill ask thanks for all your help its nice to have someone like u who can help me and it helps me learn more about programming
There are more replies on the next page.
1
2