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

2012/7/11

Check for dragging mouse on object without the drag having to be started nor ended on it

MrDoomsday MrDoomsday

2012/7/11

#
As the title says I want to check for the mouse being dragged on an actor object without the drag having to be started nor ended on that object. In other words it has to return true whenever the mouse appears on that object already in a pressed state. I tried to combine some of the mouse booleans from greenfoot but I failed. Is there any convenient way of doing this?
SPower SPower

2012/7/11

#
You can use this method:
if (Greenfoot.mouseDragged(this)) {
   // the mouse dragged over this object
}
This code would probably go in your act method, or in a method called from act.
MrDoomsday MrDoomsday

2012/7/11

#
Did you even read what I said ? -.- This does NOT work because in order for this to work the drag must have been STARTED on that object.
SPower SPower

2012/7/11

#
I thought you meant something else, I'm sorry. For that, create an instance variable:
private boolean draggingStarted;
And put this inside the if statement I gave you:
if (!draggingStarted) {
    draggingStarted = true;
} else {
    // do whatever you want
}
And add this else case after the if statement I gave you:
else {
   draggingStarted = false;
}
SPower SPower

2012/7/11

#
I'm sorry: forget everything of my last post. This is the good code: Create an instance variable:
private boolean draggingStarted = true;
The if statement:
if (Greenfoot.mouseDragged(this)) {
   if (!draggingStarted) {
       // do what you want
   } else {
       draggingStarted = false;
   }
} else {
   draggingStarted = true;
}
MrDoomsday MrDoomsday

2012/7/11

#
Hmm .. still doesn't work correctly for me :( The drag still needs to be started on the object.
SPower SPower

2012/7/11

#
Do you want 'dragging starting' to last longer than now? Because it now will take a very short time..
danpost danpost

2012/7/11

#
The only way to see if the mouse dragging cross over another object is to check it programatically. Try the following:
if (Greenfoot.mouseDragged(null) && mouseOnObject(obj)) // do something
// with the following method
private boolean mouseOnObject(Actor obj)
{
    MouseInfo mi = Greenfoot.getMouseInfo();
    int actorX = obj.getX(), actorY = obj.getY();
    int actorWide = obj.getImage().getWidth(), actorHigh = obj.getImage().getHeight();
    boolean ck1 = mi.getX() >= actorX - actorWide / 2;
    boolean ck2 = mi.getY() >= actorY - actorHigh / 2;
    boolean ck3 = mi.getX() <= actorX + actorWide / 2;
    boolean ck4 = mi.getY() <= actorY + actorHigh / 2;
    return ck1 && ck2 && ck3 && ck4;
}
You need to login to post a reply.