GAGE Examples

I’ have the start of my engine which uses Gage to render maps. But if I try to use a tile size other than 64 x 64 I get ArrayOutofBounds exceptions in the Map rendering / GAGE code. Does anyone have examples of using it w/ different tile sizes for different maps.

Can you post the exception? Also, make sure that you’re intializing the TileManager with the correct size of tile. It would be rather annoying if you started seeing gaps simply because the sizes are wrong. :slight_smile:

JBanes;

The Exception I’m getting follows. I’m trying to overlay map w/ 32 x 32 px. Sprites on top of what is basically your Map Example code. I use a seperate TileManager for the 32 x 32 map. But nothing is drawn.Everything worked fine before I added the additional Map.

java.lang.ArrayIndexOutOfBoundsException: -55
at com.krypto.k2.graphics.level.Map.render(Map.java:289)
at com.krypto.k2.graphics.level.Parallax.render(Parallax.java:132)
at com.krypto.k2.graphics.GFXEngine.render(GFXEngine.java:195)
at com.krypto.k2.graphics.GameEngine.run(GameEngine.java:151)

The Map in the Stacktrace is just the GAGE stuff repackaged. Here is some of my code.

 protected static Parallax map = new Parallax();
    private static String[] tileNames = {"tile1.png", "tile2.png", "tile3.png", "tile4.png", "tile5.png", "blue.png"};
    private static int[] ids = new int[tileNames.length];
    private static int[] mapdata = {
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 0, 1, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 4, 2, 4, 0, 4, 1, 4, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 5, 4, 4, 0, 3, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 4, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 1, 4, 0, 4, 4, 1, 2, 3, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 0, 5, 4, 0, 4, 0, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 0, 4, 4, 4, 0, 4, 2, 4, 4, 0, 0, 0, 0, 0, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 0, 0, 4, 4, 0, 0, 0, 0, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 0, 4, 4, 0, 0, 4, 4, 0, 0, 0, 0, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 0, 0, 0, 0, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 0, 4, 3, 4, 4, 0, 5, 0, 0, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 0, 0, 0, 0, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};
        
    private static int[] mapdata2 = {
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 0, 1, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 
        4, 4, 4, 2, 4, 0, 4, 1, 4, 1, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 5, 4, 4, 0, 3, 4, 4, 4, 4, 5, 
        4, 4, 4, 4, 4, 4, 4, 4, 2, 3, 4, 4, 4, 4, 4, 
        4, 4, 1, 4, 0, 4, 4, 1, 2, 3, 4, 5, 4, 4, 4, 
        4, 4, 0, 5, 4, 0, 4, 0, 2, 3, 4, 4, 4, 4, 4, 
        4, 4, 0, 4, 4, 4, 0, 4, 2, 4, 4, 0, 0, 0, 0, 
        4, 4, 4, 4, 4, 4, 4, 0, 0, 4, 4, 0, 0, 0, 0, 
        4, 4, 4, 4, 0, 4, 4, 0, 0, 4, 4, 0, 0, 0, 0, 
        4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 0, 0, 0, 0, 
        4, 4, 4, 4, 4, 4, 0, 4, 3, 4, 4, 0, 5, 0, 0, 
        4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 0, 0, 0, 0, 
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};
        
     private static int[] mapdata3 = {
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 
        6,6,6,6,6,6,6,6,6,6,6,6,0,6,6, 
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 
        6,6,6,6,6,0,6,6,6,6,6,6,6,6,6, 
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 
        6,6,6,6,6,0,6,6,6,6,6,6,6,6,6, 
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 
        6,6,6,6,6,6,6,6,6,0,6,6,6,6,6, 
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6
      };
            
    /** Creates a new instance of K2Driver */
    public GameEngine()
    {
        TileManager manager = new TileManager(64, 64);
        TileManager manager2 = new TileManager(32, 32);
        
        Map layer1 = new Map(manager, 20, 20, 800, 600);
        Map layer2 = new Map(manager, 15, 15, 800, 600);
        Map layer3 = new Map(manager2, 15, 15, 800, 600);       
        //Layer[] layers = {layer2,layer1};
        Image[] images = loadImages();               
        
        for(int i=0; i<images.length; i++) ids[i] = manager.addTile(images[i]);
        manager2.addTile(images[5]);
        // manager2.addTile(images[6]);
        for(int i=0; i<mapdata.length; i++)
        {
            if(mapdata[i] > 0) mapdata[i] = ids[mapdata[i]-1];
        }
           
        layer1.copyMap(mapdata);
        layer2.copyMap(mapdata2);
        layer2.copyMap(mapdata3);
        
        map.addMap(layer2);
        map.addMap(layer1);
         map.addMap(layer3);
        //map = new Map(layers);
    }

I’d need to see the modified version of Map to be sure (the line numbers are different), but I do notice one thing off the bat:

You’re ignoring the reference numbers returned by “addTile” for manager2. It’s most likely 1, not 6. (New manager, new id numbers.)

Try fixing that. If it doesn’t work, send the changed Map class to jbanes@techie.com and I’ll take a look at it.

Your problem is that your map is smaller than your screen. (15x32 = 480 which makes for a 480x480 map which is less than the 800x600 viewport you defined). GAGE doesn’t expect (or define) what will happen in this situation. BTW, another bug is here:

layer2.copyMap(mapdata3);

That should read:

layer3.copyMap(mapdata3);

In answer to your other question, GAGE will handle various sizes of Maps without fail. Using Parallax, the smaller ones will simply scroll slower than the larger ones. The only catch is that all your maps have to have the same size viewport. Since this is usually the size of the screen itself, this isn’t a big deal.

You may want to look at cleaning up your code too. The cleaner you keep it, the easier it will be on you later.

JBanes;
Thanks for your reply. I know the code is dirty, I’m just testing stuff at the moment.

[quote]I know the code is dirty, I’m just testing stuff at the moment.
[/quote]
Just some friendly advice. I know from experience that test can often become production code. :slight_smile:

Your Right;
I’m cleaning it up before I go any further… now that it is working, thanks again. ;D