3

 9 represented by 9

7 represented by 7

8represented by 8

6represented by 6

2d array the -1 is empty region and 0 is grass full

this is the code i am using to render the view

//map class
        private int[][] terrain = new int[20][20];
        }


        //constructor
        protected Drawable[] tiles = new Drawable[10];

        tiles[GameMap.GRASS]  = getResources().getDrawable(R.drawable.grass);
        tiles[GameMap.GRASS_WU]  = getResources().getDrawable(R.drawable.grass_wu);
        tiles[GameMap.GRASS_WD]  = getResources().getDrawable(R.drawable.grass_wd);
        tiles[GameMap.GRASS_EU]  = getResources().getDrawable(R.drawable.grass_eu);
        tiles[GameMap.GRASS_ED]  = getResources().getDrawable(R.drawable.grass_ed);

            protected void onDraw(Canvas canvas) {
        //move_Unit();

        for (int x=0;x<map.getWidthInTiles();x++) {
            for (int y=0;y<map.getHeightInTiles();y++) {

                if(map.getTerrain(x,y) != -1){

                tiles[map.getTerrain(x,y)].setBounds(x*16,y*16,(x*16) + 16,(y*16) + 16);


                if (map.getUnit(x,y) != 0) {
                    tiles[map.getUnit(x,y)].setBounds(x*16,y*16,(x*16) + 16,(y*16) + 16);
                } 
                tiles[map.getTerrain(x, y)].draw(canvas);
                tiles[map.getUnit(x, y)].draw(canvas);
                }
            }
        }

        }

    } 

why does my view appear like this and how do i fix it?enter image description here

kimlexson
  • 31
  • 1
  • 4

3 Answers3

1

Have you checked cocos2d-x? You have that stuff already.

Macarse
  • 91,829
  • 44
  • 175
  • 230
  • yes i did briefly but i abandoned it as it focused on the iphone but your link seems to also cover android. let me take a closer look thanks!! – kimlexson Mar 13 '11 at 23:17
  • `cocos2d-x` is a c++ port of `cocos2d-iphone` :) – Macarse Mar 13 '11 at 23:22
  • http://www.raywenderlich.com/1163/how-to-make-a-tile-based-game-with-cocos2d the link does shed some light but c++ is not one of my strong sides so instead of using the QT version of tiled i will use the java version and hopefully it will be easier to intergrate with the android code – kimlexson Mar 14 '11 at 00:18
0

Have you considered the fact that the x and the y are usually reversed in computer graphics. I recall doing some work with it when I made a 2d tile engine with XNA framework.

Basically the problem is that the function call takes x first and the y so it doesn't match the array lookup you're performing.

Your map array is square, try making it odd sized and flipping the draw function parameters to see what happens.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
t-my
  • 113
  • 1
  • 2
  • 10
0

It's surprisingly easy. Just treat it as a 2D overhead game, but things toward the bottom of the screen need to be drawn later, so that they appear in front of things higher up on the screen. So, for example, if the van in the top of this screenshot were to drive forward a little, the bus right in front of it needs to be drawn after the van:

http://nanek.name/images/autotrafego_rain.jpg

That way the top of the bus is drawn over the front of the van and it looks like the bus is closer to the camera, occluding something behind it. The artwork all has to be drawn to match, of course, but that doesn't take any programming effort. When a car in the above game is going a different direction, the game just draws a different packaged piece of art for it.

Of course, there are games that actually use 3D models, with a positioned camera, to achieve this appearance as well.

Lance Nanek
  • 6,377
  • 2
  • 24
  • 19
  • thanx lance i belive its called the "Zig-zag" or " diamond" approach: this link offers the code samples http://stackoverflow.com/questions/892811/drawing-isometric-game-worlds – kimlexson Mar 14 '11 at 06:22
  • Yes this i definitely what am looking for. I will implement it for now using the 2d arrays and them when i have a working model i will then move on to integrate it with a map editor – kimlexson Mar 14 '11 at 06:31