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

2021/11/12

mouseClicked() does not work with getOneIntersectingObject()

Luvis Luvis

2021/11/12

#
Actor ziel = getOneIntersectingObject(Bälle.class);
if (Greenfoot.mouseClicked(ziel) && ((Welt) getWorld()).getMagazin().getMagazin() >= 1)
            {
                if(ziel != null)
                {
                    getWorld().removeObject(ziel);
                    ((Welt) getWorld()).getPunktestand().einPunkt();
                    ((Welt) getWorld()).getMagazin().geschossen();
                }
                if(ziel == null)
                {
                    ((Welt) getWorld()).getMagazin().geschossen();   
                }
}
Luvis Luvis

2021/11/12

#
If I click "ziel" with my mouse, it only does: ((Welt) getWorld()).getMagazin().geschossen(); So it either cannot trigger getWorld().removeObject(ziel); or it only uses line 10. Please help me ^^.
danpost danpost

2021/11/13

#
Luvis wrote...
If I click "ziel" with my mouse, it only does: ((Welt) getWorld()).getMagazin().geschossen(); So it either cannot trigger getWorld().removeObject(ziel); or it only uses line 10. Please help me ^^.
Are you sure that: (1) the actor of the class is touching a Bälle object; and (2) getMagazin return a value greater than 0; One way to be sure is if the point counter increases (line 7 executes). I presume it is a displayed value via a Punktestand actor. Both the above conditions must be satisfied for the actor removing and the counter incrementing to take place.
danpost danpost

2021/11/13

#
An aside: you code can be simplified to the following:
Actor ziel = getOneIntersectingObject(Bälle.class);
if (Greenfoot.mouseClicked(null) && ((Welt) getWorld()).getMagazin().getMagazin() >= 1)
{
    if (ziel != null)
    {
        getWorld().removeObject(ziel);
        ((Welt) getWorld()).getPunktestand().einPunkt();
    }
    ((Welt) getWorld()).getMagazin().geschossen();
}
Note the change in what is clicked on ('null' in line 2). This is essentially the same as what you originally had because any click anywhere would cause a shot to be fired, provided condition 2 above is met.
Luvis Luvis

2021/11/13

#
Thank you!
Luvis Luvis

2021/11/13

#
danpost wrote...
An aside: you code can be simplified to the following:
Actor ziel = getOneIntersectingObject(Bälle.class);
if (Greenfoot.mouseClicked(null) && ((Welt) getWorld()).getMagazin().getMagazin() >= 1)
{
    if (ziel != null)
    {
        getWorld().removeObject(ziel);
        ((Welt) getWorld()).getPunktestand().einPunkt();
    }
    ((Welt) getWorld()).getMagazin().geschossen();
}
Note the change in what is clicked on ('null' in line 2). This is essentially the same as what you originally had because any click anywhere would cause a shot to be fired, provided condition 2 above is met.
Now it works. Thanks a lot!
You need to login to post a reply.