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

2015/9/24

How to get an Object from a Java.util.List?

yolomilk yolomilk

2015/9/24

#
Hi I would like to get an Object placed in my World in order to get 2 Parameters from it and than safe it in a array. I tried to use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private void Bootespeichern()
    {
        for(int y = 0; y < getHeight(); y++)        
        {
            for(int x = 0; x < getWidth(); x++)
            {
                
                if(!getObjectsAt(x,y,BootP1.class).isEmpty())                  //Boot1.class is a Class with the Parameters CordX, CordY,
                {
                     
                     
                    int Ox = getObjectsAt(x,y,BootP1.class).CordX;
                    int Oy = getObjectsAt(x,y,BootP1.class).CordY;
                    BootevonP1 [1] [1] = getObjectsAt(x,y,BootP1.class);
                }
                else
                {
                    BootevonP1 [x] [y] = null;
                }         
            }
 
        }
    }
danpost danpost

2015/9/24

#
I will presume that the method given is in a subclass of World. The two 'for' loop together will check every location within your world. If there are no BootP1 objects at a location then the BootevonP1 array element for that location is set to 'null'; otherwise --- I do not know, exactly. Lines 12 and 13 would never compile as the List class does not contain fields names 'CordX' and 'CordY'. Line 14 wants to put a List object in your 'BootevonP1' array and always in the second element of the second subArray (both indecis are '1'). It might help if you show how the 'BootevonP1' array is declared. I doubt very much it is declared to hold List objects
yolomilk yolomilk

2015/9/26

#
this is where the BootevonP1 is declared:
1
public BootP1 BootevonP1[][] = new BootP1 [15] [15] ;
this is how CordX and Cord Y get created:
1
2
public int CordX = 0;
public int CordY = 0;
and how they get set:
1
2
3
4
5
public void act()
    {
        CordX = getX();
        CordY = getY();
    }
Super_Hippo Super_Hippo

2015/9/26

#
Use '.get(0)' after the 'getObjectsAt' method. Cast it to a 'BootP1' object and then, you can get 'CordX' and 'CordY' from it. Since these ints are only the current x and y coordinates, you can also use 'getX()' and 'getY()' instead.
danpost danpost

2015/9/26

#
Super_Hippo wrote...
Use '.get(0)' after the 'getObjectsAt' method. Cast it to a 'BootP1' object and then, you can get 'CordX' and 'CordY' from it. Since these ints are only the current x and y coordinates, you can also use 'getX()' and 'getY()' instead.
You can get the x- and y-coordinates either way, but having the fields 'CordX' and 'CordY' in the BootP1 class is redundant since the Actor class has 'x' and 'y' fields for an actor's location in the world that can be obtained by using the Actor class 'getX' and 'getY' methods. I really do not see a need for any of the code given. Each actor, as just stated, already contains its x- and y- coordinate values and the world contains a List of all actors within it and has a 'getObjects' method that can be used to get a List object containing all actors of a specific type. If you really believe that you need this code, please explain how the array 'BootevonP1' will be used. I seriously believe that there will be a better way to go about it.
yolomilk yolomilk

2015/10/1

#
I will open diffrent worlds and I think this information gets lost if u open another world. I will swap back to the world later in the program and have to place the boots again.
danpost danpost

2015/10/2

#
yolomilk wrote...
I will open diffrent worlds and I think this information gets lost if u open another world. I will swap back to the world later in the program and have to place the boots again.
You can save a reference to the original world in the new one and return to the original one in the same state that it was in when you left it.
yolomilk yolomilk

2015/10/29

#
how can I do that?
danpost danpost

2015/10/29

#
yolomilk wrote...
how can I do that?
Add an instance field to the new world to hold a reference to the old one:
1
public World originalWorld;
When going to the second world (I will call it 'NewWorld' -- you should correct its name):
1
2
3
4
NewWorld newWorld = new NewWorld();
newWorld.originalWorld = this; // if in World subclass
// or
newWorld.originalWorld = getWorld(); // if in Actor subclass
Then, to return:
1
Greenfoot.setWorld(originalWorld);
yolomilk yolomilk

2015/11/20

#
thx
You need to login to post a reply.