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

2016/10/28

Determining the Actor subclass of mouseInfo.getActor()

tonyreks tonyreks

2016/10/28

#

Here's the problem:

    public void act() 
    {
        if(Greenfoot.mouseClicked(null) == true){
            if(Greenfoot.getMouseInfo() == null){return;}
                Pixel p = (Pixel)Greenfoot.getMouseInfo().getActor();
                p.turnRed();
        }
    }    
Inside a "Canvas" actor, I look for the user clicking on grid of "pixel" actors inside the canvas. Using Greenfoot.mouseClicked(Pixel.class) didn't work for some reason, so on line 3, you can see that I just used null; thus, any click in the world satisfies the if-statement. Going further, we see that I make sure that we actually get a MouseInfo object, and finally, I take the Pixel.class object that (presumably) has been clicked, and turn it red. HOWEVER, I run into quite obvious problems if I try to click anything BUT a Pixel.class object (casting amiright). Here are the options I see; get Greenfoot.MouseClicked(Pixel.class) to work -or- Find a way to determine if Greenfoot.getMouseInfo().getActor() is holding a Pixel.class object I would love an explanation for either; understanding why or why not this works would be really great.
davmac davmac

2016/10/28

#
Documentation for Greenfoot.mouseClocked():
True if the mouse has been clicked (pressed and released) on the given object. If the parameter is an Actor the method will only return true if the mouse has been clicked on the given actor. If there are several actors at the same place, only the top most actor will receive the click. If the parameter is a World then true will be returned if the mouse was clicked on the world background. If the parameter is null, then true will be returned for any click, independent of the target clicked on
So you see, the parameter must be an actor or the world; you gave it a class (albeit an actor subclass) instead. One way to have it work as you intended is for each Pixel to check whether they have been clicked, i.e. insert into the Pixel class' act method:
if (Greenfoot.mouseClicked(this)) {
    // something here
}
The other option is to use instanceof to check the type of object that has been clicked:
public void act() 
{
    if(Greenfoot.mouseClicked(null) == true){
        if(Greenfoot.getMouseInfo() == null){return;}
            Actor a = Greenfoot.getMouseInfo().getActor();
            if (a instanceof Pixel) {
                Pixel p = (Pixel)a;
                p.turnRed();
            }
    }
}
By the way, you don't need to write "== true" in a condition. Just the following is fine:
public void act() 
{
    if(Greenfoot.mouseClicked(null)){
        if(Greenfoot.getMouseInfo() == null){return;}
            Actor a = Greenfoot.getMouseInfo().getActor();
            if (a instanceof Pixel) {
                Pixel p = (Pixel)a;
                p.turnRed();
            }
    }
}
"== true" won't change the truth value; if it is already true, the result of the comparison is true; if it is false, the result of the comparison is false. (And as a final note, your indentation is odd; I didn't want to change it because I wanted it to correspond better to your original code).
tonyreks tonyreks

2016/10/28

#
Thanks so much for the clarification! I think I got confused because the docs didn't make it very clear how it was supposed to be used; I assumed it was supposed to be applied in a more general sense, but it seems that it is intended for pretty much a self-referencing Actor using "this". At that point, I feel like there might as well be an Actor method mouseClickedThis()
danpost danpost

2016/10/29

#
tonyreks wrote...
At that point, I feel like there might as well be an Actor method mouseClickedThis()
The method can not only be used for specific Actor objects, but also the World object; and it can also be used generically, where it does not matter what was clicked on, only that a click was detected. I often use it for specific Actor object where the code is placed in a World subclass. It is also used for cases where one wishes to detect when a mouse button was released (in situations where the pressing of the mouse is either unimportant or dealt with separately).
You need to login to post a reply.