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

2013/4/5

mouse Help

GrimCreeper312 GrimCreeper312

2013/4/5

#
i want to have it so that when the mouse is hovering over Button5 the button adds a Barrier to the location 497, 205. please help
danpost danpost

2013/4/5

#
You will need a boolean to track the hovering state; it will be something like this:
// add instance field
private boolean isHovering; // default is false
// in act or method it calls
if (!isHovering && Greenfoot.mouseMoved(this) && getWorld().getObjectsAt(497, 205, Barrier.class).isEmpty())
{
    isHovering = true;
    getWorld.addObject(new Barrier(), 497, 205);
}
if (isHovering && !Greenfoot.mouseMoved(this))
{
    isHovering = false;
    // add the following if barrier disappears when mouse no longer hovers over button
    getWorld().removeObjects(getWorld().getObjectsAt(497, 205, Barrier.class));
}
GrimCreeper312 GrimCreeper312

2013/4/5

#
is there a way to have it so that the mouse doesn't have to move continouslly over the button to work
danpost danpost

2013/4/5

#
How exactly do you want it to work? When is the Barrier object supposed to be present and not present? Like -- what do you want to happen when the mouse first arrives over the button? while it is moved over the button? and when it is moved off the button?
GrimCreeper312 GrimCreeper312

2013/4/5

#
i want it to add a barrier when the mouse is over the button and remove it when the mouse isn't your code works fine but i have to move the mouse continiouslly to have the barrier stay
danpost danpost

2013/4/5

#
Sorry, try changing line 9 to the following:
if (isHovering && Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))
You need to login to post a reply.