×
Namespaces

Variants
Actions
Revision as of 09:00, 30 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to create 2d tiled array binary file and load it in Java ME

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to create 2d tiled array binary file and load it in Java ME

Article Metadata
Article
Created: reflexus@ig.com.br (21 Aug 2012)
Last edited: hamishwillee (30 Jul 2013)

This article explains how to save a tiled 2d array, using a very simple utility written in Java (not J2ME), and then use the produced binary file into your own Java ME game program. By this way you have the advantage to save some memory device RAM (during runTime), because the array will not be hard coded (built-in into code).

Let's start with the arrays. Note:

  • class Tiles is compiled together with Main
  • 4x6 byte arrays: (short arrays: you can use dataout.writeShort and dis.readShort())
  • those numbers are examples

In this example, We have 2 levels in the game, they represent the tiled map

package matrix;
 
/** the hard-coded way **/
 
public final class Tiles {
 
 
protected static byte[][] tiles01() {
byte[][] t = {
{65, 32, 65, 34},
{65, 65, 65, 65},
{67, 56, 0, 0},
{65, 0, 0, 0},
{65, 99, 94, 65},
{ 0, 99, 98, 0}
};
return t;
}
 
protected static byte[][] tiles02() {
byte[][] t = {
{65, 65, 65, 65},
{65, 65, 65, 65},
{61, 62, 0, 0},
{65, 54, 65, 66},
{65, 94, 95, 65},
{11, 99, 10, 0}
};
return t;
}
}

Here is the utility to save the 2d array binary file to your hard disk.

Please note that this is Java code (not J2ME) and you will have to create a Java project (in NetBeans, for example) and compile the utility yourself.

The method write2DArray (int level, byte tiles[][])

Write the binary file in the same folder (dist) of the app The file will be exactly (tile#.map) size in bytes:

1*n if we use dataout.writeByte
2*n if we use dataout.writeShort
4*n if we use dataout.writeInt
and so on...
where n = total number of elements of the array
      n = 4*6;  (for this example) 


To run the utility, run the jar file:

  • double click it or
  • in the command line (prompt MS-DOS) type:
    java -jar c:\appName.jar
  • it will generate 2 binary files (2 maps):
      tile1.map 24 bytes  (4x6)
    tile2.map 24 bytes (4x6)
package matrix;
 
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
/**
* @author George Roberto Peres
*/

 
public class Main {
 
 
private static Tiles tiles = new Tiles();
 
 
public static void main(String[] args) {
// in this example we have 2 levels to create
for (int i=1; i<3; i++) {
switch(i) {
case 1: write2DArray(i, tiles.tiles01()); break;
case 2: write2DArray(i, tiles.tiles02()); break;
//case 3: write2DArray(i, tiles.tiles03()); break;
//case 4: write2DArray(i, tiles.tiles04()); break;
}
}
}
 
 
 
private static void write2DArray(int level, byte tiles[][]) {
// modify the string argument as you wish
File file = new File("tile"+level+".map");
try {
FileOutputStream output = new FileOutputStream(file);
DataOutputStream dataout = new DataOutputStream(output);
for (short row = 0; row < tiles.length; row++) {
for (byte col = 0; col < tiles[row].length; col++) {
dataout.writeByte(tiles[row][col]);
//dataout.writeShort(tiles[row][col]);
//dataout.writeInt(tiles[row][col]);
}
}
output.close();
dataout.close();
}
catch(IOException ioe) { }
}
 
 
}

...And the Java ME method code

readLevel is very very simple. in this case, we are using local variable t to hold our level; this way we can reuse t to call other methods, for example: defineBackground(stage, t);

Warning.pngWarning: The imports; in this example we are working with 4x6 arrays that represents each game level/stage

You can easily produce tiled maps arrays with NetBeans built-in game constructor known as Game Builder.

import java.io.DataInputStream;
import java.io.IOException;
 
 
protected void readLevel(byte stage) {
byte[][] t = new byte[6][4]; //4x6 array
DataInputStream dis;
try {
// modify the string argument as you wish
dis = new DataInputStream(getClass().getResourceAsStream("tile"+stage+".map"));
for (byte lin=0; lin<6; lin++) {
for (byte col=0; col<4; col++) {
t[lin][col] = dis.readByte();
//t[lin][col] = dis.readShort();
//t[lin][col] = dis.readInt();
}
}
dis.close();
}
catch (IOException e) { }
finally { dis = null; }
//defineBackground(stage, t);
//defineMiddleground(stage, t);
t = null;
}


...That's it. As you can see it is not so difficult. Thankfully, the Java API is really a "vast pool of functionality". Don't forget to put the files produced by the Java app (tile1.map and tile2.map) into your Java ME app diretory (wherever you wish).

Happy coding!

This page was last modified on 30 July 2013, at 09:00.
70 page views in the last 30 days.
×