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

2014/4/2

How to click on an actor and remove it from the world

1
2
qwertyuiop qwertyuiop

2014/4/2

#
Basically when my mouse is over an image of a target I want to be able to click and remove the target. Any help would be appreciated, thanks.
danpost danpost

2014/4/2

#
What are your thoughts as far as what methods you might need and where you would need to put the code?
qwertyuiop qwertyuiop

2014/4/2

#
Well I have tried to use an if statement saying that if the mouse and target collide and the left mouse button is pressed it should remove the object however I don't think I did that right either.
danpost danpost

2014/4/2

#
qwertyuiop wrote...
Well I have tried to use an if statement saying that if the mouse and target collide and the left mouse button is pressed it should remove the object however I don't think I did that right either.
Where did you try it? What 'it' should remove the object? What exactly do you mean by 'mouse and target collide'?
trias95 trias95

2014/4/2

#
If you are just wanting to remove it from the world you could use the following code
1
2
3
4
5
6
if (Greenfoot.mouseClicked(this)) {
       World world;
       world = getWorld();
       world.removeObject(this);
       return;     
   }
That code could go inside your act method inside the "target"'s code. or if you would like to do it neater, you can set your target's code out like so.
1
2
3
4
5
6
7
8
9
10
11
12
13
public void act()
    {
        removeOnClick();
}
public void removeOnClick()
{
 if (Greenfoot.mouseClicked(this)) {
        World world;
        world = getWorld();
        world.removeObject(this);
        return;
    }   
}
qwertyuiop qwertyuiop

2014/4/2

#
I added that to my target code and nothing happens when I click on them. Any ideas why?
qwertyuiop qwertyuiop

2014/4/2

#
I changed:
1
if (Greenfoot.mouseClicked(this))
to...
1
if (Greenfoot.mouseClicked(null))
and it works but I have 10 objects on the screen and I only want to remove the one I am hovering over.
danpost danpost

2014/4/2

#
If you have an actor that follows the mouse, then clicks will not be registered on your targets (only on that actor that follows the mouse).
qwertyuiop qwertyuiop

2014/4/2

#
So is there anyway to remove the target object when the mouse is firstly hovering over a target object and when it is clicked from the actor that follows the mouse (which is called "Aim")?
danpost danpost

2014/4/2

#
How about trying:
1
if (Greenfoot.mouseClicked(null) && !getIntersectingObjects(Aim.class).isEmpty()) ...
or if you need a smaller hit area:
qwertyuiop qwertyuiop

2014/4/2

#
That works, but I can hit some targets from outside the actual area of the object itself.
danpost danpost

2014/4/2

#
danpost wrote...
How about trying:
1
if (Greenfoot.mouseClicked(null) && !getIntersectingObjects(Aim.class).isEmpty()) ...
or if you need a smaller hit area:
1
if (Greenfoot.mouseClicked(null) && !getObjectsAtOffset(0, 0, Aim.class).isEmpty()) ...
Sorry, the second code part did not show on my last post.
qwertyuiop qwertyuiop

2014/4/2

#
Ahhh thought so, no problem I will try it now thanks.
qwertyuiop qwertyuiop

2014/4/2

#
This works just fine. Thanks for your time.
danpost danpost

2014/4/2

#
You may need to adjust the image of your Aim object when over a target (make it smaller -- or just an invisible point); or you could prevent the Aim object from moving over a target (put a condition on following the mouse) so the click will register on the target itself. EDIT: ignore this if you are satisfied with what you have.
There are more replies on the next page.
1
2