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

2019/4/18

Question with code (keydown)

CodeTime CodeTime

2019/4/18

#
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);
            }
        }
}
}
danpost danpost

2019/4/18

#
Replace your act method with the following:
private int wait = 0;
private int mX, mY;

public void act()
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if (mouse != null)
    {
        mX = mouse.getX();
        mY = mouse.getY();
    }
    if (Greenfoot.mouseClicked(null) && !Greenfoot.mouseClicked(this)) wait = 0;
    if (wait == 0 && Greenfoot.mouseClicked(this)) wait = 1;
    else if (wait == 1 && Greenfoot.isKeyDown("d"))
    {
        setLocation(mX, mY);
        wait = 2;
    }
}
Because the click and the key pressing happen at different times, the wait variable needs to be retained between act steps. Also, if no mouse action is detected, you may have getMouseInfo return a null value. Therefore, the location of the last detected mouse action must be retained between act steps as well -- hence lines 1 and 2. Lines 6 through 11 gets the location of the mouse when possible. Line 12 "deselects" the actor if a click is detected elsewhere (not on this actor). The rest processes your moving sequence. I am not sure why you have wait set to 2 after moving the actor, as setting it to zero would seem more appropriate.
CodeTime CodeTime

2019/4/25

#
danpost I am not sure why you have wait set to 2 after moving the actor, as setting it to zero would seem more appropriate.[/quote wrote...
This is late but thank you very much for the help. To answer your question, this is because soon after, I was planning for another function to run if wait was set to 2, but did not get to it as I had a problem with the part that I posted.
You need to login to post a reply.