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

2018/1/10

Can somebody please explain me this code

menshave menshave

2018/1/10

#
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)
 
 
public class Spieler extends Actor
{
    private int targetx=0, targety=0;
    private int abstand=0;
 
    public void addedToWorld(World MyWorld)
    {
        targetx=getX();
        targety=getY();
    }
     
    public void move()
    {
       double rx=targetx-getX();
       double ry=targety-getY();
       double r=Math.sqrt(rx*rx+ry*ry);
       int bts=10;
       int posx=0,posy=0;
       if(r>bts)
       {
          posx=(int)(getX()+bts*rx/r);
          posy=(int)(getY()+bts*ry/r);
        }
        else
        {
            posx=targetx;
            posy=targety;
        }
        setLocation(posx,posy);
    }
     
    public void act()
    {
      if(Greenfoot.mouseMoved(null))
      {
          MouseInfo mouse=Greenfoot.getMouseInfo();
          targetx=mouse.getX();
          targety=mouse.getY();
      }
danpost danpost

2018/1/10

#
It looks like the player is to quickly gravitate toward wherever the mouse happens to be. It is beyond me why the 'move' method is not called within the class.
menshave menshave

2018/1/10

#
I have no idea what that means. I must explain it on a presentation tomorrow. Can you pleas help me danpost.
menshave menshave

2018/1/10

#
danpost wrote...
It looks like the player is to quickly gravitate toward wherever the mouse happens to be. It is beyond me why the 'move' method is not called within the class.
can you please explain me what that means?
danpost danpost

2018/1/10

#
The act method has the target coordinates adjusted to wherever the mouse moves to. The addedToWorld method prevents the actor from zipping to the upper-left corner, location (0, 0), after it is placed into the world and before any mouse location coordinates are acquired. The move method has the actor move toward or placed at the target location (if it is ever called). The 'abstand' field is inconsequential (not used anywhere within the class). Here's a little more detail about the 'move' method. It first determine the straight line distance, 'r', to the target location (lines 17 through 19). Then, if the distance is greater than 10 units, it moves the actor toward the target location; otherwise, if within 10 units, places the actor at the target location.
menshave menshave

2018/1/10

#
Thank you for answer so quick, that really helped me to understand it. But I haven`t understand it completly. Can you please explain me what if(Greenfoot.mouseMoved(null)) means. I appreciate that you are so reliable
menshave menshave

2018/1/10

#
Danke wrote...
The act method has the target coordinates adjusted to wherever the mouse moves to. The addedToWorld method prevents the actor from zipping to the upper-left corner, location (0, 0), after it is placed into the world and before any mouse location coordinates are acquired. The move method has the actor move toward or placed at the target location (if it is ever called). The 'abstand' field is inconsequential (not used anywhere within the class). Here's a little more detail about the 'move' method. It first determine the straight line distance, 'r', to the target location (lines 17 through 19). Then, if the distance is greater than 10 units, it moves the actor toward the target location; otherwise, if within 10 units, places the actor at the target location.
So what is targetx and y on the method move(). Is it the coordinate from the mouse?
danpost danpost

2018/1/10

#
menshave wrote...
explain me what if(Greenfoot.mouseMoved(null)) means.
It asks if the mouse has moved. If it has, then a MouseInfo object is accessible, Since the location of the mouse is crucial here, this is the correct method to use for that determination. The MouseInfo object, in turn will contain the new location of the mouse.
what is targetx and y on the method move(). Is it the coordinate from the mouse?
Initially, it is the location of the actor (via the 'addedToWorrld' method). Once movement of the mouse is detected and from then on, it is the last known location of the mouse.
You need to login to post a reply.