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

2019/11/8

Check something is disappear in world

MianBao_Bread MianBao_Bread

2019/11/8

#
I want make a game like pacman, now I want to end the game when all score disappear. This is my Code
if (getObjectsInRange(555, Score.class) == null) {
            Greenfoot.stop();
        }
It's not work, I want to know how to solve it. I am sorry that my English is not good.
danpost danpost

2019/11/8

#
MianBao_Bread wrote...
I want make a game like pacman, now I want to end the game when all score disappear. This is my Code << Code Omitted >> It's not work, I want to know how to solve it. I am sorry that my English is not good.
The condition in line 1 will always be false. A List object is always returned by getObjectsInRange. You need to check the size of the list returned; or rather, check if it is an empty list
if (getObjectsInRange(555, Score.class).size() == 0) {

// or
if (getObjectsInRange(555, Score.class).isEmpty()) {

// or possibly
if (getWorld().getObjects(Score.class).isEmpty()) {
MianBao_Bread MianBao_Bread

2019/11/8

#
danpost wrote...
MianBao_Bread wrote...
I want make a game like pacman, now I want to end the game when all score disappear. This is my Code << Code Omitted >> It's not work, I want to know how to solve it. I am sorry that my English is not good.
The condition in line 1 will always be false. A List object is always returned by getObjectsInRange. You need to check the size of the list returned; or rather, check if it is an empty list
if (getObjectsInRange(555, Score.class).size() == 0) {

// or
if (getObjectsInRange(555, Score.class).isEmpty()) {

// or possibly
if (getWorld().getObjects(Score.class).isEmpty()) {
Thanks! it can work.
You need to login to post a reply.