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

2017/10/10

If an object is eaten an new object is created

Bangman Bangman

2017/10/10

#
I am trying to implement this problem: If a turtle is eaten by a snake, 10 new snakes should be created. I do not understand how I can correct the code!
public void spawnMehr()
 {
        Actor lil_turtle;
        boolean lt1 = getWorld().removeObject         (lil_turtle);        
        
     if( lt1=true)
      {for(int i=0; i<10; i++)
            {
            getWorld().addObject( new Snakee(),Greenfoot.getRandomNumber(850),
            Greenfoot.getRandomNumber(600));
            }           
        
        }
        
    }
danpost danpost

2017/10/10

#
Going backwards, line 6 should test the current value of 'ltl' without assigning a 'true' value to it first, which is what you have using the assignment operator '='. To compare 'ltl' and 'true', you need to use the equality comparison operator '==' (a double equal sign), which would look like this:
if (ltl == true)
Now, going back to line 4, 'ltl' should be assigned a boolean value, yet the 'removeObject' method does not return any value (it is a 'void' method). Finally, line 3 declares an Actor reference that is never assigned any possible value prior to its use in line 4 ('lil_turtle' is always 'null' and nothing will ever be removed from the world by line 4 (even without the beginning of the line: 'boolean ltl = '). You can start with this:
if (isTouching(Turtlee.class))
and use 'removeTouching' and add multiple snakes within the following block of code.
You need to login to post a reply.