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.

Page 2 of 2 FirstFirst 12
Results 26 to 37 of 37

Thread: Trouble Updating an Array

  1. #26
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Trouble Updating an Array

    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)

  2. #27
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    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.

  3. #28
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Trouble Updating an Array

    Quote Originally Posted by Gravity Games View Post
    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.
    Yeah, you're right...I was somehow absent-minded. Sorry.

  4. #29
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Trouble Updating an Array

    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)

  5. #30
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    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:
    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");
    	}
    }
    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 closeFile() {
    	l.close();
    }
    // And...
    public void closeHeader() {
    	lh.close();
    }
    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.

  6. #31
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Trouble Updating an Array

    Quote Originally Posted by jps View Post
    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:
    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");
    	}
    }
    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 closeFile() {
    	l.close();
    }
    // And...
    public void closeHeader() {
    	lh.close();
    }
    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.
    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:
    ggggg
    ggggg
    ggggg
    ggggg
    ggggg
    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.

    Example (though obviously not how it will be done):
    spawn "ground" 0,0
    spawn "ground" 32,0
    ...
    This is the way I need my level format set up, object based, correct?
    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)

  7. #32
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    Quote Originally Posted by Gravity Games View Post
    ... like my old Game Maker prototype.
    no idea what that is ...
    Quote Originally Posted by Gravity Games View Post
    ...object based, correct?
    certianly the way I would approach it



    So given your text file example:
    ggggg
    ggggg
    ggggg
    ggggg
    ggggg
    ...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.
    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.

  8. #33
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Trouble Updating an Array

    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)

  9. #34
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    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.

  10. #35
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Trouble Updating an Array

    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)

  11. #36
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble Updating an Array

    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.

  12. #37
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Trouble Updating an Array

    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)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Trouble with writing/reading array to/from file
    By MaximusPrime in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2012, 08:41 PM
  2. Trouble passing array in parameters
    By AngryCrow in forum Collections and Generics
    Replies: 2
    Last Post: September 12th, 2011, 09:49 AM
  3. .add(j, i); not updating array
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 08:30 AM
  4. [SOLVED] array trouble
    By littlefuzed in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:56 PM
  5. Grade Array Trouble
    By kite98765 in forum Collections and Generics
    Replies: 3
    Last Post: January 7th, 2010, 08:14 PM