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

2012/2/8

Need help with mouse info.

TechMaster3333 TechMaster3333

2012/2/8

#
I'm trying to write a method so that if the user mouses over my Actor, it switches to picture A, and then when they move their mouse away, it switches to picture B. This is the code I have written so far: public void act() { if(Greenfoot.mouseMoved(this)) { this.setImage("Play_button_selected.png"); } else if(...) { this.setImage("Play_button.png"); } } However, I'm not sure how to code the ... . I did also think of using the while statement, but I'm not sure which will work best. Help?!?
sp33dy sp33dy

2012/2/8

#
Quite simply, if you don't need to check a condition, just use:
if (condition) {
..blah;
} else {
}
The else will always fire if the mouseMoved(this) doesn't fire.
TechMaster3333 TechMaster3333

2012/2/8

#
Thanks!
TechMaster3333 TechMaster3333

2012/2/8

#
Not working :( public void act() { if(MouseInfo.getActor = this) { this.setImage("Play_button_selected.png"); } else { this.setImage("Play_button.png"); } } Error: cannot find symbol- variable getActor. I have tried to work it a different way, but to no avail.
kiarocks kiarocks

2012/2/8

#
use this:
if(Greenfoot.mouseMoved(this))
{
    overAnActor = true;
}
else overAnActor = false;
if(overAnActor)
{
//put your code here
}
else
{
//more code here
}
and put
public boolean overAnActor = false;
with other variables
davmac davmac

2012/2/9

#
Not working :( public void act() { if(MouseInfo.getActor = this)
"getActor" is a method, not a variable, so you call it by appending parentheses, ie: if(MouseInfo.getActor() = this)
danpost danpost

2012/2/9

#
* setImage(GreenfootImge image) takes an image, not a String * mouseMoved(this) can still return null while STOPPED over an actor Set up an instance boolean variable in your actor class, maybe called 'usingA', and set it to false. Then in your act method use:
if (Greenfoot.mouseMoved(null) // has mouse moved at all
{
    if (Greenfoot.mouseMoved(this) && !usingA) // if wrong picture for over this
    {
        setImage(new GreenfootImage("Play_button_selected.png");
        usingA =true;
    }
    if (!Greenfoot.mouseMoved(this) && usingA) // if wrong picture for not over this
    {
        setImage(new greenfootImage("Play_button.png");
        usingA = false;
    }
}
danpost danpost

2012/2/9

#
@davmac, why is your post of 1 hour ago (two up from this, unless somebody else posts here while I am writing this) showing only 10 minutes ago on the Activity page? A bit of a glitch, I'd say! That figures! It is gone now!
TechMaster3333 TechMaster3333

2012/2/9

#
danpost, would I have to initiate usingA? EDIT: Just re-read your post, and you did say...
sp33dy sp33dy

2012/2/9

#
Techmaster, Dan has stated: create usingA set to false. I.E :
private boolean usingA = false;
You need to login to post a reply.