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

2014/12/13

Dragging an object to a certain destination? Help

AnthonioBanderas AnthonioBanderas

2014/12/13

#
Hi, I am trying to drag an object to a certain point in the World with the left mouse hold. Before dragging it, it moves to the right. I already made the code where I am able to drag the object trough the world, but I want it to drag to a certain point, and when it is moved in that certain area, the object stops. Does anyone know how to do this? I can't find anything like this on the forum. This is the code so far (moving and dragging):
public class Bee extends Actor
{
   
    public void act() 
    {
        move(1);
       
        if (  Greenfoot.mouseDragged(this) ) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            this.setLocation(mouse.getX(), mouse.getY());
        }
    } 
}   
Any help is appreciated
Super_Hippo Super_Hippo

2014/12/13

#
You could compare the current x and y coordinates to a specified area while dragging and change a boolean then.
public class Bee extends Actor
{
    // x1,x2,y1,y2 are the corners of the area
    private boolean reachedArea =false
    public void act() 
    {
        move(1);
        
        if (  !reachedArea && Greenfoot.mouseDragged(this) ) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            this.setLocation(mouse.getX(), mouse.getY());
            if (getX() >= x1 && getX() <= x2 && getY() >= y1 && getY() <= y2) reachedArea = true;
        }
    } 
}
AnthonioBanderas AnthonioBanderas

2014/12/13

#
Super_Hippo wrote...
You could compare the current x and y coordinates to a specified area while dragging and change a boolean then.
public class Bee extends Actor
{
    // x1,x2,y1,y2 are the corners of the area
    private boolean reachedArea =false
    public void act() 
    {
        move(1);
        
        if (  !reachedArea && Greenfoot.mouseDragged(this) ) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            this.setLocation(mouse.getX(), mouse.getY());
            if (getX() >= x1 && getX() <= x2 && getY() >= y1 && getY() <= y2) reachedArea = true;
        }
    } 
}
Thank you. The code works, but not like it has to. If I now drag an object to the area, you can't drag it around anymore, but it still moves to the right. Do you know a solution? The object needs to stop instantly when it comes in the area. Thnx
danpost danpost

2014/12/13

#
This 'certain area' -- is there an actor object there or is it just a specified world location that can be described with (x1, y1)-(x2, y2)? Either way, I will imagine you have a method called 'checkLocation' to set the boolean state of the Bee being at the location or on the object (just for the sake of showing code):
private boolean beingDragged;
private boolean atTargetLocation;

public void act()
{
    if (atTargetLocation) return;
    if (!beingDragged)
    {
        move(1);
        if (Greenfoot.mousePressed(this)) beingDragged = true;
    }
    else
    {
        if (Greenfoot.mouseDragged(this))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if (mouse != null) setLocation(mouse.getX(), mouse.getY());
        }
        if (Greenfoot.mouseClicked(this) || Greenfoot.mouseDragEnded(this))
        {
            beingDragged = false;
            checkLocation(); //  sets 'atTargetLocation' field
        }
    }
}
The way I wrote it, the Bee must be dragged to the target location for it to stop there. Placing the Bee before it and having it move by itself to the location will not have it stop at the location. However, adding another 'checkLocation' call at line 10 would cause it to stop there. With the code Super_Hippo gave, the Bee will still move after being placed at the target location. Also, the Bee could move out from under the mouse if you stop moving the mouse without releasing the button; and releasing the button will not bring it back under the mouse. The fix to this requires the second boolean field 'beingDragged' to be added to the code to track the current dragging state (so that there is something to check to see if the Bee should move or not; the 'mouseDragged' method does not return true unless a button is down AND the mouse is moved).
AnthonioBanderas AnthonioBanderas

2014/12/13

#
danpost wrote...
This 'certain area' -- is there an actor object there or is it just a specified world location that can be described with (x1, y1)-(x2, y2)? Either way, I will imagine you have a method called 'checkLocation' to set the boolean state of the Bee being at the location or on the object (just for the sake of showing code):
private boolean beingDragged;
private boolean atTargetLocation;

public void act()
{
    if (atTargetLocation) return;
    if (!beingDragged)
    {
        move(1);
        if (Greenfoot.mousePressed(this)) beingDragged = true;
    }
    else
    {
        if (Greenfoot.mouseDragged(this))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if (mouse != null) setLocation(mouse.getX(), mouse.getY());
        }
        if (Greenfoot.mouseClicked(this) || Greenfoot.mouseDragEnded(this))
        {
            beingDragged = false;
            checkLocation(); //  sets 'atTargetLocation' field
        }
    }
}
The way I wrote it, the Bee must be dragged to the target location for it to stop there. Placing the Bee before it and having it move by itself to the location will not have it stop at the location. However, adding another 'checkLocation' call at line 10 would cause it to stop there. With the code Super_Hippo gave, the Bee will still move after being placed at the target location. Also, the Bee could move out from under the mouse if you stop moving the mouse without releasing the button; and releasing the button will not bring it back under the mouse. The fix to this requires the second boolean field 'beingDragged' to be added to the code to track the current dragging state (so that there is something to check to see if the Bee should move or not; the 'mouseDragged' method does not return true unless a button is down AND the mouse is moved).
Thanks for he effort of writing and explaining the code you just wrote! It is btw a specified world location that can be described with (x1, y1)-(x2, y2), not an obstacle. But 1 problem, when I try to compile, I am still missing the method called checkLocation. I have no idea how to fix this. I tried to make a method called checkLocation but it still doesnt work. I am new in programming so could you help me with this?
danpost danpost

2014/12/13

#
Well, it does not have to be a method, as I said before. You can just set the value depending on the current conditions at the line I placed the call to that 'imaginary' method; however, if you wanted to make a method of it, that would be fine (that is, it would run the same). The method, if you went ahead and wrote it would be like this:
private void checkLocation()
{
    atTargetLocation =
        getX() >= x1 &&
        getX() <= x2 &&
        getY() >= y1 &&
        getY() <= y2;
}
Basically, this is line 12 of the code Super_Hippo provided.
AnthonioBanderas AnthonioBanderas

2014/12/14

#
danpost wrote...
Well, it does not have to be a method, as I said before. You can just set the value depending on the current conditions at the line I placed the call to that 'imaginary' method; however, if you wanted to make a method of it, that would be fine (that is, it would run the same). The method, if you went ahead and wrote it would be like this:
private void checkLocation()
{
    atTargetLocation =
        getX() >= x1 &&
        getX() <= x2 &&
        getY() >= y1 &&
        getY() <= y2;
}
Basically, this is line 12 of the code Super_Hippo provided.
Thank you master
You need to login to post a reply.