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

2014/9/3

How do you display the "x" and "y" of an actor?

ILOVELABRADORS ILOVELABRADORS

2014/9/3

#
Hi, Can anyone tell me how you are supposed to display the text of an actor's "x" and "y" of an actor? I know how to display text, but I don't know how to make it change as you move an actor to a different place. Thanks in advance...
danpost danpost

2014/9/3

#
Create an actor to display the initial text and change its image text anytime the actor changes position. Coded in the class of the actor you wish to track:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// instance field
Actor coordsActor;
// method to add actor into world
public void addedToWorld(World world)
{
    coordsActor = new Actor(){};
    world.addObject(coordsActor, /* x  and y values */);
    updateCoords();
}
// method to update image of coords actor
public void updateCoords()
{
    String text = "X: "+getX()+"\nY: "+getY();
    GreenfootImage image = new GreenfootImage(text, 24, null, null);
    coordsActor.setImage(image);
}
// or just
public void updateCoords()
{
    coordsActor.setImage(new GreenfootImage("X: "+getX()+"\nY: "+getY(), 24, null, null));
}
The second way will avoid creating local variables and, in turn, avoid excess garbage collection (it is more efficient, both in code and in CPU usage). Now, all you have to do is call 'updateCoords' at the end of the 'act' method; or, better, after anytime the actor actually moves (only setting a new image when necessary).
ILOVELABRADORS ILOVELABRADORS

2014/9/3

#
I'll try that...
ILOVELABRADORS ILOVELABRADORS

2014/9/3

#
danpost wrote...
Now, all you have to do is call 'updateCoords' at the end of the 'act' method; or, better, after anytime the actor actually moves (only setting a new image when necessary).
Umm... Can you explain that part again please? I wrote the code down, but it only shows the x and y coordinate of where it starts, and when I start moving it , it remains the same as it started.
ILOVELABRADORS ILOVELABRADORS

2014/9/3

#
by the way, here is the "act" part of the code,
1
2
3
4
5
public void act()
    {
        moveAround();
        eat();
    }
... and here's the part that I just wrote...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// instant fields
    Actor coordsActor;
    // method to add actor into world
    public void addedToWorld(World world)
    {
        coordsActor = new Actor(){};
        world.addObject(coordsActor, 100, 30);
        updateCoords();
    }
     
    // method to update image of coords actor
    public void updateCoords()
    {
        String text = "X: "+getX()+"\nY: "+getY();
        GreenfootImage image = new GreenfootImage(text, 24, null, null);
        coordsActor.setImage(image);
    }
     
    // or just
    public void updateCoords2()
    {
        coordsActor.setImage(new GreenfootImage("X: "+getX()+"\nY: "+getY(), 24, null, null));
    }
ILOVELABRADORS ILOVELABRADORS

2014/9/3

#
ILOVELABRADORS wrote...
1
2
3
4
public void updateCoords2()
         {
                coordsActor.setImage(new GreenfootImage("X: "+getX()+"\nY: "+getY(), 24, null, null));
         }
I wrote " public void updateCoords2() over there because it wouldn't compile if there were two of those.
davmac davmac

2014/9/3

#
I wrote " public void updateCoords2() over there because it wouldn't compile if there were two of those.
danpost gave you two alternatives; you were meant to use one or the other, not both.
I wrote the code down, but it only shows the x and y coordinate of where it starts, and when I start moving it , it remains the same as it started
You don't seem to update the image during your act method. If you want it to update constantly, you need to update it from the act method (just call the updateCoords method from your act method). danpost:
The second way will avoid creating local variables and, in turn, avoid excess garbage collection (it is more efficient, both in code and in CPU usage).
The second way still creates objects and still puts them on the stack (the JVM is a stack-based machine), so there is no difference in terms of garbage collection between the two methods you outlined. I don't imagine you'd see any performance difference at all between the two methods.
ILOVELABRADORS ILOVELABRADORS

2014/9/3

#
Oh.
ILOVELABRADORS ILOVELABRADORS

2014/9/3

#
I removed one of them, but I'm kind of a starter at programming, :( so can you please write the code down please? Thanks
davmac davmac

2014/9/3

#
To call a method that's in the same class, you write the name of the method followed by parentheses containing the parameter values, if any. As 'updateCoords' doesn't have any parameters you can leave the parentheses empty in this case. If you don't understand something I've said, then please ask about it specifically. Writing a method call is so fundamental that you need to learn to do it yourself, so I'm not going to write it for you.
You need to login to post a reply.