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

2016/3/16

Why my code is removing 2 objects first and then 1 by 1?

heitorpereira heitorpereira

2016/3/16

#
When i click on "-", greenfoot must remove 1 object per click. But on the first click, 2 objects are removed, and than 1 per click. I have no ideia what the problem is.
public void act() {
        if (Greenfoot.mouseClicked(addButton))
        {
            MouseInfo mouseInfo = Greenfoot.getMouseInfo();
            x += 30;
            
            addObject(new Pessoa(), x, ALTURA/2);
        }
        if (Greenfoot.mouseClicked(removeButton)) {
            MouseInfo mouseInfo = Greenfoot.getMouseInfo();
            x -= 10;
            removeObjects(getObjectsAt(x, ALTURA/2, Pessoa.class));
            
        }

    }
abelbarradas abelbarradas

2016/3/16

#
I'm having the same problem. Anyone??
danpost danpost

2016/3/16

#
If you only want to remove one object, use 'removeObject' instead of 'removeObjects'. You may have to check to see if 'getObjects' returns a list that is not empty before extracting an element from it.
You need to login to post a reply.