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

2014/4/19

Unreachable statement error

patrick26 patrick26

2014/4/19

#
Hi, I keep getting an unreachable statement error in my 'getWorld().addObject(new Enemyfireball(),getX(),getY());' lines of code. Does anyone know how to fix this problem? Is it because you are not allowed to add the same object from the same class? The purpose for the code underneath is to keep adding a fireball whenever it hits the ground or right edge of the world. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Enemyfireball here. * * @author (your name) * @version (a version number or a date) */ public class Enemyfireball extends Actor { private int vSpeed; private int hSpeed; /** * Act - do whatever the Enemyfireball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { plant(); moveAround(); vSpeed = Greenfoot.getRandomNumber(8); hSpeed = 6; } public void moveAround() { setLocation(getX()+hSpeed,getY()+vSpeed); } public void plant() { if(getX()>getWorld().getWidth()-3) { getWorld().removeObject(this); return; getWorld().addObject(new Enemyfireball(),getX(),getY()); } Actor ground = getOneIntersectingObject(Ground.class); if(ground!=null) { getWorld().removeObject(this); return; getWorld().addObject(new Enemyfireball(),getX(),getY()); } } }
danpost danpost

2014/4/19

#
Firstly, no code within the same block will execute after a 'return' statement. Secondly, if the position of the enemyFireball determines it needs to be removed from the world, why would be place the new enemyFireball at the same location (using 'getX' and 'getY')? You could just re-locate the current enemyFireball instead of removing one and adding a different one.
You need to login to post a reply.