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

2013/5/31

Remove Object in world class

Nike.Sprite Nike.Sprite

2013/5/31

#
Hi I'm trying to remove the middle Wall from the horizontal grey bar in my world class. I tried the code removeObject but I'm getting an error. How can I remove an object at a specefic cell from a specefic Actor class in my world class? Here you can see a picture of my world, the image with the red circle must be removed: I hope someone can help me as fast as possible. Kind regards Nike Sprite ;)
public void vrijlatenGhost(int tijd) // laat een Ghost om de 10 vrij.
    {
        if (tijd == 10)
        {
// I WANT TO REMOVE THE WALL WITH THIS COORDINATES
            removeObject(12, 7, Wall.class); 

            greenGhost.setLocation(12, 6); 
                    }
        if (tijd == 20)
        {
            blueGhost.setLocation(12, 6);
        }
        if (tijd == 30)
        {
            redGhost.setLocation(12, 6);
        }
        if (tijd == 40)
        {
            pinkGhost.setLocation(12, 6);
        }
        if (tijd == 50)
        {
            orangeGhost.setLocation(12, 6);
        }
    }

davmac davmac

2013/5/31

#
Check the API documentation. The 'removeObject' method takes a single Actor argument - not an Actor class and co-ordinates as you have given it. You could try: removeObjects(getObjectsAt(12, 7, Wall.class)); If your code is inside an actor, you will need to use: getWorld().removeObjects(getWorld().getObjectsAt(12, 7, Wall.class));
Nike.Sprite Nike.Sprite

2013/5/31

#
Now it works, I already tried this:
removeObjects(getObjectsAt(12, 7, Wall.class));
I thought it was only one object so I didn't use the plural ?
Nike.Sprite Nike.Sprite

2013/5/31

#
Thanks (y)
davmac davmac

2013/6/1

#
I thought it was only one object so I didn't use the plural ?
The method getObjectsAt(...) returns a List. This may contain no objects, one object, or more than one object; its type is still List, regardless of what it contains. removeObject(...) requires an Actor parameter. A List is not an Actor, so you can't use 'removeObject' to remove it.
Nike.Sprite Nike.Sprite

2013/6/3

#
Hi This is in my world class as you can see this are 2 images, they are defined in my constructor of the Food Class. (2 images in 1 Class, they are NOT subclasses)
private Food smalldot = new Food ("smalldot");
    private Food bigdot = new Food ("bigdot");
Now I'm making a counter and I want to make a difference in points for the smalldot en the bigdot so I used the code:
public void gameWon()
    {
        int foodCount = getObjects(new Food("smalldot")).size(); 

// he says: method getObjects in class greenfoot.World cannot be applied to given types.

        if (foodCount == 0)
        {
            addObject(new Hulp(), getWidth()/2, getHeight()/2-2);
            Greenfoot.stop();
        }
    }
How can I solve this, I want to use the variable smalldot and bigdot in the getObjects() methode .... I do not want to make 2 subclasses (orders from school). I hope someone can help me !
You're supposed to insert a .class, not an object. This is what you want to do:
int foodCount = getObjects(Food.class).size();
But if you don't want to make two subclasses, you're going to have to go through each Food object to see if they have smalldot or bigdot. To solve this, you need to make a getDot(), getName() or whatever method that will return the String inserted at the beginning ("smalldot" or "bigdot") Then you can do this:
int foodCount = 0;

for (Actor a : getObjects(Food.class))
{
    if (((Food)a).getDot().equals("smalldot"))
        foodCount++;
}
I hope that helps!
Nike.Sprite Nike.Sprite

2013/6/3

#
Thanks for your quick answer How do I make the getDot(), getName() ? I first tried the Food.class method, but for my score counter I need to make a difference between them ;) Kind Regards Nike Sprite
the geDot()/getName() method would be as follows (I'm assuming you have a private String called dotSize that is equal to the parameter in the Food constructor):
public String getDot()
{
    return dotSize;
}
That's it. These methods are called getter/accessor methods (or so I was told in my AP Computer Science class). You use these methods when you want to another object or method to be able to get/access a private variable in another object, but not edit the variable.
Nike.Sprite Nike.Sprite

2013/6/4

#
No I haven't, I do not use dotSize, it's for a Pacman game, I have smallfood & bigfood, I need to give 10 points per smallfood and 50 points per bigfoot, but I do not know how to put it into my method... they do not recognize smallfood, because you cannot use it in the getObjects method.
davmac davmac

2013/6/5

#
"smallfood" and "bigfood" are two types of Food. There must be some method available in the Food class so you can distinguish the two types. What is the code for the Food class?
Nike.Sprite Nike.Sprite

2013/6/5

#
I have no code in my Food class ;) I acces bigfood & smallfood from my world class When pacman eats bigfood, the bigfood must be deleted, when he eats smallfood, the smallfood must be deleted..
private Food smallfood = new Food ("smallfood");  
    private Food bigfood = new Food ("bigfood");
davmac davmac

2013/6/5

#
I have no code in my Food class
You cannot have a class with no code at all. The Food class must have code in it. What is the code for the Food class?!!
When pacman eats bigfood, the bigfood must be deleted, when he eats smallfood, the smallfood must be deleted.
They are both Food, so just look for Food, and delete the Food object that you find.
Nike.Sprite Nike.Sprite

2013/6/6

#
there is no code in Food Class, just public Food() but there is nothing in it ;) But all my problems are solved & I already handed in my game, thanks for your time. Good night grtzzz Nike Sprite
You need to login to post a reply.