Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Making a side scroller game  (Read 7225 times)
0 Members and 1 Guest are viewing this topic.
Offline DurzoBlint15

Senior Newbie





« Posted 2012-03-07 18:05:21 »

I want to make a kind of 2D puzzle side-scroller which will use tiles for the map. Do you have any idea on how I could load multiple files like Frame1.txt and Frame2.txt and translate numbers into tiles? Also, how could I make it so when the character wants to go to the next frame, it transitions smoothly?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-03-07 19:31:32 »

Reading a file is dead easy:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
BufferedReader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("Frame1.txt")),"UTF-8"));

    int i;
    while((i = reader.read()) != -1) {
        int num = Integer.parseInt(String.valueOf((char)i));
        //now you can play with the number
   }
}
catch(Exception exc) {
    exc.printStackTrace();
    //cleanup appropriately
}
finally {
    if(reader != null) {
        try {
            reader.close();
        }
        catch(Exception exc) {
            //error with flushing/closing the stream
       }
    }
}

Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #2 - Posted 2012-03-08 23:35:59 »

I love to read any thread I can! ra4king, you just answered a weird question I had not so related to OP.
Thank you both Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-03-09 16:49:06 »

Fine idiom, but for the love of Bob, please stop catching and ignoring exceptions.  I swear checked exceptions cause more damage than they ever prevented.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-03-10 00:42:54 »

Fine idiom, but for the love of Bob, please stop catching and ignoring exceptions.  I swear checked exceptions cause more damage than they ever prevented.
It's the freaking close() method! There are only 2 possible exceptions at that point: NullPointerException, caused if an exception was thrown inside the outer try-catch; and IOException, which almost never happens and if it does, it means something happened when flushing the buffer Smiley

Offline sproingie

JGO Kernel


Medals: 202



« Reply #5 - Posted 2012-03-10 01:40:05 »

It's in the block above the close too.  Printing the exception then going on like nothing happened is still ignoring the exception.  I know, example code, but I'm seeing everyone do this everywhere because they don't like being bitched at by the compiler.  If you want to defeat checked exceptions, the boilerplate to use instead is this:

1  
2  
3  
except (Exception e) { 
    throw new RuntimeException(e);
}

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #6 - Posted 2012-03-10 03:35:03 »

There are only 2 possible exceptions at that point: NullPointerException, caused if an exception was thrown inside the outer try-catch; ...

FWIW, and I'm sure you already knew this, but you can avoid the possible NPE quite easily by checking the reader for null in your finally block.

1  
2  
3  
4  
5  
6  
7  
8  
9  
} finally {
   if (reader!=null) {
      try {
         reader.close();
      } catch (IOException ioe) {
         // Logging and error handling of your choice
     }
   }
}


Don't you just love how verbose Java can be.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #7 - Posted 2012-03-10 03:44:40 »

You can also use Lombok

http://projectlombok.org/features/Cleanup.html

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #8 - Posted 2012-03-10 03:47:06 »

It's in the block above the close too.  Printing the exception then going on like nothing happened is still ignoring the exception.  I know, example code, but I'm seeing everyone do this everywhere because they don't like being bitched at by the compiler.

I agree with this for large-scale applications, but for smaller scale games like most folks here are writing, I think a simple printStackTrace() is sufficient in cases like this.  A game loading a level data file or whatever is exceedingly unlikely to fail since you package it with your game, meaning you know its exact location and it's guaranteed to exist.  And if somehow the file reading *does* fail at runtime, you're going to stare at a stack trace to figure out why, no matter what.  Whether that was printed in some deeply nested try/catch block or stopped your main thread via a wrapper runtime exception, your course of action will be the same.

Though if someone's paying you for your application, and/or it accepts arbitrary input, or has some other reason to be more robust, then you'll need to be writing some error handling boilerplate.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #9 - Posted 2012-03-10 04:00:16 »

Fine idiom, but for the love of Bob, please stop catching and ignoring exceptions.  I swear checked exceptions cause more damage than they ever prevented.
It's the freaking close() method! There are only 2 possible exceptions at that point: NullPointerException, caused if an exception was thrown inside the outer try-catch; and IOException, which almost never happens and if it does, it means something happened when flushing the buffer Smiley

You could at least print a stack trace, just so we know if this becomes a problem in the future, for whatever reason.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #10 - Posted 2012-03-10 04:08:48 »

I agree with this for large-scale applications, but for smaller scale games like most folks here are writing, I think a simple printStackTrace() is sufficient in cases like this.  

No, it isn't, not even slightly.  What do you think the proper behavior is when the program fails to load a level?  Yes, you're going to get a stack trace sometime, later, but it'll probably be a NPE, and you'll have to re-run the program under a debugger to find out why.  Printing the exception to stderr is not very useful if the program was launched from a GUI.  Your top-level can take care of capturing, logging, and displaying uncaught exceptions and do it in one place.

