So I have a code that should make an object move to another place when it is clicked, then you put your mouse over another location, press d and it moves it there. However, there seems to be a problem with recording the coordinates of the new position. Rather than record the place where the mouse is when you press "d", it documents the location where you clicked when you clicked on the object. Is there a way for fix this?
Code for reference
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; public class wPawn extends Actor { public wPawn() { GreenfootImage image = getImage(); image.scale(90,90); setImage(image); } public void act() { MouseInfo mouse=Greenfoot.getMouseInfo(); int wait = 0; int xpos = getX(); int ypos = getY(); float mX = 0; float mY = 0; if (Greenfoot.mouseClicked(this)) { wait = 1; //checks if object is clicked } while (wait == 1) { if (Greenfoot.isKeyDown("d")) { mX = mouse.getX(); //should get location only when "d" is pressed mY = mouse.getY(); //however, it get the location where the mouse pressed when I clicked on the object wait = 2; System.out.println(mX); System.out.println(mY); } } } }