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

2020/3/21

Clicking on the transparent part of an actor

Marcel_C Marcel_C

2020/3/21

#
Hello, Why does a click on the transparent part of the image count as a click on the image and how can I change this? I have already seen a similar question at https://www.greenfoot.org/topics/62839/0, but it does not answer my question exactly. I once tried to write a code where the colors of the clicked image are recognized at the location of the mouse pointer. Unfortunately the code does not work.
    
 MouseInfo info = Greenfoot.getMouseInfo();
            GreenfootImage img = getImage();
            Color color = new Color(0,0,0,0);
        if(Greenfoot.mouseClicked(this)){
            if (img.getColorAt(info.getX(),info.getY()).equals(color)) {
                
            } else {
                setLocation(getX()-70, getY());
                setImage("/Users/mc1/Desktop/Picture1.png");
            }
        }
Are there other approaches? How do I get it that a click on the transparent part of the image is no longer counted as a click on the image?
danpost danpost

2020/3/21

#
The given code does not work because the mouse x and y are with respect to the top left corner of the world -- not of the image of the actor. You need mouse coords minus the difference of actor location coords and half its image dimension sizes. This is assuming that the actor has zero rotation only.
Marcel_C Marcel_C

2020/3/21

#
First off, thanks for the quick answer. I need to admit that I've trouble implementing your approach.
 MouseInfo info = Greenfoot.getMouseInfo();
            System.out.println("");
            GreenfootImage img = getImage();
            Color color = new Color(0,0,0,0);
            int actorx = getX();
            int actory = getY();
            double dblex =info.getX();
            double dbley =info.getY();
            int mousex = (int)dbley;
            int mousey = (int)dbley;
            
        if(Greenfoot.mouseClicked(this)){
            if (img.getColorAt((mousex-(actorx-(1/2)*340)),(mousey-(actory-(1/2)*626))).equals(color)) {  
            } else {
                setLocation(getX()-70, getY());
                setImage("/Users/mc1/Desktop/GreenfootProjects/Spielebilder_Meto/Meto_Tuer_offen.png");
            }
        }
It still doesn't really work and I'm not sure if I completely understood the problem. Isn't there a more compact/effective solution or a different approach for my problem? I mean when I normally code with Java (Eclipse) I never had the problem that a click on the transparent part of the image has been counted as a click on the image...
Super_Hippo Super_Hippo

2020/3/21

#
My first guess is that (1/2) is 0 and not 0.5.
Marcel_C Marcel_C

2020/3/21

#
Why should 1/2 be 0?
danpost danpost

2020/3/21

#
The information that needs gathered, only if this actor is clicked on, is:
GreenfootImage image = getImage();
MouseInfo mouse = Greenfoot.getMouseInfo();
int mouseX = mouse.getX();
int mouseY = mouse.getY();
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
Color transparency = new Color(0, 0, 0, 0);
With this, the clicked pixel of the image (again, assuming actor has zero rotation) has coordinates:
int imageClickedAtX = mouseX-(getX()-imageWidth/2);
int imageClickedAtY = mouseY-(getY()-imageHeight/2);
Now, checking for click on non-transparent part would be:
if ( ! transparency.equals(image.getColorAt(imageClickedAtX, imageClickedAtY)))
Marcel_C Marcel_C

2020/3/21

#
Oh man thank you so much it works :D. I really appreciate your help!!
You need to login to post a reply.