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

2012/9/25

How can I pop up a new worm?

yasumiishi yasumiishi

2012/9/25

#
This is an 'idea' in page 55 of the textbook - 'making new worms pop up when one is eaten'. Can't figure out ... how and where I should use getWorld()?
nooby123 nooby123

2012/9/25

#
Do you want to add a new Worm()?
nooby123 nooby123

2012/9/25

#
Then use this :
getWorld().addObject(new Worm(), getX(), getY());
danpost danpost

2012/9/26

#
@nooby123, if your code is used as soon as you add the worm to the world it will be eaten and another worm will be added and it will be eaten also, then another, and another... Using getX() and getY() would not be advisable here. Better would be Greenfoot.getRandomNumber(getWorld().getWidth()) and Greenfoot.getRandomNumber(getWorld().getHeight()). @yasumiishi, from within a sub-class of 'Actor', you can use 'getWorld()' to get a reference to the world that an actor is in. You can save it in a variable, and then use it to perform some method within the 'World' class:
World world = getWorld();
int worldWidth = world.getWidth();
int worldHeight = world.getHeight();
which would be preferable if you need a reference to the world for multiple operations. If you need the reference only one or two times, you could just use it directly:
int worldWidth = getWorld().getWidth();
int worldHeight = getWorld().getHeight();
Please note that methods and variables that you create in your sub-class of World must be accessed differently.
hey not all of the green foot users like me go to the same school as you or have the same book as you so can you please explain whats on page 55
yasumiishi yasumiishi

2012/10/17

#
Hi Abdullah, sorry for my late response ... It's "Introduction to Programming with Greenfoot" (latest version). Thank you all :O
You need to login to post a reply.