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
Super_Hippo Super_Hippo

2016/3/21

#
actTimer++;
if (actTimer==1650)
{
    //change point to ghost
}
else if (actTimer==2200)
{
    change ghost to point
    actTimer=0;
}
ExLegendHD ExLegendHD

2016/3/21

#
private int ActTimer = 0;
    int changes = 0;
    int actorCount = getWorld().getObjects(Point1.class).size();

    public void act()
    {
        ActTimer++;
        if (ActTimer==1650)
        {
            while (actorCount > 0 && changes < 10)
            {
                Actor actor = (Actor)getWorld().getObjects(Point1.class).get(Greenfoot.getRandomNumber(actorCount));
                getWorld().addObject(new Ghost1(), actor.getX(), actor.getY());
                getWorld().removeObject(actor);
                actorCount--;
                changes++;
            }
        }
        else if (ActTimer==2200)
        {
            while (actorCount > 0 && changes < 10)
            {
                Actor actor = (Actor)getWorld().getObjects(Ghost1.class).get(Greenfoot.getRandomNumber(actorCount));
                getWorld().addObject(new Point1(), actor.getX(), actor.getY());
                getWorld().removeObject(actor);
                actorCount--;
                changes++;
            }
            ActTimer=0;
        }
    }    
The Points doesnt change to Ghosts
Super_Hippo Super_Hippo

2016/3/21

#
In what class is this code located? Stop using 'getWorld' outside methods! (line 3)
ExLegendHD ExLegendHD

2016/3/21

#
http://www.greenfoot.org/scenarios/16249
ExLegendHD ExLegendHD

2016/3/21

#
http://www.greenfoot.org/scenarios/16250 that is the right
Super_Hippo Super_Hippo

2016/3/21

#
I think you can place lines 2+3 at the start of the act-method.
ExLegendHD ExLegendHD

2016/3/21

#
the points dont change
danpost danpost

2016/3/21

#
Super_Hippo wrote...
I think you can place lines 2+3 at the start of the act-method.
Yes -- those two lines should be moved to the beginning of the act method of the class. For the reason no change is being made -- you are not creating a Timer object and placing it in the world for the act method of the Timer class to actually execute.
You need to login to post a reply.
1
2
3