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

2012/12/17

Unreachable Statment

Hawx_ Hawx_

2012/12/17

#
Here's what I've got:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mine2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mine2 extends Actor
{
    /**
     * Act - do whatever the Mine2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        location();
        intersect();
    }

    private void move()
    {
        move(-(Greenfoot.getRandomNumber (10)));

    }  

    private void location()
    {
        if (getX() <= 5)
        {
            Mine mine = new Mine();
            { setLocation(596,(Greenfoot.getRandomNumber (300))); }

        }
    }

    private void intersect()
    { 
        Actor Rocket = getOneIntersectingObject(Rocket.class);
        if (Rocket != null) 
            Greenfoot.stop();
        return;

           
        Actor Bullet = getOneIntersectingObject(Bullet.class); //This line here- unreachable???
        if (Bullet != null) 
            getWorld().removeObject(Bullet);
        return;

    }


}

The line I've commented on is the line that is unreachable , I'm just starting to learn Java so could someone explain why this is unreachable to me please. Thanks a lot, Hawx
tylers tylers

2012/12/17

#
change the intersect() method to this:
 private void intersect()  
    {   
        Actor Rocket = getOneIntersectingObject(Rocket.class);  
        if (Rocket != null)   {
            Greenfoot.stop();  
        return;  
        }
  
             
        Actor Bullet = getOneIntersectingObject(Bullet.class); //This line here- unreachable???  
        if (Bullet != null)  { 
            getWorld().removeObject(Bullet);  
        return; 
        } 
  
    }  
Hawx_ Hawx_

2012/12/17

#
Thanks , all's worked out now !:)
davmac davmac

2012/12/17

#
The problem was line 43 - you 'return' from the method unconditionally (outside the 'if' block), so there was no way any of the code following that line could ever be reached.
You need to login to post a reply.