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

2016/3/11

How to fix unreachable statement error, and have boolean return true??

KeithM KeithM

2016/3/11

#
When I place the "return true" statement in this position,
  public boolean eat()
    {
        Food f1 = new Food();
        Actor f;
        f = getOneObjectAtOffset(0, 0, Food.class);
        if (f != null)
        {
            return true;
            World world;
            world = getWorld();
            world.removeObject(f);
            getWorld().addObject(f1, (int)(Math.random()*36+1), (int)(Math.random()*25+1)); 
            foodEaten += 1;
         
        }
        else
            return false;
       
    }
It gives me an unreachable statement error, however when i place it in this postion,
   public boolean eat()
    {
        Food f1 = new Food();
        Actor f;
        f = getOneObjectAtOffset(0, 0, Food.class);
        if (f != null)
        {
            World world;
            world = getWorld();
            world.removeObject(f);
            getWorld().addObject(f1, (int)(Math.random()*36+1), (int)(Math.random()*25+1)); 
            foodEaten += 1;
            return true;
        }
        else
            return false;
       
    }
It never returns true. Any help is appreciated.
Super_Hippo Super_Hippo

2016/3/11

#
A 'return' exits the method. So nothing after that can executed. If you place it in line 8, then lines 9 through 13 are useless. How do you use the 'eat' method?
KeithM KeithM

2016/3/11

#
heres the whole class. I use it to remove the "food" class.
public class Snake extends Actor
    {
        private int foodEaten = 0;
        private FollowSnake fs = new FollowSnake();
        private int snakeLength = 1;
    
    public Snake()
    {
        
    }
  
    public void act() 
    {
        move();
        eat();
        grow();
    }
    
    public void move()
    {
        move(1);
        if (Greenfoot.isKeyDown("left"))
        {
            turnTowards(getX()-1, getY());
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turnTowards(getX()+1, getY());
        }
        if (Greenfoot.isKeyDown("down"))
        {
            turnTowards(getX(), getY()+1);  
        }
        if (Greenfoot.isKeyDown("up"))
        {
            turnTowards(getX(), getY()-1);  
        }   
         
        if (getX() <= 0 || getX() > 38 || getY() <= 0 || getY() > 27)
        {
            Greenfoot.stop();
            return;
        }
    }   
    
    public boolean inBounds()
    {
        if (getX() <= 0 || getX() > 38 || getY() <= 0 || getY() > 27)
        {
            Greenfoot.stop();
            return false;
        }
        else 
            return true;
    }     
       
    
    public boolean eat()
    {
        Food f1 = new Food();
        Actor f;
        f = getOneObjectAtOffset(0, 0, Food.class);
        if (f != null)
        {
            World world;
            world = getWorld();
            world.removeObject(f);
            getWorld().addObject(f1, (int)(Math.random()*36+1), (int)(Math.random()*25+1)); 
            foodEaten += 1;
            return true;
        }
        else
            return false;
       
    }
        
    private void grow()
    {
        for (int i = 0; i < foodEaten*3; i++)
        {          
       
        }
    }
    
    public int getsnakeLength()
    {
        return snakeLength;
    }
    
}
The grow method currently does nothing.
KeithM KeithM

2016/3/11

#
i understand this, however the boolean never = true. When i remove line 67, it is functional, however, then the food is never removed from the game board. (The food class contains no methods)
danpost danpost

2016/3/11

#
Replace lines 70 through 73 with the following:
}
return f != null;
This should not really change anything from the code it replaces; but, if ever a Food object is removed, which you seem to indicate happens with "When <such-n-such>, then the food is never removed...", the method must return a true value. All this is rather immaterial, as the value returned is not used through the call to the 'eat' method. That is, line 15:
eat();
calls the method, but ignores totally any returned value.
You need to login to post a reply.