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

2013/3/2

Clicking on an object

1
2
3
Gingervitis Gingervitis

2013/3/2

#
If I wanted to click on an object to change worlds, where would I have to put the code? Would it be in the world class or actor class?
Gevater_Tod4711 Gevater_Tod4711

2013/3/2

#
I would put the code into an actor class. The code for this should look like this:
1
2
3
4
5
6
//in the object you want to click to change the world;
public void act() {
    if (Greenfoot.mouseClicked(this)) {
        Greenfoot.setWorld(new NextWorld());
    }
}
Gingervitis Gingervitis

2013/3/2

#
Thank you. I have one more question related to the mouse. Is there a way to make it so when the mouse cursor gets to that object before it is clicked, it will be transparent? I know there is a setTransparency method but how could I implement that to do what I want to do?
Gevater_Tod4711 Gevater_Tod4711

2013/3/2

#
If you just want your actor to become transparent it's simple. Use this method:
1
2
3
4
5
private boolean mouseOnObject(Actor actor) {
    MouseInfo mouse = Greenfoot.getMouseInfo();
    return mouse != null && mouse.getX() > actor.getX() - actor.getImage().getWidth()/2 && mouse.getX() < actor.getX() + actor.getImage().getWidth()/2 &&
        mouse.getY() > actor.getY() - actor.getImage().getHeight()/2 && mouse.getY() < actor.getY() + actor.getImage().getHeight()/2;
}
then you have to check whether the mouse is on this image like this:
1
2
3
4
5
6
7
8
9
//in the class that should be transparent;
public void act() {
    if (mouseOnObject(this)) {
        setTransparenzy(0);
    }
    else {
        setTransparenzy(255);
    }
}
You need the method in the class with this code then it should work.
danpost danpost

2013/3/2

#
Or, more simply:
1
2
if (Greenfoot.mouseMoved(this)) setTransparency(0);
if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this)) setTransparency(255);
Gingervitis Gingervitis

2013/3/2

#
I didn't know there was a '.mouseMoved()'. Where was that?
Gevater_Tod4711 Gevater_Tod4711

2013/3/2

#
But this method only works if the mouse is moved on the object and not if it just is standing still on it or not?
danpost danpost

2013/3/2

#
Gevater_Tod4711 wrote...
But this method only works if the mouse is moved on the object and not if it just is standing still on it or not?
This is not true. When it moves on the object, it becomes (or stays) transparent. If it stops on the object, it stays transparent. If it moves off the object, it becomes (or stays) opaque. If it is stopped off the object, it stays opaque. Try it out.
Gingervitis Gingervitis

2013/3/2

#
It said that setTransparency() is not a method. Does it have to be defined somewhere else?
danpost danpost

2013/3/2

#
Gingervitis wrote...
It said that setTransparency() is not a method. Does it have to be defined somewhere else?
I did not think about that when I was re-doing Gevater_Todd4711's code. It should be
1
2
if (Greenfoot.mouseMoved(this)) getImage().setTransparency(0);
if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this)) getImage().setTransparency(255);
Gingervitis Gingervitis

2013/3/2

#
Thank you. I had feeling that there would need a 'something.' in front of it. . .
Gevater_Tod4711 Gevater_Tod4711

2013/3/2

#
Oh ok that would work. But the method mouseMoved() just checks whether the mouse is realy moved doesn't it?
Gingervitis Gingervitis

2013/3/2

#
I just put in the code in my class exactly as you posted, and the transparency stayed the same. Is it because what I want transparent is text? (I wrote the text in Preview and it was not created from Greenfoot labels)
danpost danpost

2013/3/2

#
Yes. The 'mouseMoved' method just checks whether the mouse has been moved or not. But the only time you need to check whether it is over the actor or not is when the mouse has been moved. And, the only time the transparency needs changed is when the mouse is moved onto or off of the actor. So, the first 'if' statement covers all movements where the mouse ends up over the object; and the second one covers all movements where the mouse does not end up over the object.
danpost danpost

2013/3/2

#
Please show the class code for that actor the text is wrote on.
There are more replies on the next page.
1
2
3