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

2018/5/22

How to know if an object is present in the World

spacefrags spacefrags

2018/5/22

#
I'm doing an University Project with Greenfoot that consist of creating a chess game that plays by itself. Now I already done much of it, but now I'm facing with a problem: every time a CPU player is doing a move there will be a method that randomize the selected piece and if it cannot do anything, it will pass to a second randomize piece. The problem comes when someone begin to eat the pieces, because those objects will be removed, and the next move if those pieces are selected to do something it will give me the error that the actor is not in the world. What I'm asking is if there is a way, a condition, a method to know if the object exist in the world so that I can skip that object till I find one that exist. Thanks in advance
danpost danpost

2018/5/22

#
If you have actors and references to them, then:
if (actor.getWorld() != null)
will be true if actor is in the world (and false, if not).
spacefrags spacefrags

2018/5/23

#
Sorry for the late response, but I had to try it well before and I have to say that it's perfect, now it's doing what it should do! Thank you.
You need to login to post a reply.