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

2016/3/20

Find out how many objects from one class are on the world

1
2
3
ExLegendHD ExLegendHD

2016/3/20

#
Hi, i have a big Problem. In my game the world shall change if all "Point3" and all "Point4" Are collectet by the Players. how i coding it?
public void checkNextLevel()  
    {  
        int actors = numberOfObjects(Point3.class);   
        if (actors == 1)  
        {  
            Greenfoot.setWorld (new Shop(PlayerNumber));
        }  
    }
} this code doesn´t work. pls help :3
Super_Hippo Super_Hippo

2016/3/20

#
If the code is in a World subclass:
if (getObjects(Point3.class).size() == 1)
{
    //...
}
If it is in an Actor subclass:
if (getWorld().getObjects(Point3.class).size() == 1)
{
    //...
}
ExLegendHD ExLegendHD

2016/3/20

#
public void CheckNextLevel()
    {
        if (getObjects(Point1.class).size() == 0 && getObjects(Point2.class).size() == 0 && getObjects(Point3.class).size() == 0 && getObjects(Point4.class).size() == 0 )
        {
            Greenfoot.setWorld (new Shop(PlayerNumber));
        }
    }
i try it in my first level, but it doesnt switch to the shop world
danpost danpost

2016/3/20

#
ExLegendHD wrote...
i try it in my first level, but it doesnt switch to the shop world
Are you sure that there are none of these points left in the world? Are you calling the CheckNextLevel method from the act method of the world class?
ExLegendHD ExLegendHD

2016/3/20

#
I call it in the act yes and no there are no left
danpost danpost

2016/3/20

#
ExLegendHD wrote...
I call it in the act yes and no there are no left
If you are getting any error message, copy/paste it here. If not, you may to post the class codes of one or more of the following: * World subclass * Shop class * Point class(es) or, just upload the scenario with source included here.
ExLegendHD ExLegendHD

2016/3/20

#
you can download it here. https://onedrive.live.com/redir?resid=63673F8BA9F3A8A8!6716&authkey=!AOIxADmyWAuN89A&ithint=folder%2c
danpost danpost

2016/3/20

#
I do not open attachments in mail from unfamiliar sources. Try this -- comment out the call to the CheckNextLevel method in the act method and replace the CheckNextLevel method with the following code; run the scenario to a point where you feel it should proceed to the Shop world; pause the scenario, right-click on the world and manually run the CheckNextLevel method and report back what is printed to the terminal:
public void CheckNextLevel()
{
    System.out.println("Level check");
    int count1 = getObjects(Point1.class).size();
    int count2 = getObjects(Point2.class).size();
    int count3 = getObjects(Point3.class).size();
    int count4 = getObjects(Point4.class).size();
    System.out.println('\n"+count1+"\n"+count2+"\n"+count3+"\n"+count4+"\n");
    if (count1 == 0 && count2 == 0 && count3 == 0 && count4 == 0 )
    {
        System.out.println("success");
        Greenfoot.setWorld (new Shop(PlayerNumber));
    }
}
ExLegendHD ExLegendHD

2016/3/20

#
System.out.println('\n"+count1+"\n"+count2+"\n"+count3+"\n"+count4+"\n");
it shows fails here. fails: illegal character: / unclosed string literal not a statement
danpost danpost

2016/3/20

#
I obviously did not get the shift key down on the first quote mark -- it should be a double quote ( " ).
ExLegendHD ExLegendHD

2016/3/20

#
http://www.greenfoot.org/scenarios/16246
danpost danpost

2016/3/20

#
ExLegendHD wrote...
http://www.greenfoot.org/scenarios/16246
Got it. You can delete it from the site if you wish.
danpost danpost

2016/3/20

#
I do not see what is wrong. When I remove all the Point objects, the method does proceed to the Shop world. Exactly what problem do you think you have? or, maybe I did not test it enough. When will you end up with no points in the world?
ExLegendHD ExLegendHD

2016/3/20

#
i dont understand the question. the level shall chang eto shop when all points on the world are collected
ExLegendHD ExLegendHD

2016/3/20

#
and it also must work for more level. at the end i will have 5 level and after every level i must change world to shop
There are more replies on the next page.
1
2
3