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

2019/8/27

Scrolling text

Bluetiger Bluetiger

2019/8/27

#
I am trying to create a rpg game with text that types for itself in a text box. Is there any way this can happen??
danpost danpost

2019/8/27

#
Bluetiger wrote...
I am trying to create a rpg game with text that types for itself in a text box. Is there any way this can happen??
Yes -- absolutely. For help, need more details and some attempted codes (class code for typing text box)..
Bluetiger Bluetiger

2019/8/27

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class text extends Actor
   
{
    private GreenfootImage baseImg, textImg;
    private int scrollAmt;
 
    public text(String text)
    {
        baseImg = new GreenfootImage(100, 20);
        baseImg.drawRect(0, 0, 99, 19);
        setText(text);
        updateImage();
    }
 
    // allows setting/changing of scrolling text
    public void setText(String text)
    {
        GreenfootImage stringImg = new GreenfootImage(text, 16, Color.BLACK, new Color(0, 0, 0, 0));
        textImg = new GreenfootImage(stringImg.getWidth(), 20);
        textImg.drawImage(stringImg, 0, 10-stringImg.getHeight()/2);
        scrollAmt = baseImg.getWidth(); // reset scroller
    }
 
    // creates the image to display at current scroll position
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(baseImg);
        image.drawImage(textImg, scrollAmt, 0);
        setImage(image);
    }
 
    // runs the scrolling of the text
    public void act()
    {
        scrollAmt--;
        if (scrollAmt ==-textImg.getWidth()) scrollAmt = baseImg.getWidth();
        updateImage();
    }
}
Bluetiger Bluetiger

2019/8/27

#
I tried this but it didn't give me what I wanted. The text I want is the type that rpgs have and go from left to right and stop at the end when the sentence is complete. Sorry if this is confusing
danpost danpost

2019/8/28

#
Bluetiger wrote...
I tried this but it didn't give me what I wanted. The text I want is the type that rpgs have and go from left to right and stop at the end when the sentence is complete. Sorry if this is confusing
I was really off as far as what I thought you were wanting. Still, of course, it is possible. Not sure why you create two images in the setText method. Only need to create the first one (and have it named texttImg) and have the updateImage method draw it at an appropriate height (maybe 2)..
danpost danpost

2019/8/28

#
I just updated my Menu Demo scenario (from about 8 yrs back). I does not quite look as good running in javascript; however is does (and has always) contain a Marquis class that extends a Button class. It creates repeating horizontally scrolling text. Cancel a menu by clicking on the menu title text to view the scrolling text.
danpost danpost

2019/8/28

#
Bluetiger wrote...
I tried this but it didn't give me what I wanted. The text I want is the type that rpgs have and go from left to right and stop at the end when the sentence is complete. Sorry if this is confusing
I guess I am a bit confused. Maybe a step-by-step of what should happen is needed because what you initially said, the posted code AND what you later stated present 3 totally different ideas (in my mind). Only the code suggests scrolling and I am guessing that is not actually what you want. The initially given phrase that catches what I think you want was:
text that types for itself in a text box
This suggests you want one letter to appear at a time, starting at the left in the text box. Am I on the right track?
Bluetiger Bluetiger

2019/8/29

#
This suggests you want one letter to appear at a time, starting at the left in the text box. Am I on the right track?
Yes this is exactly what I want. Ahah sorry that I was a bit confusing
danpost danpost

2019/8/29

#
Bluetiger wrote...
Yes this is exactly what I want. Ahah sorry that I was a bit confusing
You will need a timer and two String fields -- one for "input" and one for "output". A base image would also be helpful. See what you can put together and come back with your attempted codes if and when you get stuck.
You need to login to post a reply.