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

2012/6/2

How do I use getObjectsInRange?

Gazzzah Gazzzah

2012/6/2

#
I'm trying to write code to determine if an enemy hears a 'gunshot'. I figured I could use getObjectsInRange with the actor I use for a gunFlash. This is what I have:
private boolean hearShot()
    {
        heardShot = (GunFlash) getObjectsInRange(1200, GunFlash.class);
        return heardShot != null;
    }
if (!canSeePlayer() && hearShot())
        {
            lastSeen.set(heardShot.x, heardShot.y);
            setState(chasing);
        }
When I try to run I get this error: java.lang.ClassCastException: java.util.ArrayList cannot be cast to GunFlash at Enemy.hearShot(Enemy.java:100) at Enemy.stateOfBehaviour(Enemy.java:293) at Enemy.act(Enemy.java:75) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) So I guess the problem is that it's returning a list and I just want the one actor. How can I adapt this to make it work? Or is there anything I could use alternatively? It would be nice if there was a way to detect nearby objects like the getOneIntersectingObject and getOneObjectAtOffset methods, but they don't really cover areas :/ (Just had an idea as I wrote that: what if I create an actor that is just a massive invisible box and use getOneIntersectingObject... would that work?)
SPower SPower

2012/6/2

#
First, import the List class:
import java.util.List;
Then, do this:
List<GunFlash> actors = getObjectsInRange(1200, GunFlash.class);
We're creating a list, not a single actor. Finally, walk through the list:
for (GunFlash a in actors) {
    if (a != null) {
          // the code for each GunFlash
    }
}
Gazzzah Gazzzah

2012/6/2

#
I only need one, chances are there will only be one anyway. I don't think there will be any loss if it just picks the first one on the list.
SPower SPower

2012/6/2

#
If you just want one, change the walk through the list code to:
for (GunFlash a in actors) {  
    if (a != null) {  
          // the code for each GunFlash  
          break;
    }  
}
Gazzzah Gazzzah

2012/6/2

#
A'ight, sweet, thanks
Gazzzah Gazzzah

2012/6/2

#
Actually, I don't really want to apply code to the "GunFlash"s, I really just want it to return true if there is something in the list.
SPower SPower

2012/6/2

#
You're not required to do something for each GunFlash, just say:
for (GunFlash a in actors) {  
    if (a != null) {  
          return true;
    }  
}  
trash1000 trash1000

2012/6/2

#
First, that's not the correct syntax for a foreach loop in Java.
for(Gunflash a : actors) {
    if(a != null) {
        return true;
    }
}
Second, I don't really know if getObjectsInRange() is the best way to do it. But you don't really need a foreach-loop to determine whether or not the list is empty.
private boolean heardShot() {
    return getObjectsInRange(1200, GunFlash.class).isEmpty();
}
So when there are no objects of GunFlash.class in range the enemy did not hear to shot, when there it did.
SPower SPower

2012/6/2

#
I'm sorry for the wrong fast enumeration loop: I was confused with objective c.
Gazzzah Gazzzah

2012/6/2

#
Ahhhh, yeah thanks guys
You need to login to post a reply.