开发者

Problem removing objects in Java

Lets say a graphic World,lets say an API of a World, and Actor, and I built an object from new class name Food that inherent from Actor but in certain situations I need the object to be disappeared from my world. what should be a good way to do so ?

I tried this:

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

But it didn't killed any kind of object from Class that inherent from Food... why ?

I wrapped it (in the 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 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. 
    }

But the object did not disappeared... why ?

Thanks !!

========================================================================================== EDIT: canMove method & Mushroom Class

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


I assume that your code looks like 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 the face of it, that should work.

Possible reasons why a cheezeburger doesn't get removed include:

  • your code isn't actually calling act() on the cheezeburger,
  • getWorld() returns a different world to the one you are expecting,
  • World.removeObject(...) is not working properly,
  • the Cheezeburger class (or a superclass) has overridden act or canMove or killFood and the override method(s) do the wrong thing.

All of these scenarios can be summarized as "the bug is somewhere else".


Assuming that you've inspected your code and cannot find the problem, the next step should be to run it using a debugger, and single step it through the code that is not working to see what is really happening.


It looks the getWorld().removeObject(this) will only remove a specific instance from your world. It won't remove all instances of a specific class from your world.

You need to call getWorld().removeObject on each specific instance of a class that you want to remove.

So to see how this works try something like this:

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. 

Anyways, I think from your questions, that you are confusing instances of a class with the class itself.

This is of course assuming that removeObject works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