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

2012/5/28

make a dialogue/text box load from .txt

Andriyanto Andriyanto

2012/5/28

#
I have a problem with dialogue .. I made a class text to display dialogue and call the txt file but when I call on the MyWorld text appears only when I click, the next dialogue does not appear again, so how to create a dialogue so that the next show? mycode in myworld : private boolean runningStory = false; int storyTime = 0; public void setLevel_pilihlevel() { background = new GreenfootImage("pilihlevel.png"); setBackground(background); if (runningStory) { storyTime++; if (storyTime ==10) { addObject(new Text("PrologueText1",500, 20f),350,185); } if (storyTime == 20) { addObject(new Text("PrologueText2",500, 20f),350,185); } }
danpost danpost

2012/5/28

#
We would have to see the code for your 'Text' class to see what is actually going on. Also, where in your code is 'runningStory' changed to true?
Andriyanto Andriyanto

2012/5/28

#
oh yes, i don't understand about it.. i want make a dilogue after dialogue1 appear, iam klik then dialogue2 appear..so iam make if (runningStory) and storyTime..but it doesn't work..please help me.. this is my code class Text.. import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.awt.Font; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.StringTokenizer; import java.util.ArrayList; public class Text extends Actor { private float FONT_SIZE; private int WIDTH; private int HEIGHT; private String curString; private int index; private int pos; private int hPos; private String nxtFile; private String curFile; private boolean closing; ArrayList<String> words; public GreenfootImage image; Font font = new Font("Monospaced",getImage().getFont().getStyle(), getImage().getFont().getSize()); public Text() {getImage().setFont(font);} public Text(String text, int width, float fSize) { initialize(text, width, fSize); getImage().setFont(font); } public void initialize(String text, int width, float fSize) { //setImage(new GreenfootImage(1,1)); closing = false; words = new ArrayList<String>(); curFile = text; FONT_SIZE = fSize; WIDTH = width; String temp = "text/"+text+".txt"; curString = getTextFile(temp); HEIGHT = calcHeight(curString); readStringToArray(curString); index = 0; pos = (int)(FONT_SIZE); hPos = (int)(FONT_SIZE*1.3); image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(0,0,0, 128)); image.fillRect(0, 0, WIDTH, HEIGHT); //image.setColor(new Color(0, 0, 0, 128)); //image.fillRect(3, 3, WIDTH-6, HEIGHT-6); Font font = new Font("Courier New", image.getFont().getStyle(), image.getFont().getSize()) ; font = font.deriveFont(FONT_SIZE); image.setFont(font); image.setColor(Color.WHITE); setImage(image); } public void act() { if (closing) { getWorld().removeObject(this); return; } ///**** if (words.size() == 0 && (Greenfoot.mouseClicked(this) || Greenfoot.isKeyDown("enter"))) { if (nxtFile != null && !curFile.equals(nxtFile)) { initialize(nxtFile, WIDTH, FONT_SIZE); } else closing = true; } if (words.size() == 0) return; else if (words.get(0).equals("@@")) { words.remove(0); nxtFile = words.remove(0); } else if (pos > WIDTH-FONT_SIZE*words.get(0).length() && index == 0 ) { pos = (int)FONT_SIZE; hPos += (int)FONT_SIZE*2-FONT_SIZE/1.5; } else { String temp = words.get(0).substring(index, index + 1); image.drawString(temp, pos, hPos); index += temp.length(); pos += (int)FONT_SIZE/2; setImage(image); if (index >= words.get(0).length()) { words.remove(0); index = 0; pos+= FONT_SIZE/2; } } } public String getTextFile(String textFileName) { String str = ""; StringBuffer sb = new StringBuffer(); try{ URL url = getClass().getClassLoader().getResource(textFileName); if(url == null) throw new IOException("File not found: " + textFileName); InputStream is = url.openStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); while(line != null) { if (line != null) str += " "+line; line = br.readLine(); } is.close(); br.close(); } catch(Exception ex) { return str; } return str; } public void readStringToArray(String str) { StringTokenizer st = new StringTokenizer(str); while (st.hasMoreTokens()) words.add(st.nextToken()); } public int calcHeight(String str) { int numRow = (str.length()*(int)FONT_SIZE)/(WIDTH-2*(int)FONT_SIZE); if (numRow == 0) numRow = 1; return numRow*(int)FONT_SIZE+3*(int)FONT_SIZE; } public myWorld getGWorld() { return (myWorld) getWorld(); } public void loadNext(String text) { //nxtFile = ""; curFile = text; //setImage(new GreenfootImage(1,1)); String temp = "text/"+text+".txt"; curString = getTextFile(temp); HEIGHT = calcHeight(curString); //reachedEnd = false; index = 0; pos = (int)(FONT_SIZE); hPos = (int)(FONT_SIZE*1.3); //acts = -2; image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(255,255,255, 128)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 128)); image.fillRect(3, 3, WIDTH-6, HEIGHT-6); Font font = new Font("Courier", image.getFont().getStyle(), image.getFont().getSize()) ; font = font.deriveFont(FONT_SIZE); image.setFont(font); image.setColor(Color.WHITE); } }
You need to login to post a reply.