The language provides structured exceptions for a reason, so we don't have to write code like C and check for null returns from everything.  And you get this feature by writing less boilerplate, not more.  So I really don't see the need to cripple a program's robustness just because it's small.
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #11 - Posted 2012-03-10 04:57:53 »

No, it isn't, not even slightly.  What do you think the proper behavior is when the program fails to load a level?  Yes, you're going to get a stack trace sometime, later, but it'll probably be a NPE, and you'll have to re-run the program under a debugger to find out why.

If you did an "e.printStackTrace()", then the level-loading stack trace would also be printed, before the cascading NPE.  The original error is not lost.  No need for a debugger.  My point was that folks writing small-scale games, the I/O involved in level loading is very unlikely to fail once you get it working the first time.  If the data is static, Class.getResource() loads it from a relative location and it "just works."

Quote
Printing the exception to stderr is not very useful if the program was launched from a GUI.

Of course, you can log the exception however is appropriate for your deployment scenario.

I'm not arguing to do this all the time, and I'm not arguing to "write code like C" either.  I'm just trying to follow the KISS principle.  Decide how robust your error handling should be in your game and implement it appropriately.  
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2012-03-10 15:30:10 »

Alright, well I updated my post Smiley

Offline gimbal

JGO Knight


Medals: 25



« Reply #13 - Posted 2012-03-12 17:03:50 »

Hmm, a mention of Java 7's automatic resource cleanup seems appropriate seeing the thread content Smiley

http://www.oracle.com/technetwork/articles/java/trywithresources-401775.html
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2012-03-12 21:47:22 »

Yup, using Java 7, that huge try-catch block would shrink down to:
1  
2  
3  
4  
5  
6  
7  
try(BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("Frame1.txt"),"UTF-8"))) {
    int i;
    while((i = reader.read()) != -1) {
        int num = Integer.parseInt(String.valueOf((char)i));
        //now you can play with the number
   }
}

Offline GabrielBailey74
« Reply #15 - Posted 2012-03-14 04:19:55 »

That looks like what I was going to make a post about!.
I wanted to be able to read a file and say: Ok, if a number is 1 than that means draw a tile.
But.. I wanted to know how to detect spaces in the file, also collumn rows, possibly say if there's a space, that means draw the image at the current x but + the image width, vice versa for a images y for a new line.

Reading a file is dead easy:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
BufferedReader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("Frame1.txt")),"UTF-8"));

    int i;
    while((i = reader.read()) != -1) {
        int num = Integer.parseInt(String.valueOf((char)i));
        //Do all that FANCY STOOFS here XD

    }
}
catch(Exception exc) {
    exc.printStackTrace();
    //cleanup appropriately
}
finally {
    if(reader != null) {
        try {
            reader.close();
        }
        catch(Exception exc) {
            //error with flushing/closing the stream
       }
    }
}


For transfering into a new frame/map, try adding a way to pause the game/slow it down and render a nice black fading transparent screen.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2012-03-14 04:23:11 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
switch((char)i) {
    case ' ':
        //space;
       break;
    case '1':
        //1
       break;
    //etc....
}

Offline GabrielBailey74
« Reply #17 - Posted 2012-03-14 04:26:10 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
switch((char)i) {
    case ' ':
        //space;
       break;
    case '1':
        //1
       break;
    //etc....
}


*Saves in notepad*
Should keep me busy for tonight.
1  
2  
3  
4  
5  
6  
0 0000000000 0
0 1111111111 0
0 2232323220 0
0 2232323220 0
0 1111111111 0
0 0000000000 0

Thank you.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (20 views)
2014-09-21 15:42:18

BurntPizza (14 views)
2014-09-21 14:30:30

moogie (14 views)
2014-09-21 13:26:15

UprightPath (25 views)
2014-09-21 09:14:06

BurntPizza (27 views)
2014-09-19 16:14:18

Dwinin (42 views)
2014-09-12 22:08:26

Norakomi (73 views)
2014-09-11 02:57:51

TehJavaDev (97 views)
2014-09-10 19:39:09

Tekkerue (49 views)
2014-09-09 15:24:56

mitcheeb (70 views)
2014-09-08 19:06:29
List of Learning Resources
by Longor1996
2014-08-16 23:40:00

List of Learning Resources
by SilverTiger
2014-08-06 08:33:27

Resources for WIP games
by CogWheelz
2014-08-02 05:20:17

Resources for WIP games
by CogWheelz
2014-08-02 05:19:50

List of Learning Resources
by SilverTiger
2014-08-01 05:29:50

List of Learning Resources
by SilverTiger
2014-08-01 05:26:06

List of Learning Resources
by SilverTiger
2014-08-01 00:54:12

HotSpot Options
by dleskov
2014-07-08 14:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!