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


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 } |
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 ; } |
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 ; } } |
1 2 3 4 | public void act() { // lines 14 through 41 } |
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 ; } } } |
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); } } |