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

2014/7/12

How to access instance field of subclass of actor from subclass of World

MusicPenguin MusicPenguin

2014/7/12

#
Title. unfinished code: Trying to set field Tile.valueOf to the array in line 10
public void seekTiles(){
        int[][] temp= new int[4][4];
        switch (direction){

            
            case 0: System.out.println(direction + "\n");
                for (int x = 0; x < 4; x++){
                    for (int y = 0; y < 4; y++){
                        if (!getObjectsAt(x, y, null).isEmpty()){
                            temp[x][y] = Tile.getValueOf();
                        }
                    }
                }
                for (int x = 1; x < 4; x++){
                    for (int y = 0; y < 4; y++){
                        if (!getObjectsAt(x, y, null).isEmpty()){
                            System.out.println(x + ", " + y + ", " + getObjectsAt(x, y, null));
                        }
                    }
                }
                
                break;
            
            case 1: System.out.println(direction + "\n");
                for (int x = 2; x > -1; x--){
                    for (int y = 0; y < 4; y++){
                        if (!getObjectsAt(x, y, null).isEmpty()){
                            System.out.println(x + ", " + y + ", " + getObjectsAt(x, y, null));
                        }
                    }
                }
            
                break;
            
            case 2: System.out.println(direction + "\n");
                for (int y = 1; y < 4; y++){
                    for (int x = 0; x < 4; x++){
                        if (!getObjectsAt(x, y, null).isEmpty()){
                            System.out.println(x + ", " + y + ", " + getObjectsAt(x, y, null));
                        }
                    }
                }
                
                break;
            
            case 3: System.out.println(direction + "\n");
                for (int y = 2; y > -1; y--){
                    for (int x = 0; x < 4; x++){
                        if (!getObjectsAt(x, y, null).isEmpty()){
                            System.out.println(x + ", " + y + ", " + getObjectsAt(x, y, null));
                        }
                    }
                }
            
                break;
            
            default: break;
        }
danpost danpost

2014/7/13

#
I was going to post this:
Where is the relevant code from the Tile class. You should probably post the entire class.
But, then I realized that you probably have bigger problems. Aren't you getting error messages saying something about calling non-static method in a static context? If you have several Tile objects and are trying to get the values from each one individually, the Tile class in itself cannot help. You need to refer to each Tile instance and get the values from them (how can the Tile class itself know which tile you want the value of?). By your use of 'null' in the 'getObjectsAt' calls, I will presume that Tile objects are the only type of objects in your world and therefore if the returned list is NOT empty you can assume that all objects listed are indeed Tile object. Then you can use:
Tile tile = (Tile)getObjectsAt(x, y, null).get(0);
to get a reference to the tile objects at that (x, y) location and then use:
temp[x][y] = tile.getValueOf();
(presuming that there is a 'getValueOf' method in the Tile class and it returns the value of a Tile object).
You need to login to post a reply.