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

2020/3/15

Move Actor to Location of click of mouse

1
2
APCSP-6 APCSP-6

2020/3/15

#
I'm working on a project that's sort of like Diner Dash and I really need some help on the code. I need the waitress to move to the location that the mouse was clicked. I am just now starting out so I do not have everything. I am kind of new to coding... my AP class doesn't get to code as much we're mostly on Edhesive so I need some insight please.
danpost danpost

2020/3/15

#
You will need fields to hold mouse click coordinates -- maybe:
1
private int targetX, targetY;
Use addedToWorld method to initialize settings:
1
2
3
4
5
protected void addedToWorld(World world)
{
    targetX = getX();
    targetY = getY();
}
In act:: -- for mouse clicking
1
2
3
4
5
6
if (Greenfoot.mouseClicked(null))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    targetX = mouse.getX();
    targetY = mouse.getY();
}
-- for movement (maybe)
1
2
3
4
5
if (targetX != getX() || targetY != getY())
{
    turnTowards(targetX, targetY);
    move(1);
}
APCSP-6 APCSP-6

2020/3/16

#
I tried the code but it isn't working, is there anything that I need to add in to the MyWorld?
danpost danpost

2020/3/16

#
APCSP-6 wrote...
I tried the code but it isn't working, is there anything that I need to add in to the MyWorld?
No. Please show what you tried (show class).
APCSP-6 APCSP-6

2020/3/17

#
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
private int targetX, targetY;
   public void act()
   
       GreenfootImage image = getImage(); 
       image.scale(50, 80);
       setImage(image); 
 
   }
 
   protected void addedToWorld(World world)
   {
       targetX = getX();
       targetY = getY();
   }
 
   public void isClicked()
   {
       if (Greenfoot.mouseClicked(null))
       {
           MouseInfo mouse = Greenfoot.getMouseInfo();
           targetX = mouse.getX();
           targetY = mouse.getY();
       }
   }
 
   public void isMoved()
   {
       if (targetX != getX() || targetY != getY())
       {
           turnTowards(targetX, targetY);
           move(1);
       }
   }
danpost danpost

2020/3/17

#
Change line 2 to:
1
public Waitress()
using the appropriate class name. All codes should be in that particular class. Add the following more appropriate act method:
1
2
3
4
5
public void act()
{
    isClicked();
    isMoved();
}
APCSP-6 APCSP-6

2020/3/17

#
When I entered in the code, the Waitress seemed to glitch out. And she's rotating or turning, I need her to just glide (?) to the click of the location... or for her to just not rotate.
danpost danpost

2020/3/18

#
APCSP-6 wrote...
When I entered in the code, the Waitress seemed to glitch out. And she's rotating or turning, I need her to just glide (?) to the click of the location... or for her to just not rotate.
Add to the end of act:
1
setRotation(0);
APCSP-6 APCSP-6

2020/3/18

#
Thank you, it stopped the rotating, but the Waitress is still glitching to and from the original spot to the click of the cursor. Is there any way to stop that?
danpost danpost

2020/3/18

#
APCSP-6 wrote...
Thank you, it stopped the rotating, but the Waitress is still glitching to and from the original spot to the click of the cursor. Is there any way to stop that?
Describe the "glitching"?
APCSP-6 APCSP-6

2020/3/18

#
When I click to a spot, the Waitress flicks back and forth between the spot clicked and the original spot the actor was in.
danpost danpost

2020/3/18

#
APCSP-6 wrote...
When I click to a spot, the Waitress flicks back and forth between the spot clicked and the original spot the actor was in.
I tried your code out (with the given changes) and do not get that glitching behavior. Show your entire class codes.
APCSP-6 APCSP-6

2020/3/18

#
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
import greenfoot.*; 
import java.util.*;
public class Waitress extends Actor
{
    private int speed;
    private int WaitressSpeed = -3;
    private int targetX, targetY;
    public void act()
    {
        isClicked();
        isMoved();
        setRotation(0);
    }
 
    public Waitress()
    
        GreenfootImage image = getImage(); 
        image.scale(50, 80);
        setImage(image); 
    }
 
    protected void addedToWorld(World world)
    {
        targetX = getX();
        targetY = getY();
    }
 
    protected void isClicked()
    {
        if (Greenfoot.mouseClicked(null))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            targetX = mouse.getX();
            targetY = mouse.getY();
        }
    }
 
    protected void isMoved()
    {
        if (targetX != getX() || targetY != getY())
        {
            turnTowards(targetX, targetY);
            move(100);
        }
    }
 
}
Yehuda Yehuda

2020/3/19

#
If you want the Waitress to move at a speed of 3 (waitressSpeed), in the isMoved method put move(3) instead of 100. Now it won't jump, but there is a different problem. The distance between targetX/Y and getX/Y might not be a multiple of 3. If that happens the Waitress will constantly be jumping 3 pixels in each direction as they're != to each other. Danpost said to move(1) which would avoid this problem, but is a slower movement for the set execution speed. P. S. The last two methods should be "private", not "protected".
APCSP-6 APCSP-6

2020/3/19

#
1
2
3
4
5
private void addedToWorld(World world)
    {
        targetX = getX();
        targetY = getY();
    }
The code "addedToWorld(World world)" is saying that there is an error and that it cannot override the one in Greenfoot or something like that.
There are more replies on the next page.
1
2