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

2015/4/5

Buttons removing blocks at a distance

Ned_Chapers Ned_Chapers

2015/4/5

#
Hey, i would love some help on a Greenfoot problem i have. Basically I am making a platformer and want to have a button at the top right which when i touch removes an actor at the other side of the world (Not intersectingObject because they arent touching.) I can remove objects when they are touching using getOneIntersectingObject however these two arent touching. What should I do?
danpost danpost

2015/4/5

#
If each button you touch removes just one block, then you can have each button hold a reference to the block it is to remove:
// in Button class
private Block block; // the reference

public Button(Block block)
{
    this.block = block;
    // whatever else you might have had in 'public Button()'

public void act()
{
    if (block != null && isTouching(Player.class))
    {
        getWorld().removeObject(block);
        block = null;
    }
}
Then, in the world, when creating the actors, create the blocks first and then create the buttons. Use something like this:
Block block = new Block();
addObject(block, /** wherever */);
Button button = new Button(block); // this button will remove the previous block
addObject(button, /** wherever */);
// for additional buttons
block = new Block();
addObject(block, /** wherever */);
button = new Button(block); // this button will remove the previous block
addObject(button, /** wherever */);
You need to login to post a reply.