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

2012/12/30

Dragging with the mouse

behappy2 behappy2

2012/12/30

#
Could someone please help me with making an object move when you click and drag it along.
nooby123 nooby123

2012/12/30

#
First make a variable that determines if it is dragging or not.
1
2
3
4
5
6
7
8
boolean drag = false;
 
if(Greenfoot.mousePressed(this)) drag = true;
if(Greenfoot.mouseClicked(null)) drag = false;
 
if(drag) {
        // what ever you want to put in here
}
This'll work.
danpost danpost

2012/12/30

#
The following code will allow an object to be dragged by the mouse without having it go under other objects during the dragging procedure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* add an instance boolean field to the class of the Actor object to be dragged */
private boolean isGrabbed;
 
/* in the 'act' method in the class of the Actor object to be dragged */
// check for initial pressing down of mouse button
if (Greenfoot.mousePressed(this) && !isGrabbed)
{
    // grab the object
    isGrabbed = true;
    // the rest of this block will avoid this object being dragged UNDER other objects
    World world = getWorld();
    MouseInfo mi = Greenfoot.getMouseInfo();
    world.removeObject(this);
    world.addObject(this, mi.getX(), mi.getY());
    return;
}
// check for actual dragging of the object
if ((Greenfoot.mouseDragged(this)) && isGrabbed)
{
    // follow the mouse
    MouseInfo mi = Greenfoot.getMouseInfo();
    setLocation(mi.getX(), mi.getY());
    return;
}
// check for mouse button release
if (Greenfoot.mouseDragEnded(this) && isGrabbed)
{
    // release the object
    isGrabbed = false;
    return;
}
behappy2 behappy2

2012/12/30

#
aha, so the world recreates the object, but where the mouse is! BUT it says --> if (Greenfoot.mousePressed(this) && !isGrabbed) is 'an illegal start of type'
danpost danpost

2012/12/30

#
You will have to show your class code (you probably put it in the wrong place and do not have your bracketing set proper).
behappy2 behappy2

2012/12/30

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class something here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class something extends Actor
{
    /* add an instance boolean field to the class of the Actor object to be dragged */
private boolean isGrabbed;
 
/* in the 'act' method in the class of the Actor object to be dragged */
// check for initial pressing down of mouse button
if (Greenfoot.mousePressed(this) && !isGrabbed)
{
    // grab the object
    isGrabbed = true;
    // the rest of this block will avoid this object being dragged UNDER other objects
    World world = getWorld();
    MouseInfo mi = Greenfoot.getMouseInfo();
    world.removeObject(this);
    world.addObject(this, mi.getX(), mi.getY());
    return;
}
// check for actual dragging of the object
if ((Greenfoot.mouseDragged(this)) && isGrabbed)
{
    // follow the mouse
    MouseInfo mi = Greenfoot.getMouseInfo();
    setLocation(mi.getX(), mi.getY());
    return;
}
// check for mouse button release
if (Greenfoot.mouseDragEnded(this) && isGrabbed)
{
    // release the object
    isGrabbed = false;
    return;
}
}
this is all my code
behappy2 behappy2

2012/12/30

#
and this is something
danpost danpost

2012/12/30

#
You need to enclose everything from line 14 on inside the 'act' method:
1
2
3
4
public void act()
{
    // lines 14 through 41
}
behappy2 behappy2

2012/12/30

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class something here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class something extends Actor
{
    /* add an instance boolean field to the class of the Actor object to be dragged */
private boolean isGrabbed;
 
/**
 *
 */
public void act()
{
   /* in the 'act' method in the class of the Actor object to be dragged */
   // check for initial pressing down of mouse button
  if (Greenfoot.mousePressed(something) && !isGrabbed)
 {
    // grab the object
    isGrabbed = true;
    // the rest of something block will avoid something object being dragged UNDER other objects
    World world = getWorld();
    MouseInfo mi = Greenfoot.getMouseInfo();
    world.removeObject(something);
    world.addObject(something, mi.getX(), mi.getY());
    return;
}
// check for actual dragging of the object
if ((Greenfoot.mouseDragged(something)) && isGrabbed)
{
    // follow the mouse
    MouseInfo mi = Greenfoot.getMouseInfo();
    setLocation(mi.getX(), mi.getY());
    return;
}
// check for mouse button release
if (Greenfoot.mouseDragEnded(something) && isGrabbed)
{
    // release the object
    isGrabbed = false;
    return;
}
}
}
my if (Greenfoot.mousePressed(something) && !isGrabbed) doesnt find symbol- variable something
behappy2 behappy2

2012/12/30

#
do you have a dragging scenario that i could try out and edit into my own scen.?
nooby123 nooby123

2012/12/30

#
You can check out my scenario , but this is a smooth moving drag.
vonmeth vonmeth

2012/12/30

#
You shouldn't be replacing "this" with "something."
behappy2 behappy2

2012/12/30

#
ok
ZoeF ZoeF

2016/11/2

#
Ok i tryed to follow this as i kinda needed something similar. But i get a nullpointer exception on line 27. Any help and especialy explenation as to why would be realy appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import greenfoot.*;
 
public class Cannon extends Towers
{
    private String cannonType;
    private Boolean isGrabbed;
     
    public Cannon()
    {
        this("0");
    }
     
    public Cannon(String newCannon)
    {
        setCannon(newCannon);
    }
     
    private void setCannon(String newCannon)
    {
        cannonType = newCannon;
    }
     
    public void act()
    {
        super.act();
        loadImage();
        if (Greenfoot.mousePressed(this) && !isGrabbed)
        {
            // grab the object
            isGrabbed = true;
            // the rest of this block will avoid this object being dragged UNDER other objects
            World world = getWorld();
            MouseInfo mi = Greenfoot.getMouseInfo();
            world.removeObject(this);
            world.addObject(this, mi.getX(), mi.getY());
            return;
        }
        // check for actual dragging of the object
        if (Greenfoot.mouseDragged(this) && isGrabbed)
        {
            // follow the mouse
            MouseInfo mi = Greenfoot.getMouseInfo();
            setLocation(mi.getX(), mi.getY());
            return;
        }
        // check for mouse button release
        if (Greenfoot.mouseDragEnded(this) && isGrabbed)
        {
            // release the object
            isGrabbed = false;
            return;
        }
    }
 
    private void loadImage()
    {
        GreenfootImage myCannon = new GreenfootImage("towers/cannon" + cannonType + ".png");
        setImage(myCannon);
    }
}
danpost danpost

2016/11/2

#
The field type on line 6 should be 'boolean' -- with a lowercase 'b'. Using an uppercase 'B' will have the field treated as an Object field reference of type Boolean instead of as a primitive type boolean field. As an object reference, its default value is 'null'; as a primitive field, it is assigned a value of 'false' by default. Also, all your multi-line comments should begin with '/**', using two asterisks. That way, at least in the postings here, some of your code lines will not look like comment lines because of their text color.
You need to login to post a reply.