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

2013/5/13

add a new object

vbh vbh

2013/5/13

#
Hi i want to remove an object when i click whit the mouse, and then add a new object randomly after. this is my code public void act() { // Add your action code here. if(canSee(fisk1.class)) { eat(fisk1.class); fisk1 fisk = new fisk1(); world.addObject(new fisk1(),Greenfoot.getRandomNumber(400), Greenfoot.getRandomNumber(500)); } } i get this erro can not find symbol - method addObject(fisk1, int, int) can i only use addObject in world??
Busch2207 Busch2207

2013/5/13

#
You can use addObject in other classes, too! But first you have to get the current world, to call a method from it! This you can do by: 'getWorld()' So you have to write 'getWorld().addObject(...)' instead of 'world.addObject(...)', unless you declared world as an attribute of the world class and initialized it as one. e.g. by:
World world=getWorld();
world.addObject(...)
vbh vbh

2013/5/13

#
ty new problem i have a fish in the world i want it to disapeare when i click whit the mouse and then add a new fish in a different place, be abel to keep on click remove and add new one. right now the fish that is in the world from start dont get removed and every time i clik i get more and more fish where i only want 1 at the time here is my code any suggestion public void act() { // Add your action code here. if( Greenfoot.mouseClicked(null)) { eat(fisk1.class); getWorld().addObject(new fisk1(),Greenfoot.getRandomNumber(400), Greenfoot.getRandomNumber(500)); } }
Busch2207 Busch2207

2013/5/13

#
Well, the eat(...)-Method can only remove another fish (inasmuch you haven't wrote the method by yourself) To remove the fish in which the method is called you have to write:
getWorld().removeObject(this);
The removeObject - method of the world expects an Actor, that it can remove! If you enter 'this', the objects gives itself to the method.
vbh vbh

2013/5/13

#
i used the method from little crab so i tryed this instead but get a runtime erro, so im not sure i understand how to do it public void act() { // Add your action code here. if( Greenfoot.mouseClicked(null)) { getWorld().removeObject(this); getWorld().addObject(new fisk1(),Greenfoot.getRandomNumber(400), Greenfoot.getRandomNumber(500)); } }
vbh vbh

2013/5/13

#
if i remove the getWorld().addObject(new fisk1(),Greenfoot.getRandomNumber(400), Greenfoot.getRandomNumber(500)); it works my fish is removed when i click, so now i need to find another way to add a new fish :/
Busch2207 Busch2207

2013/5/13

#
Well, the problem is, that if you remove the object from the world, the method getWorld() don't return the world anymore. (because now, there is no) So you've got two possibilities. One is, you first add the new object and then you remove the fish with:
getWorld().addObject(...);
getWorld().removeObject(this);
Or you initialize a parameter and then you call the methods:
World world=getWorld();
world.removeObject(this);
world.addObject(...);
vbh vbh

2013/5/13

#
that works thanky very much :-)
You need to login to post a reply.