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

2015/4/9

String as parameter?

jackandjim jackandjim

2015/4/9

#
Well, we tried to use a String as a parameter and it gives us an error:
1
cannot find Symbol - class GegnerNR
The code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private void schlagen(){
        schlagMethode("Gegner1","gegner1","g1Leben",1);
        schlagMethode("Gegner2","gegner2","g2Leben",2);
    }
     
    private void schlagMethode(String GegnerNR,String gegnerNR,String gNRLeben,int GNR){
        if (isTouching(GegnerNR.class)){
            List<GegnerNR> gegnerNR = getWorld().getObjects(GegnerNR.class);
            int  gX = gegnerNR.get(0).getGX();
            int  gY = gegnerNR.get(0).getGY();
            if(this.getX() <= gX && getImage().equals(pa4a) ){
                    if(treffer == 0){
                        gNRLeben = gNRLeben -1;
                    }
                    treffer = GNR;
            }
            if(this.getX() >= gX && getImage().equals(pa3a) ){
                    if(treffer == 0){
                        gNRLeben = gNRLeben -1;
                    }
                    treffer = GNR;
            }
            if(this.getY() >= gY && getImage().equals(pa1a) ){
                    if(treffer == 0){
                        gNRLeben = gNRLeben -1;
                    }
                    treffer = GNR;
            }
            if(this.getX() <= gX && getImage().equals(pa2a) ){
                    if(treffer == 0){
                        gNRLeben = gNRLeben -1;
                    }
                    treffer = GNR;
            }
        }
    }
I'd be happy to give any information if needed. Thank you in advance, J&J!
Super_Hippo Super_Hippo

2015/4/10

#
How are the Enemy classes different to each other? If they are similar, you could create a class "Gegner" and have "Gegner1" and "Gegner2" extend it. Then you can use something like this:
1
Gegner g = (Gegner) getOneIntersectingObject(Gegner.class);
What exactly does each parameter specify? The first one is the class for which it is checked, but what are the others doing?
davmac davmac

2015/4/10

#
In this line:
1
if (isTouching(GegnerNR.class)){
"GegnerNR.class" is a class literal - it refers to a class object by name. In this case, the class name that you've given is 'GegnerNR' and there is no class by that name, which is why you get the error. I think what you are trying to do is use the String variable 'GegnerNR' to identify the class. You can't really do that. The closest you can get is to use a Class parameter:
1
private void schlagMethode(Class gegnerNR,String gNRLeben,int GNR){private void schlagMethode(class gegnerNR,String gNRLeben,int GNR){
By the way, Java convention is that variable and parameter names begin with a lower-case letter - it should be 'gegnerNR' rather than 'GegnerNR'. You had a duplicate parameter (both gegnerNR and GegnerNR) so I removed one of them in the example above. You would then pass a class literal to the method (Gegner1.class for example). However, you would then get another problem on this line:
1
List<GegnerNR> gegnerNR = getWorld().getObjects(GegnerNR.class);
Although you could change 'GegnerNR.class' to 'gegner', the problem is the 'List<GegnerNR>'. You cannot parameterise a list type with a variable. The solution in this case is to have Gegner1 and Gegner2 both extend a common base class (perhaps called just Gegner). Then you can do:
1
List<Gegner> gegnerList = getWorld().getObjects(gegnerNR);
Note I also changed the variable name of the list from 'gegnerNR' to 'gegnerList' to avoid the shadowing that otherwise occurs (you already have a parameter named gegnerNR...). There is one other problem I can see: on lines 13, 19, 25 and 31 you are trying to subtract an integer from a string. I think that again, you are trying to use the string 'indirectly' (it contains a variable name) but you cannot do that in Java. Perhaps you need to look at using arrays to hold the lives (leben) so that you can identify the correct one via an index.
davmac davmac

2015/4/10

#
Super_Hippo:
Then you can use something like this:
1
Gegner g = (Gegner) getOneIntersectingObject(Gegner.class);
The problem this this is that it then does not differentiate between the two types of object, Gegner1 and Gegner2. You would get one of either type, when what is wanted is one of a specific type.
Super_Hippo Super_Hippo

2015/4/10

#
In his code, I didn't see where he differentiates between the different Gegner classes. If he needs to do that, he can still use this:
1
2
3
4
5
6
7
8
9
10
11
12
Gegner g = (Gegner) getOneIntersectingObject(Gegner.class);
if (g != null)
{
    if (g instanceof Gegner1)
    {
        //...
    }
    else if (g instanceof Gegner2)
    {
        //...
    }
}
jackandjim jackandjim

2015/4/10

#
Both Gegner1 and Gegner2 are the same but we used 2 different classes because we didn't manage to (for example) delete them one by one. Is there any way to (for example) delete 2 objects of one class one by one? It would solve all problems...
davmac davmac

2015/4/10

#
In his code, I didn't see where he differentiates between the different Gegner classes
He is trying to use the GegnerNR (String) parameter to identify which class he wants.
he can still use this:
No, the method is supposed to work on either Gegner1 instances or Gegner2 instances, not both at the same time. The code you give finds an object that is either a Gegner1 or Gegner2, and then acts differently according to which was found. But if the method should act only on Gegner1 (for instance), it might miss a suitable Gegner1 - even if one were close enough - because a Gegner2 was found instead.
davmac davmac

2015/4/10

#
Both Gegner1 and Gegner2 are the same but we used 2 different classes because we didn't manage to (for example) delete them one by one.
In that case you should have three classes - Gegner, and then Gegner1 and Gegner2 (which inherit from Gegner).
Is there any way to (for example) delete 2 objects of one class one by one? It would solve all problems...
Your question doesn't really make sense (what do you mean by delete 2 objects "one by one"? just delete one, then delete the other...). I think what you really want is to be able to delete some (but not all) objects of one class - in that case, yes, you can get a list of objects, then filter it manually. I.e. go through the list and choose which ones you want to delete, put them in a new list, and call removeObjects(...) with that list.
Super_Hippo Super_Hippo

2015/4/10

#
Ah, now I know what you mean... this should finally work it.
1
2
3
4
5
6
7
8
9
10
11
12
13
for (Object obj : getIntersectingObjects(Gegner.class))
{
    Gegner g = (Gegner) obj;
     
    if (g instanceof Gegner1)
    {
        //...
    }
    else if (g instanceof Gegner2)
    {
        //...
    }
}
Of course you can remove just one object of a class from the world. Either the object itself causes this (maybe it moves outside the world, dies a natural death or something), then the code should be in the act method of this class, so you have a reference to the object with 'this' which you can remove then. Or it is eaten or killed by another object (predator->prey or bullet->enemy), then you can get a reference to it with 'getOneIntersectingObject' or as above to remove this object.
davmac davmac

2015/4/10

#
Ah, now I know what you mean... this should finally work it.
No, your revised code still deals with both Gegner1 and Gegner2 instances at the same time. It's supposed to be parameterised so that you can choose which one you want to deal with - either Gegner1 instances, or Gegner2 instances.
jackandjim jackandjim

2015/4/10

#
Ok thank you guys ^^ Fixed it :)
Super_Hippo Super_Hippo

2015/4/10

#
davmac wrote...
No, your revised code still deals with both Gegner1 and Gegner2 instances at the same time. It's supposed to be parameterised so that you can choose which one you want to deal with - either Gegner1 instances, or Gegner2 instances.
He was calling the method once for each enemy. It didn't look like he needed it separately.
You need to login to post a reply.