Hi, Gravity Games here, and I need to be able to load variables from a file so I can make different sized levels for my game. I'd prefer not to have to hard code anything, because I plan to have a level editor later on. So I NEED to be able to load the variables from a file.
This is the code from the java class that loads the levels:
package scorpioengine.main; import java.awt.*; import java.io.*; import java.util.*; import java.lang.*; import javax.swing.ImageIcon; public class Level { private Scanner l; private Scanner lh; private String Header[] = new String[5]; private String stringlevelsizex = "14"; private String stringlevelsizey = "14"; private String stringmusic = "1"; private String stringtileset = "1"; public void openHeader(){ try{ lh = new Scanner(new File("C://CWWLEVELS//world1stage1cfg.txt")); }catch(Exception e){ System.out.println("error loading configuration file"); } } public void readHeader(){ while(lh.hasNext()){ for(int i=0; i < 1; i++){ stringlevelsizex=lh.next(); } for(int i=1; i < 2; i++){ stringlevelsizey=lh.next(); } for(int i=2; i < 3; i++){ stringmusic=lh.next(); } for(int i=3; i < 4; i++){ stringtileset=lh.next(); } } } public void closeHeader(){ lh.close(); } private int levelsizex = Integer.parseInt(stringlevelsizex); private int levelsizey = Integer.parseInt(stringlevelsizey); private int music = Integer.parseInt(stringmusic); private int tileset = Integer.parseInt(stringtileset); private String Level[] = new String[(levelsizey)]; private Image grassek, grassektop, waterek, path1, path2; public Level(){ ImageIcon img = new ImageIcon("C://CWWGFX//GrassmainEKlarge.PNG"); grassek=img.getImage(); img = new ImageIcon("C://CWWGFX//ChompPortrait.PNG"); grassektop=img.getImage(); img = new ImageIcon("C://CWWGFX//EKWaterLarge.PNG"); waterek=img.getImage(); img = new ImageIcon("C://CWWGFX//Path1Large.PNG"); path1=img.getImage(); img = new ImageIcon("C://CWWGFX//Path2Large.PNG"); path2=img.getImage(); openHeader(); readHeader(); closeHeader(); openFile(); readFile(); closeFile(); } public Image getGrassEK(){ return grassek; } public Image getGrassEKTop(){ return grassektop; } public Image getWaterEK(){ return waterek; } public Image getPath1(){ return path1; } public Image getPath2(){ return path2; } public String getLevel(int x, int y){ String index = Level[y].substring(x,x+1); return index; } public int getlevelsizex(){ return levelsizex; } public int getlevelsizey(){ return levelsizey; } public void openFile(){ try{ l = new Scanner(new File("C://CWWLEVELS//world1stage1.txt")); }catch(Exception e){ System.out.println("error loading map"); } } public void readFile(){ while(l.hasNext()){ for(int i=0; i < ((levelsizey)); i++){ Level[i]=l.next(); } } } public void closeFile(){ l.close(); } }
...and here is the text in "world1stage1cfg":
I know its not the file itself, because I've tried the numbers with and without the quotation marks. If you know whats wrong, please help! I've tried everything, but I'm pretty sure the problem is that the open, read, and close Header routines aren't being run properly. Also, it runs fine when the defaults for the variables are the same as in the file, but obviously that won't always be the case."14"
"14"
"1"
"1"