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

2015/5/10

Shooting

purplefoot purplefoot

2015/5/10

#
Im trying to make it so the view is crosshairs(an actor), and the crosshairs follow your mouse, and you are meant to shoot an actor which is a deer. but i want it so the way to shoot them is to click on them and the use getWorld().removeObject(this). However, im not sure how to do this because clicking on them wont work as it only clicks on the crosshairs which are infront of the deer.
danpost danpost

2015/5/10

#
Have the crosshairs detect the click and if it intersects a deer, remove it.
purplefoot purplefoot

2015/5/10

#
how would i do that (sorry ive only just got greenfoot...)
Super_Hippo Super_Hippo

2015/5/10

#
//in the crosshair class
public void act()
{
    if (Greenfoot.mousePressed(this))
    {
        Actor deer = getOneIntersectingObject(Deer.class);
        if (deer != null)
        {
            removeObject(deer);
        }
    }
}
danpost danpost

2015/5/10

#
Or more simply:
public void act()
{
    if (Greenfoot.mouseClicked(this) && isTouching(Deer.class)) removeTouching(Deer.class);
}
As an alternative to this way, you could put the following in your world class constructor:
setPaintOrder(Deer.class);
and then the deer will detect clicks on themselves without the crosshairs being in the way.
You need to login to post a reply.