views:

174

answers:

2

Background:

I have two 2d arrays. Each index within each 2d array represents a tile which is drawn on a square canvas suitable for 8 x 8 tiles.

The first 2d array represents the ground tiles and is looped and drawn on the canvas using the following code:

                 //Draw the map from the land 2d array
    map = new Canvas(mainFrame, 20, 260, 281, 281);
        for(int i=0; i < world.length; i++){
            for(int j=0; j < world[i].length; j++){
                for(int x=0; x < 280; x=x+35){ 
                    for(int y=0; y < 280; y=y+35){
                        Point p = new Point(x,y);
                        map.add(new RectangleObject(p,35,35,Colour.green));
                    }
                }
            }
        }

This creates a grid of green tiles 8 x 8 across as intended.

The second 2d array represents the position on the ground. This 2d array has everyone of its indexes as null apart from one which is comprised of a Person class.

Problem

I am unsure of how I can draw the position on the grid. I was thinking of a similar loop, so it draws over the previous 2d array another set of 64 tiles. Only this time they are all transparent but the one tile which isn't null. In other words, the tile where Person is located.

I wanted to use a search throughout the loop using a comparative if statement along the lines of

if(!(world[] == null)){
map.add(new RectangleObject(p,35,35,Colour.red));}

However my knowledge is limited and I am confused on how to implement it.

A: 

Do not use a second Array at all. Simply create a RectangleObject named, for example, currentPosition or activeTile or personPosition. You, however, do not really need to store the Point in the RectangleObject. Either use a 2D RectangleObject array for that (so that you can use the indexes to access them) and exclude the Point information in the RectangleObject,

or

create a List of RectangleObjects and add the Point information - but do not increment by 35, but rather by 1. When you're drawing the tiles, you can still (by knowing the index) figure out where to put the tile (e.g. indexX*tileWidth, indexY*tileHeight).

(It's not quite clear from what you've written what world and map are used for. Please explain so I can give a better answer)

Tedil
A: 

Reply to Tedil: Thanks for the reply and my apologies for not being entirely clear. map is just a variable name for a blank canvas. world is a variable containing the first 2d array talked about.

I'll post back after examining the implementation of your two solutions.

mcp
Apologies, I should have clarified further before your answer. I am certain I will need 2 seperate 2d arrays for my situation. From what I understand your solutions implied that each tile index within world isnt important?position and world are based of two different class types. world is made up of WorldProperty. The reason for this is that I intend to have each index draw a tile of a different colour dependant on what is contained within each WorldProperty. This is the reason why I think drawing the position array over the world array is the suitable solution despite me lacking the answer.
mcp