This site requires JavaScript, please enable it in your browser!
Greenfoot back
blankaex
blankaex wrote ...

2014/5/29

Traversing a Matrix

blankaex blankaex

2014/5/29

#
So I have a 10x10 matrix of a class called distance, and it goes something like
Distance[][] matrix = { { } }
with instances of "null" and "new Distance()" in it, and it's compiling fine so I don't think there's any issues there. However, since it's a matrix of my distance classes, I can't call the addObject() method inside it, so I figured I'd make a loop that does that for me.
        int i = 0;
        int j = 0;
        
        while (i < 10 && j < 10)
        {
            if (matrix[i][j] != null)
            {
                addObject(matrix[i][j], 640, 360);
            }
            i++;
            j++;
        }
Long story short, it's not working. It compiles but I get a blank screen, something I've come across when I've had infinite loops and such, but I don't think that's the case here. Any ideas? I can post more of the code if anyone needs it.
lordhershey lordhershey

2014/5/29

#
problem is you are only acting upon objects matrix , , ,, ...
        int i = 0;
        int j = 0;
        
        for(i = 0 ; i < 10 ; i++)
        for(j = 0 ; j < 10 ; j++)
        {
            if (matrix[i][j] != null)
            {
                addObject(matrix[i][j], 640, 360);
            }
        }
another problem is that all objects are being added to the same location 640,360 , maybe you want to lay it out like tiles or something like
int i = 0;
        int j = 0;
        
        for(i = 0 ; i < 10 ; i++)
        for(j = 0 ; j < 10 ; j++)
        {
            if (matrix[i][j] != null)
            {
                addObject(matrix[i][j], 64 * i , 36 * j);
            }
        }
you would also have to make sure that matrix had stuff in it.
You need to login to post a reply.