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

2017/5/14

How can I detect an object in a specific cell?

LoopingSample LoopingSample

2017/5/14

#
So I'm making a labyrinth game and I have troubles detecting the object when it is at the exit. Basically, it is exactly one cell where the player has to be standing to trigger the loading of the next level. Being in the World Class, I thought of using
if (getObjectsAt(winX, winY, Player.class) !=null) {

loadLv2();

}
But in this case it triggers the second level instantly without even standing on the right cell (which is defined earlier on). Is it possible that getObjetsAt() always returns NOT null? How could I make the game detect if the player is standing exactly at one cell and then make it trigger the next level?
danpost danpost

2017/5/14

#
LoopingSample wrote...
Is it possible that getObjetsAt() always returns NOT null?
That is exactly what happens. It always returns a List object. It is the size of list that is important here.
LoopingSample LoopingSample

2017/5/15

#
Okay then, but how could I make the program toggle the next level then? I also have multiple levels, so if I use the List.isEmpty(), it will only work once, as after that the List is already filled. Can you somehow restore the empty status of a List?
LoopingSample LoopingSample

2017/5/15

#
I also tried creating a "finish" object which, when intersecting Player, will call a method in Playfield via
if (getOneIntersectingObject(Player.class) !=null) {
            
            ((Playfield)getWorld().getObjects(Playfield.class).get(0)).victory();
            
        }
Gives me an error though.
Yehuda Yehuda

2017/5/15

#
Using getObjectsAt returns a list of the objects at the specified location, if there are no objects there then the list will be empty no matter what used to be there or what will be there. So I think that just doing the following should be fine.
if (!getObjectsAt(winX, winY, Player.class).isEmpty()) {
    loadLv2();
}
LoopingSample wrote...
I also tried creating a "finish" object which, when intersecting Player, will call a method in Playfield via <Code Omitted> Gives me an error though.
If Playfield is the name of your World subclass, then you shouldn't be trying to get a List of all the instances of Playfield that are in the world. If you have a method called victory() in a world subclass called Playfield, then an actor in that World can call the method by just doing:
((Playfield) getWorld()).victory();
LoopingSample LoopingSample

2017/5/16

#
Thanks, got it now :D
You need to login to post a reply.