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

2023/6/19

Having a list with multiple actors

FALAFFI FALAFFI

2023/6/19

#
public void Aim(){
 
        List <Gegner4> gegnerr = getObjectsInRange(100, Gegner4.class);
        

        for(Gegner4  jederGegner : gegnerr ){
            if(schießen > 60){
            schießen = 0;
            Pfeil pfeil = new Pfeil();
            getWorld().addObject(pfeil, getX(), getY());
            pfeil.turnTowards(jederGegner.getX(), jederGegner.getY());
        } 
    }
}
Hello I have already written this code but now I want to add in another Actor in the List ( List <Gegner4> gegnerr = getObjectsInRange(100, Gegner4.class);) called Gegner5 is there anyway I can add him in
danpost danpost

2023/6/19

#
FALAFFI wrote...
Hello I have already written this code but now I want to add in another Actor in the List ( List <Gegner4> gegnerr = getObjectsInRange(100, Gegner4.class);) called Gegner5 is there anyway I can add him in << Code Omitted >>
To accommodate both types, make a list of Actor object:
public void Aim() {
    List<Actor> gegnerr = getObjectsInRange(100, Gegner4.class);
    gegnerr.addAll( (List<Actor>)getObjectsInRange(100, Gegner5.class) );
    for (Actor  jederGegner : gegnerr ) {
            if (schießen > 60){
            schießen = 0;
            Pfeil pfeil = new Pfeil();
            getWorld().addObject(pfeil, getX(), getY());
            pfeil.turnTowards(jederGegner.getX(), jederGegner.getY());
        } 
    }
}
FALAFFI FALAFFI

2023/6/30

#
thank you but it dosnt work and says "incompatible types: java.util.List<Gegner5> cannot be converted to java.util.List <greenfoot.Actor>
danpost danpost

2023/6/30

#
FALAFFI wrote...
thank you but it dosnt work and says "incompatible types: java.util.List<Gegner5> cannot be converted to java.util.List <greenfoot.Actor>
Sorry. Try this for line 2:
List<Actor> gegnerr = (List<Actor>)getObjectsInRange(100, Gegner4.class);
You need to login to post a reply.