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

2016/7/13

changing text

FutureCoder FutureCoder

2016/7/13

#
i have this code, getWorld().showText(""+life,getX(), getY()); This shows my characters life counter, but i need to but it above him and change the size, how can i do this?
SPower SPower

2016/7/13

#
To move it up, you need to subtract some constant from the y value that you're passing into the method. Making it a different size is a more difficult endeavor, however. In fact, it's not possible using this method: you're going to need to generate the text yourself (at the appropriate size) and then display that. What I would advice you to do is to create a new Actor subclass, called something along the lines of "TextDisplay" (without the quotes). Then, its constructor would have the text to be displayed as a parameter:
public TextDisplayer(String text)
{
    // implementation
}
To render the text, you're gonna need to create a GreenfootImage object and set the image of the TextDisplayer to that image. For the exact implementation, you're gonna have to look into the documentation (because that's an important thing to learn), but it'll be something along the lines of:
GreenfootImage newImage = ...;
setImage(newImage);
danpost danpost

2016/7/13

#
I do not think the text that the 'showText' method displays can be altered as far as size (or font, or color). Also, changing the location of text using the 'showText' method is basically impossible (you would have to remove it from one location and display it at another to simulate moving it). You can check out my Value Display Tutorial scenario which shows how you can bypass these issues by using an Actor object to display the text.
FutureCoder FutureCoder

2016/7/13

#
How would i subtract some constant from the y value
danpost danpost

2016/7/13

#
FutureCoder wrote...
How would i subtract some constant from the y value
To answer that, you will need to show what code you are using for the text object in the class of the actor it is to remain above.
SPower SPower

2016/7/14

#
This is what I was talking about:
showText(""+life, getX(), getY() - *insert number here*);
And the moving of showText is actually really simple, since the documentation states that when the method is called again, previous objects are removed. This means you can simply call showText over and over again with updated x and y coordinates, and it'll look like it's moving around.
danpost danpost

2016/7/14

#
SPower wrote...
And the moving of showText is actually really simple, since the documentation states that when the method is called again, previous objects are removed. This means you can simply call showText over and over again with updated x and y coordinates, and it'll look like it's moving around.
Actually, the documentation states this:
Any previous text shown at the same location will first be removed.
This means that you must remove the text at one location and show text at the next if you are to continue to use the 'showText' method.
FutureCoder FutureCoder

2016/7/14

#
i used this, getWorld().showText(""+hit,getX(), getY()-18); and the size wasn't a problem, but i don't understand how make it look like it's moving, when my character moves, the text stays where it just was, how do i stop it.
danpost danpost

2016/7/14

#
As long as no other code is changing the location of the character in question, you can do this:
// at beginning of act method (to remove)
getWorld().showText(null, getX(), getY()-18);

// within act method (to replace)
getWorld().showText(""+hit, getX(), getY()-18);
FutureCoder FutureCoder

2016/7/14

#
Thank you so much, it works perfectly.
You need to login to post a reply.