2011-05-10 79 views
1

刪除對象比方說,一個圖形世界,讓說的世界API演員,我建立了一個對象,從類新名稱食品演員固有但在某些情況下,我需要這個物體從我的世界中消失。 什麼應該是一個很好的方法呢?問題在Java中

我嘗試這樣做:

public void killFood() 
    { 
      getWorld().removeObject(this); // >>>>>Kill any object that inherate from Food and operate this method. 
    } 

但它並沒有殺死任何類型的對象從類中固有的食物......爲什麼?

我把它包(在食品類):

public void act() 
    { 
     if (canMove()) 
      move(); 
     else 
      killFood(); 
    } 



public boolean canMove() 
    { 
     World myWorld = getWorld(); 
     int x = getX(); 
     int y = getY(); 
     y--; 
     // test for outside border 
     if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) 
      return false; 
     else if (x < 0 || y < 0) // if out of the 1st quarter 
      return false; 
     return true; // if inside 1st quarter & borders it can be move. 
    } 

但該對象並沒有消失,爲什麼?

謝謝!

============================================== ============================================ 編輯:canMove方法&蘑菇類

public boolean canMove() 
    { 
     World myWorld = getWorld(); 
     int x = getX(); 
     int y = getY(); 

     // test for 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 for regular +1 down steps 

    public void move() 
    { 
      mushroomGotDown++; 
      // if mushroom didn't got down 2 times, go down one time. 
      if (mushroomGotDown != NEED_TOGO_LEFT) 
       setLocation(getX() , getY() + 1); 
       else // mushroom got down twise, third will be 1 step left. 
       { 
        setLocation(getX() - 1 , getY()); 
        mushroomGotDown=0; 
       }  
    } 
} // end of class Mushroom 
+0

沒有看到removeObject()的實現,很難告訴你爲什麼removeObject()不起作用。 – EboMike 2011-05-10 21:59:25

+0

您可以發佈方法'removeObject'的代碼? – janhink 2011-05-10 22:00:06

+0

它在上面的API鏈接中... 鏈接:http://www.greenfoot.org/doc/javadoc/ – 2011-05-10 23:57:29

回答

1

它看起來getWorld().removeObject(this)只會從你的世界中刪除特定的實例。它不會從您的世界中刪除所有特定課程的實例。

您需要在要刪除的類的每個特定實例上調用getWorld()。removeObject。

所以看到這是如何工作的嘗試是這樣的:從您的問題

Food foo1 = new Food(); 
Food foo2 = new Food(); 
Food foo3 = new Food(); 

World world = new World(); 
world.add(foo1, ...); //Be sure to place each object in a distinct position so you can see each one. 
world.add(foo2, ...); 
world.add(foo3, ...); 

//Now delete one. 
foo1.killFood(); 

//one of th ethree should go away. 

不管怎麼說,我想,你是與類本身的類的實例混亂。

這當然假定removeObject的作品。

+0

所以你說,getWorld()。removeObject(this);與'this'一起使用是否合法?如果它是一個圖形界面,爲什麼當我使用killFood()時,食物固有的物品不會死亡,如果物品碰到地面? – 2011-05-10 22:13:13

+0

getWorld()。removeObject(this);很好。它會從您的世界中移除特定的食物。我不確定你爲什麼認爲從食物中繼承的物品會死亡。也許繼承並不意味着你認爲它的作用? – 2011-05-10 22:15:00

+0

現在我也瞭解了你,所以現在你可以修復我的代碼(通過編輯你的代碼),並告訴我如何解決它,以保證從食物中固有的類創建的每個對象將會死上樓吧?我真的需要它來理解它...... – 2011-05-10 22:18:37

2

我假設你的代碼如下所示:

public abstract class Food { 
    ... 
    public void killFood() { 
     getWorld().removeObject(this); 
    } 

    public void act() { 
     if (canMove()) { 
      move(); 
     } else { 
      killFood(); 
     } 
    } 
} 

public class Cheezeburger extends Food { 
    ... 
} 

在它的面前,應該工作。

中的可能原因cheezeburger不會被刪除包括:

  • 你的代碼實際上不是呼籲cheezeburger act()
  • getWorld()返回一個不同的世界,爲您期望,
  • ​​工作不正常,
  • 的Cheezeburger類(或超類)已經覆蓋actcanMovekillFood一個d覆蓋方法做錯誤的事情。

所有這些情況可以概括爲「該錯誤是其他地方」。


假設你檢查你的代碼並不能發現問題,下一步應該是使用調試器來運行它,和單步它通過不工作,看看究竟發生什麼事的代碼。

+0

act()被接口自動調用 World.removeObject(...)測試其他類,直接從Actor演出,它的工作... Cheezeburger風格類做覆蓋,但只在移動()類 所以我們只留下wi 「可能的」getWorld()會向您期待的那個返回一個不同的世界「。你能再次檢查我的編輯代碼,並幫助我找到我的? thnx ;-) – 2011-05-11 00:00:55

+0

@Master C - 你發佈的額外代碼似乎不包含任何問題......假設'canMove'邏輯是正確的(我不能判斷) – 2011-05-11 01:12:19

+0

我複製了canMove()方法從Actor類繼承的其他類(Food是Actor的子類),因爲其他類和Food都是從Actor繼承的,並且因爲canMove()可以正常工作,所以我認爲Food應該是相同的。 但我仍然不明白爲什麼它不起作用... – 2011-05-11 01:41:04