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.
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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.