Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to be able to load variables from a file so I can make different sized le

ID: 3538488 • Letter: I

Question

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(); } }

Explanation / Answer

You should simplify your problem to solve it. Just use a simple text file and a very simple Java program that reads in the text file into say a collection of Strings. If and when that works, move to the next step, perhaps parsing the information into numbers if that is necessary, or marshalling it into objects. Continue adding functionality in a step-wise fashion until the problem is solved or until you reach a step that has an error that can't be solved. Then post the code here.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote