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

2015/2/11

Enquiry

1
2
Abanourmo Abanourmo

2015/2/11

#
I am trying to make an actor respawn after it has been removed. The code I use to remove the actor is: public void EatenbyBird() { Actor Bird = getOneObjectAtOffset(0, 0, Bird.class); // looks at the birds location if(Bird != null) { getWorld().removeObject(this); //if bird touches the food then it become null } } Please respond ASAP :-)
danpost danpost

2015/2/11

#
What code did you try to respawn the food? Where did you put that attempted code? Out of curiosity, should there be any change in the state of the bird when it eats food?
Abanourmo Abanourmo

2015/2/11

#
The state of the bird does not change. when the bird eats the food, the food disappears. I just want the food to reappear. and there can only be one food in the world.
danpost danpost

2015/2/11

#
Ok. So, if there is no food in the world, you want a food object to be added at a random location (I presume). This should be done in your world act method. This, in pseudo-code would be: if the list of objects of type Food in the world ( using World instance method 'getObjects' ) is empty ( using List instance method 'isEmpty' ), then add new Food object into the world
Abanourmo Abanourmo

2015/2/11

#
That's correct. However, I am new to Greenfoot and even Java aswell. Would the code be: if background.getObjects(Food.class).isEmpty() { addObject(new Food(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); }
danpost danpost

2015/2/11

#
Abanourmo wrote...
That's correct. However, I am new to Greenfoot and even Java aswell. Would the code be: if background.getObjects(Food.class).isEmpty() { addObject(new Food(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); }
That is very close. Remove the word 'background' (and the dot '.' after it) and enclose the condition, 'getObjects(Food.class).isEmpty()', within round braces.
Abanourmo Abanourmo

2015/2/11

#
Thank you the code works. but then the game stops running after the bird has eaten the food. I tried incorporating this code to respawn the food, but it doesn't obviously because of my lack of experience lol: public void EatenbyBird() { Actor Bird = getOneObjectAtOffset(0, 0, Bird.class); // looks at the birds location if(Bird != null) { getWorld().removeObject(this); //if bird touches the food then it become null if (getWorld().getObjects(Food.class).isEmpty()) { getWorld().addObject(new Food(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld).getHeight())); } } } it does
danpost danpost

2015/2/11

#
danpost wrote...
Ok. So, if there is no food in the world, you want a food object to be added at a random location (I presume). This should be done in your world act method.
The operative word is 'world' (in the bolded italicized part).
Abanourmo Abanourmo

2015/2/11

#
Sorry. I put it in the World class and this is the message i get back: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. After it has been removed, it does not know how to make it appear again.
danpost danpost

2015/2/11

#
Your 'EatenbyBird' method should be as you originally posted it in this discussion. If you still get that error, post the entire Food class code. Use the 'code' link below the reply box to insert code into your posts. Also, it would help if you posted the entire error trace, instead of just the first line of it.
Abanourmo Abanourmo

2015/2/11

#
public void EatenbyBird()
       {
          Actor Bird = getOneObjectAtOffset(0, 0, Bird.class);      // looks at the birds location
          if(Bird != null) {
          
              getWorld().removeObject(this);         //if bird touches the food then it become null
              
              }
          
          if (getWorld().getObjects(Food.class).isEmpty()) {
                 getWorld().addObject(new Food(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
              }      
          
          }
Here is the code.
danpost danpost

2015/2/11

#
danpost wrote...
Your 'EatenbyBird' method should be as you originally posted it in this discussion.
Remove lines 10 through 12 from the code in your last post and place the corrected 'very close' code in your World subclass ( 'background' ) 'act' method.
Abanourmo Abanourmo

2015/2/11

#
The error code does not appear but the food does not respawn.
danpost danpost

2015/2/11

#
Please show the code for your class that 'extends World'.
Abanourmo Abanourmo

2015/2/11

#
public class Background extends World
{

    /**
     * Constructor for objects of class Background.
     * 
     */
    public Background()
    {    
        /** Create a new world with 531x709 cells with a cell size of 1x1 pixels. */
        super(531, 709, 1, true);

        

        
        
        
        if (getObjects(Food.class).isEmpty()) {
          addObject(new Food(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
          }
        
        
            }
There are more replies on the next page.
1
2