Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
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  
  [SOLVED] Problem displaying an isometric map portion  (Read 4254 times)
0 Members and 1 Guest are viewing this topic.
Offline gerard_pp

Junior Member


Medals: 1



« Posted 2012-03-17 23:42:09 »

Hello

Im having touble trying to display an isometric map portion (The diamond that covers all the visible screen) on the screen. I dont really know why is wrong with my code.

Can somebody help, please.

This is the code im using for the map builder:

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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
public pruebaGM(int ancho, int alto, int mosx, int mosy, int a[][], Applet p){
     
      //Applet padre
     padre = p;
     
      //Medidas del padre
     pantancho = ancho;
      pantalto = alto;
     
      //Creacion de mosaico modelo
     prototipo = new Mosaico(0, p);
     
      //Medidas de mosaico modelo
     mosancho = prototipo.ancho;
      mosalto = prototipo.alto;
     
      //Mitad de las medidas para puntos de referencia
     mosanchoMT = (mosancho/2)-1;
      mosaltoMT = mosalto/2;
     
      //Mosaicos visibles segun tamano de la pantalla
     mosavis = Math.round(pantancho*2/mosancho);
      //mosavisy = Math.round((pantalto*2)/mosaltoMT);
     
      //Referencia para centralizar mapa
     offsetH = Math.round(pantancho/2-mosanchoMT);
      //offsetV = Math.round((pantalto-(mosavisy*mosalto))/2);
     
      //Mitad de los mosaicos visibles
     mosavisMT = Math.round(mosavis/2);
      //mosavisyMT = Math.round(mosavisy/2);
     
      //Mosaicos de definicion de boundaries
     mosaX1 = mosx-mosavisMT;
      mosaX2 = mosx+mosavisMT;
      mosaY1 = mosy-mosavisMT;
      mosaY2 = mosy+mosavisMT;
     
      //Lienzo donde sera plasmado el mapa
     grid = new BufferedImage(ancho, alto, BufferedImage.TRANSLUCENT);
     
      //Creando interfaz grafica
     pintor = grid.createGraphics();
     
      //Buffer para registro de mosaico visible
     mosanum = 0;
     
      System.out.println("Eje de X: "+mosaX1+"-"+mosaX2);
      System.out.println("Eje de Y: "+mosaY1+"-"+mosaY2);
      //Bucle de construccion
     for(int i=mosaX1; i<mosaX2; i++){
         for(int j=mosaY1; j<mosaY2; j++){
            try{
               System.out.println(i);
               mosagrid = new Mosaico[mosavis*2];
               mosagrid[mosanum] = new Mosaico(a[i][j], p);
               mosagrid[mosanum].posX = ((j*mosanchoMT)-(i*mosanchoMT))+offsetH;
               mosagrid[mosanum].posY = ((j*mosaltoMT)+(i*mosaltoMT));//+offsetV;
              mosagrid[mosanum].paint(pintor);
               mosanum++;
            }catch(Exception ex){}
         }
      }
   }

Warning: Plz, excuse my bad english...
Offline UprightPath
« Reply #1 - Posted 2012-03-17 23:47:22 »

First, change:
1  
catch(Exception ex){}

to
1  
catch(Exception ex) { exception.printStackTrace(); }


I'm assuming that you're probably hitting something like a null pointer exception, but you're catching it without knowing it. Run your program with that and see whether you are actually throwing an exception or not. If you are, tell us which and what line (With the code snippet that surrounds that line). If not, tell us what the program's doing and what you're expecting it to do. :3

Offline gerard_pp

Junior Member


Medals: 1



« Reply #2 - Posted 2012-03-18 00:12:52 »

Hi and thanks for the answer.

the exception is: java.lang.ArrayIndexOutOfBoundsException: -12

what im hoping to achieve is measure the diamond shape of the map that sorrounds the screen and display it taking as the center the "mosx" and "mosy" tile. The bidimentional iteration is passing the array of the map negative values, because i set my starting tile as (0,0).

Warning: Plz, excuse my bad english...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline UprightPath
« Reply #3 - Posted 2012-03-18 00:24:25 »

Ah, that's the exception I meant. Not Null Pointer.

I think that there are several issues with your loop, mostly coming from how you're declaring/using various variables. This is some guess work, because a lot of understanding of code comes from understanding the intent, and I can only just decipher what your variables mean. :3

