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

2021/5/16

Click on box to get the next dialogue.

Furko Furko

2021/5/16

#
Heyo, I tried to incorporate a Dialogue screen into a game of mine. After each level there will be small box where text will be written with a typewriter effect. Currently I try to get the program to change to the next dialogue text by clicking on the box. For that I am using an integer which I use as the index for a list with the dialogue. When I click on the box the integer will increase by one. Currently I have the problem that I can increase the integer, yet, somehow the next text doesn't get displayed, even after implementing a method which resets the variables for the text. Down below I will include all necessary code. I hope that someone can make out where the mistake lies and help me implementing a functioning dialogue and dialogue box.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot und MouseInfo)
import java.util.*; 

public class DialogueScreen extends World
{
    private int level;
    private DialogueText dialoguetext;
    private DialogueBox dialoguebox;
    private int dialogueTextNumber = 0;
    private List<String> dialogue = new ArrayList<String>();
    
    public DialogueScreen(int leveldb)
    {    
        super(600, 800, 1);
        prepare();
        level = leveldb;
        
        
        if(leveldb == 0)
        {
            dialogue.add("Test1");
            dialogue.add("Test2");
            dialogue.add("Test3");
        }
        else if(leveldb == 1)
        {
            
        }
        else if(leveldb == 2)
        {
            
        }
        else if(leveldb == 3)
        {
            
        }
    }
    
    public void act()
    {
        textausgeben(dialogue.get(dialogueTextNumber));
        if(dialoguebox.getMouseClickedBox())
        {
            dialogueTextNumber = dialogueTextNumber + 1;
            dialoguebox.setMouseClickedBox();
            dialoguetext.resetVariables();
        }
    }
    
    private void prepare()
    {
        setBackground(blackBackground());
        prepareBox();
    }

    private void prepareBox()
    {
        DialogueBox db = new DialogueBox("dialogueBox.png");
        addObject(db, 300, 700);
        
        dialoguebox = db;

        DialogueText dt = new DialogueText();
        addObject(dt, 250, 700);
        
        dialoguetext = dt;

        magier wi = new magier();
        addObject(wi,130, 700);
    }

    private GreenfootImage blackBackground()
    {
        GreenfootImage img = new GreenfootImage(getWidth(), getHeight());
        img.setColor(Color.BLACK);
        img.fill();
        return img;
    }
    
    private void textausgeben(String text)
    {
        dialoguetext.textausgeben(text);
    }
    
    public int getDialogueTextNumber()
    {
        return dialogueTextNumber;
    }

}

public class DialogueText extends Actor
{
    private String text = "";
    private int testlength;
    private int i;
    /**
     * Act - tut, was auch immer DialogueText tun will. Diese Methode wird aufgerufen, 
     * sobald der 'Act' oder 'Run' Button in der Umgebung angeklickt werden. 
     */
    public DialogueText() 
    {
        // Ergänzen Sie Ihren Quelltext hier...
    }  

    public void act()
    {

    }

    public void textausgeben(String test)
    {
        Color hintergrund = new greenfoot.Color(0, 0, 0, 0);
        testlength = test.length();
        if(i < testlength)
        {

            text = text + test.charAt(i);
            GreenfootImage textan = new GreenfootImage(text , 50, greenfoot.Color.WHITE, hintergrund);
            setImage(textan);
            try
            {
                Thread.sleep(200);
            }
            catch(Exception ex)
            {
                System.out.println("Fehler");
            }

            i = i + 1;
        }
    }
    
    public void resetVariables()
    {
        testlength = 0;
        text = "";
    }
}

public class DialogueBox extends Actor
{
    private int diaTextNumber;
    private boolean nextText;

    public DialogueBox(String bild)
    {
        GreenfootImage img = new GreenfootImage(bild);
        setImage(img);
    }     
    
    public void act()
    {
        if(Greenfoot.mouseClicked(this))
            nextText = Greenfoot.mouseClicked(this);
    }   
    
    public boolean getMouseClickedBox()
    {
        return nextText;
    }
    
    public void setMouseClickedBox()
    {
        nextText = false;
    }
}  

danpost danpost

2021/5/17

#
I think you need to reset i as well (else the condition on ine 115 will fail to be true).
Furko Furko

2021/5/17

#
It works now. I couldn't find the mistake on my own, so thank you very much for sharing your insights!
You need to login to post a reply.