I have a Windows OS, the double slashes worked before I changed the arrays to arraylists. Also, I'll try combining all that code into one method and see if that helps.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I have a Windows OS, the double slashes worked before I changed the arrays to arraylists. Also, I'll try combining all that code into one method and see if that helps.
Current Projects (and planned release dates):
Chomp's Wacky Worlds [???] (PC, Android and Ouya)
Kyle the Caiman [???] (PC, Android and Ouya)
KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)
One more thing my lost post had:
When you get nothing to show, but the code compiles and seems to run, it is time to use the ol debugger, or some println(my_variables) and see what is where.
Thankfully good ol' "printlns" pointed me in the right direction again. It appears that the problem might not be with the arraylist in the Level class, but with the way my JPanel interprets it.
JPanel Code:
for(int y = 0; y < (l.getlevelsizey()); y++){ for(int x = 0; x < l.getlevelsizex(); x++){ if(l.getLevel(x,y).equals("g")){ g.drawImage(l.getGrassEK(),x*32,y*32,null); } if(l.getLevel(x,y).equals("t")){ g.drawImage(l.getGrassEKTop(),x*32,y*32,null); } if(l.getLevel(x,y).equals("w")){ g.drawImage(l.getWaterEK(),x*32,y*32,null); } } }
Level Class Code (just in case):
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 = "13"; private String stringlevelsizey = "13"; private String stringmusic = "1"; private String stringtileset = "1"; 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 ArrayList<String> Level1 = new ArrayList<String>(); 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(); levelsizex = Integer.parseInt(stringlevelsizex); levelsizey = Integer.parseInt(stringlevelsizey); music = Integer.parseInt(stringmusic); tileset = Integer.parseInt(stringtileset); File(); //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 = Level1.get(x & y); return index; } public int getlevelsizex(){ return levelsizex; } public int getlevelsizey(){ return levelsizey; } 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(); } public void File(){ try{ l = new Scanner(new File("C://CWWLEVELS//world1stage1.txt")); while(l.hasNext()){ for(int i=0; i < ((levelsizey)); i++){ Level1.add(i,l.next()); } } l.close(); }catch(Exception e){ System.out.println("error loading map"); } } public void readFile(){ while(l.hasNext()){ for(int i=0; i < ((levelsizey)); i++){ Level1.add(i,l.next()); } } } public void closeFile(){ l.close(); } }
Lets hope that the next thing changed will be the last, I can't help but feel I made a really stupid mistake somewhere...
Current Projects (and planned release dates):
Chomp's Wacky Worlds [???] (PC, Android and Ouya)
Kyle the Caiman [???] (PC, Android and Ouya)
KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)
What I meant in my previous suggestion on the combined methods is a little different. Maybe you should read that link again. But basically:
Note your open methods. Both of them. They both init a scanner to a file. They are (were) exact duplicates of code:
Just for a moment, imagine these two methods as one. If you took in a String with just one method, BAM! good to go. But don't run off just yet. Consider the following:public void openHeader() { try { lh = new Scanner(new File("C://CWWLEVELS//world1stage1cfg.txt")); } catch (Exception e) { System.out.println("error loading configuration file"); } } // And... public void openFile() { try { l = new Scanner(new File("C://CWWLEVELS//world1stage1.txt")); } catch (Exception e) { System.out.println("error loading map"); } }
Look at how much less code it would be to add the close methods; Also combined in your all-in-one-method. So now if you was to toss your read methods into the middle of it, determine what you are working on, header or file, take proper action for either case, and close on the way out. Six methods become one, no code is repeated, and you can safely open/read/close your files without worry, assuming you set up your try catch finally block correctly. Reread that link on the try block. Even if you don't use the try block with resources, at least get some of this repeated code cleaned out, and cover your back side while you are reading the files.public void closeFile() { l.close(); } // And... public void closeHeader() { lh.close(); }
Okay, I'll try to combine the methods further. I also think I figured out the problem with my level format. Instead of using a text file that goes sort of like this:
to draw five rows of blocks where each g is in a grid, I need to figure out how to write the text file so it writes to the arraylist, and then the arraylist spawns objects at each location, like my old Game Maker prototype.ggggg
ggggg
ggggg
ggggg
ggggg
Example (though obviously not how it will be done):
This is the way I need my level format set up, object based, correct?spawn "ground" 0,0
spawn "ground" 32,0
...
Current Projects (and planned release dates):
Chomp's Wacky Worlds [???] (PC, Android and Ouya)
Kyle the Caiman [???] (PC, Android and Ouya)
KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)
no idea what that is ...
certianly the way I would approach it
So given your text file example:...instead of loading the entire file into one ArrayList, you could have a multi-dimensional ArrayList. I am not sure if that is more useful to you or not, depending on how you plan to iterate over them when it is time to use them.ggggg
ggggg
ggggg
ggggg
ggggg
For example if you just need to pass by each one once, there would be no need in multi-dimensional ArrayList. Except maybe if you were going to have a grid setup where you will iterate over each block of each row, and using the index of row/block provides easy access to the ArrayList... Really so much of your project is still in the dark from my chair. I don't want to mislead you into something you don't need.
Well exactly what I need is a level format that allows an infinite amount of tiles on both the X and Y "axis" (for lack of a better word) I'm thinking that loading a list of objects to spawn from a file into the arraylist would be the way to go, but I'm not sure. Also, you probably wouldn't know what my prototype is, I never released it and made it just so I could quickly get a feel of how it would play.
Current Projects (and planned release dates):
Chomp's Wacky Worlds [???] (PC, Android and Ouya)
Kyle the Caiman [???] (PC, Android and Ouya)
KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)
Well there are many possible ways to store/access/manipulate data. I would say think about what kind of access you need, what kind of sorted/unsorted order you need to store them in, and spend some time going over the different ways to store data. Maybe reading from a file is not the way you want to go. If you are talking about 5x5 items in a file, your worries are limited. If you are really going after "infinite" amounts of x and y, then you need to be really concerned. Things like needing access to elements in the middle of the data set vs needing them in top down or bottom up order, all of that gets increasingly more important as you increase the size of the set. Really get an idea of what you need, then look for what suits it best.
Well, I might not need infinite, but I will need enough for full length levels. A relatively large max number, like 9999 should work (maybe not even that large). I will need larger levels than 5x5, that was just a simple example. Simply spawning the objects from top left to bottom right should be enough, but I will need the player to interact with certain objects in different ways (like, for instance, touching a spring object will make the character bounce upwards). I absolutely need the levels to be stored as external files and not be hardcoded. I plan to have a level editor as well as plenty of levels in the main game (which will be made in the editor, so ease of creation is top priority.
Current Projects (and planned release dates):
Chomp's Wacky Worlds [???] (PC, Android and Ouya)
Kyle the Caiman [???] (PC, Android and Ouya)
KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)
You really need to consider important factors then.
How many times will you need to access the data?
How often will you need to access the data?
Will you need the data in a specific order, and if so (yes in this case I would think) what ordering is that you want?
Will you ever need immediate access to various elements of the data in a semi-randomized manner?
There are many many ways to go at it, and some are better suited than others, depending on answers to questoins like those above.
As a side note, I totally agree. Never hard code data into your program. What ever I said give you that impression, ignore it lol. Totally not what I meant. There are more options than dumping a text file into an array on startup.
I'm assuming by spawning in objects, I only need the list once and then the objects themselves will take care of the rest. Any order of spawning the objects in will probably do, but the way I did it originally was start from the very top row from left to right, then going down row by row until the bottom. I'm pretty sure that this will cut it for a platformer game, but I could be wrong.
Current Projects (and planned release dates):
Chomp's Wacky Worlds [???] (PC, Android and Ouya)
Kyle the Caiman [???] (PC, Android and Ouya)
KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)