Ahem!

Issue one is happening every iteration (Meaning that you're tossing out the previous array on every iteration, which might not be your intent).
mosagrid = new Mosaico[mosavis*2];
Issue two is that mosagrid is being indexed by mosanum.
Let's say that mosavis = 6.
Then, let's say that (mosaX2 - mosaX1) == 6, and (mosaY2 - mosaY1) ==6.
You have:
1  
2  
3  
4  
5  
6  
7  
mosanum = 0;
for(int i = 0; i < 6; i++) {
    for(int j = 0; j < 6; j++) {
        mosagrid = new Mosaic[6*2]; // 12!
       mosanum++;
    }
}

If we follow the iterations:
i = 0, j = 0: mosanum = 0
i = 0, j = 1: mosanum = 1
i = 0, j = 2: mosanum = 2
...
i = 0, j = 5: mosanum = 5
i = 1, j = 0: mosanum = 6
i = 1, j = 1: mosanum = 7
...
i = 1, j = 5: mosanum = 11
i = 2, j = 0: mosanum = 12 (EXEPTION!)

Offline gerard_pp

Junior Member


Medals: 1



« Reply #4 - Posted 2012-03-18 00:44:32 »


You are right my friend. i decided to get "mosagrid = new Mosaico[mosavis*2];" out of the "for" loops (it had to be that way from the begining).

For the error number two maybe "mosagrid" is not the problem, cause it only counts the iterations ocurred. But thanks of you pointing that out, i think i discovered what went wrong with my code.

If i declare the "mosagrid" length to be "mosavis*2", im saying that mosagridĀ“s length is 24 (12*2). But it doesnt. The total number of iterations will be (12*12).

Im applying those changes, and see if i can fix it. Ill let you know.

Warning: Plz, excuse my bad english...
Offline gerard_pp

Junior Member


Medals: 1



« Reply #5 - Posted 2012-03-18 00:58:44 »

Now it is perfect!!!

Thanks a lot.

Warning: Plz, excuse my bad english...
Offline UprightPath
« Reply #6 - Posted 2012-03-18 01:44:00 »

Glad to have helped, goodluck! Cheesy

Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #7 - Posted 2012-03-18 11:19:22 »

Would you mind posting a screenshot? I would love to see it working Smiley

Offline gerard_pp

Junior Member


Medals: 1



« Reply #8 - Posted 2012-04-03 04:01:11 »

Sure,
Sorry for the late response. Here is the pic:




Warning: Plz, excuse my bad english...
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #9 - Posted 2012-04-03 07:43:17 »

Thanks! That looks nice!

Please don't forget to keep posting your progress Smiley

By the way, I kind of feel like the map itself looks weird, why don't you just make it a diamond? So the visible area is even in every axis. I hope you can understand what I mean Smiley Keep it up!

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

Senior Member


Medals: 3
Projects: 1



« Reply #10 - Posted 2012-04-04 09:41:04 »

Thanks! That looks nice!

Please don't forget to keep posting your progress Smiley

By the way, I kind of feel like the map itself looks weird, why don't you just make it a diamond? So the visible area is even in every axis. I hope you can understand what I mean Smiley Keep it up!
Correct me if I am wrong but there is diamond in that screenshot, but its too big so only top is shown
Offline gimbal

JGO Knight


Medals: 25



« Reply #11 - Posted 2012-04-04 13:14:21 »

Thanks! That looks nice!

Indeed a nice cell-shaded type of look to it. But... a yellow background. Why!?
Offline gerard_pp

Junior Member


Medals: 1



« Reply #12 - Posted 2012-04-04 15:03:22 »

Hehehe, the yellow background and the green tiles are just placeholders Cheesy

Just like Fokusas said, the map used to be a diamon shapped thing, but then, i realized that the tiles that go offscreen were adding some innecesary cpu cicles to the whole composition process. i decided just to show the tiles that fit on the screen, based on a current character position (in this case, the gray tile (10,10)).

What you can see now (looks like a little house indeed :p) is the top roof of the map, because the starting position tile (10, 10) is nearer (0,0).

What im working right now, is the movement. Im developing some kind of engine that draws new tiles in position as they appear, and erase old tiles as they go offscreen.

Warning: Plz, excuse my bad english...
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.

CogWheelz (16 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!