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

2013/4/16

Counter (unreachable statement)

JesusJesus JesusJesus

2013/4/16

#
Hey, in my Little game the actor Spider changes his Image if canSee the Laser class. Then he should Count the Counter til 0 and then remove. But it says "Unreachable Statement". What is false??
setImage("Blut.png");
          bloodcounter = 100;
          return;
          if(bloodcounter > 0)
          {
              bloodcounter--;
          }
          else
          {
              remove(Spider.class);
              return;
          }
          setImage("Blut.png");
          return;
Thanks a lot
Gevater_Tod4711 Gevater_Tod4711

2013/4/16

#
After the first return statement the method will break up. The return will always be executed and so the code that follows the return will never be executed. That causes this compiler error.
Gevater_Tod4711 Gevater_Tod4711

2013/4/16

#
I think another problem will be in line 10. remove(Spiderl.class) will probably not work. You should use getWorld().removeObject(this); (after this statement you can use a return)
JesusJesus JesusJesus

2013/4/16

#
So i removed the return; but it isnt working also without it...
JesusJesus JesusJesus

2013/4/16

#
okay ive changed it, and it says nreachable Statement in line 04
Gevater_Tod4711 Gevater_Tod4711

2013/4/16

#
could you post the whole class?
JesusJesus JesusJesus

2013/4/16

#
Sure
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ambulance here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Spider extends All
{
    private int bloodcounter = 0;
    /**
     * Act - do whatever the Ambulance wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveRandom();  
        if (getWorld() != null) remove();  
    }
    public void moveRandom() 
    {
     move(2);
     if (Greenfoot.getRandomNumber (100) < 10)
     {
         turn(Greenfoot.getRandomNumber (90) -45);
     } 
     if (getX() <= 5 || getX() >= getWorld ().getWidth() -5)
     {
         turn(180);
     }
     if (getY() <= 5 || getY() >= getWorld().getHeight() -5)
     {
         turn(180);
     }
    }
    public void remove()
    {
        if (canSee(Laser.class) )
      {
          setImage("Blut.png");
          bloodcounter = 100;
          return;
          if(bloodcounter > 0)
          {
              bloodcounter--;
          }
          else
          {
               getWorld().removeObject(this); 
          }

      }
  }
}
JesusJesus JesusJesus

2013/4/16

#
aaaah now i understand sry and thx. But another Problem: first i had the code to remove the Spider in the laser code. But then i dont want to remove it, but i want to Change the Image. So i made the code for the Spider class (canSee(Laser.class)) and now the Spiders cant be killed?! it seems like they never see the laser class...
JesusJesus JesusJesus

2013/4/16

#
it works now, thank you very much :D
You need to login to post a reply.