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

2024/11/15

Remove isnt working

Vince361 Vince361

2024/11/15

#
For some Reason instead of the Feuerball(Fireball) being removed, when hitting something a window opens itself with error codes because the code couldnt be carried out eventhough the return should stop the code from continuing.
 public void act() 
    {
      
      if (!getWorld().getObjects(Feuerball.class).isEmpty()) {
       bewegen();
       removeTouching(Abenteurer.class);
       
     } 
     
     
    }
    
    
   
   
     public void bewegen(){
       int x=getX();
       int y=getY();
       int richtung=getRotation();
      
       if (((getRotation()==0) && getOneObjectAtOffset(1, 0, Wand.class) !=null)) {
       getWorld().removeObjects(getWorld().getObjects(Feuerball.class));
       return;
       }
       else if (((getRotation()==90) && getOneObjectAtOffset(0, 1, Wand.class) !=null)){
       getWorld().removeObjects(getWorld().getObjects(Feuerball.class));
       return;
       }
       else if (((getRotation()==180) && getOneObjectAtOffset(-1, 0, Wand.class) !=null)) {
       getWorld().removeObjects(getWorld().getObjects(Feuerball.class));
       return;
     }
       else if (((getRotation()==270) && getOneObjectAtOffset(0, -1, Wand.class) !=null)){
       getWorld().removeObjects(getWorld().getObjects(Feuerball.class));
       return;
       } 
       
       else if ((getRotation()==0) && getOneObjectAtOffset(1, 0, Wand.class) ==null){
       move('r');
         
       } 
       else if ((getRotation()==90) && getOneObjectAtOffset(0, 1, Wand.class) ==null){
       move('u');
       
       }
       else if ((getRotation()==180) && getOneObjectAtOffset(-1, 0, Wand.class) ==null){
       move('l');
       
       }
       else if ((getRotation()==270) && getOneObjectAtOffset(0, -1, Wand.class) ==null){
       move('o');
       
         }
         
          if ((richtung == 90 && x >= 12) || (richtung == 180 && y >= 12) || (richtung == 270 && x <= 0) || (richtung == 0 && y <= 0)) //es wird überprüft, ob der Feuerball den Rand getroffen hat
        {
            getWorld().removeObject(this);//Feuerball wird entfernt, da dieser nichts getroffen hat und den Rand der Welt erreicht hat
            return;
        }
    }
}
danpost danpost

2024/11/15

#
Vince361 wrote...
For some Reason instead of the Feuerball(Fireball) being removed, when hitting something a window opens itself with error codes because the code couldnt be carried out eventhough the return should stop the code from continuing. << Code Omitted >>
A Feuerball instance will only act when in the world. So,, it seems pointless for the first action taken to be asking if any of those instances are in the world (of course, the one acting is in the world). It would be much better to have objects it may hit to detect this object instead of the other way around. That way, it can deal with any damage or removals as needed for that encounter. Also, using the isTouching method would probably be sufficient there (instead of using offsets).
You need to login to post a reply.