Could someone please help me with making an object move when you click and drag it along.
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
}/* 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;
}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;
}
}public void act()
{
// lines 14 through 41
}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;
}
}
}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);
}
}