inheritance - Problem removing objects in Java -


lets graphic world,lets api of world, , actor, , built object new class name food inherent actor in situations need object disappeared world. should way ?

i tried this:

public void killfood ()     {             getworld().removeobject(this);  // >>>>>kill object inherate food , operate method.     } 

but didn't killed kind of object class inherent food... why ?

i wrapped (in food class) with:

public void act()      {         if (canmove())             move();         else             killfood();     }    public boolean canmove()     {         world myworld = getworld();         int x = getx();         int y = gety();          y--;         // test outside border         if (x >= myworld.getwidth() || y >= myworld.getheight())              return false;         else if (x < 0 || y < 0)    // if out of 1st quarter             return false;         return true;    // if inside 1st quarter & borders can move.      } 

but object did not disappeared... why ?

thanks !!

========================================================================================== edit: canmove method & mushroom class

public boolean canmove()     {         world myworld = getworld();         int x = getx();         int y = gety();          // test outside border         if (x >= myworld.getwidth() || y >= myworld.getheight()) {             return false;         }         else if (x < 0 || y < 0) {             return false;         }         return true;     }  public class mushroom  extends food {     private final int need_togo_left = 3;     private int mushroomgotdown=0; // counter regular +1 down steps      public void move()     {             mushroomgotdown++;             //  if mushroom didn't got down 2 times, go down 1 time.             if (mushroomgotdown != need_togo_left)                  setlocation(getx() , gety() + 1);                 else    //  mushroom got down twise, third 1 step left.                 {                     setlocation(getx() - 1 , gety());                     mushroomgotdown=0;                 }         }  }   // end of class mushroom 

i assume code looks this:

public abstract class food {     ...     public void killfood () {         getworld().removeobject(this);     }      public void act() {         if (canmove()) {             move();         } else {              killfood();         }     } }  public class cheezeburger extends food {     ... } 

on face of it, should work.

possible reasons why cheezeburger doesn't removed include:

  • your code isn't calling act() on cheezeburger,
  • getworld() returns different world 1 expecting,
  • world.removeobject(...) not working properly,
  • the cheezeburger class (or superclass) has overridden act or canmove or killfood , override method(s) wrong thing.

all of these scenarios can summarized "the bug somewhere else".


assuming you've inspected code , cannot find problem, next step should run using debugger, , single step through code not working see happening.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -