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

2016/3/20

#
The problem is from where you are calling the method from. It needs to be in an act method -- not in the constructor of the world.
public void act()
{
    CheckNextLeveL();
}
ExLegendHD ExLegendHD

2016/3/20

#
oh no .... am i really so stupid? but realy thank you! you save my ass.
danpost danpost

2016/3/20

#
ExLegendHD wrote...
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
Sounds like the Shop world will need to know which world to proceed to. You could pass this along with PlayerNumber to the Shop class from each level:
// Level1 code
Greenfoot.setWorld(new Shop(PlayerNumber, new Level2()));
The Shop world can hold the world in a reference field:
// Shop code

// instance field
private World gotoWorld;

public class Shop(int PlayerNumber, World world)
{
    gotoWorld = world;
    // etc.
with a method to return the world
public World getNextWorld()
{
    return gotoWorld;
}
Then the button can get the world:
Greenfoot.setWorld(((Shop)getWorld()).getNextWorld());
ExLegendHD ExLegendHD

2016/3/20

#
// Shop code
 
// instance field
private World gotoWorld;
 
public class Shop(int PlayerNumber, World world)
{
    gotoWorld = world;
    // etc.
public World getNextWorld()
{
    return gotoWorld;
}
where exactly they must be?
ExLegendHD ExLegendHD

2016/3/20

#
ok i think i have it ... it fails by
// Level1 code
Greenfoot.setWorld(new Shop(PlayerNumber, new Level2()));
danpost danpost

2016/3/20

#
ExLegendHD wrote...
ok i think i have it ... it fails by
// Level1 code
Greenfoot.setWorld(new Shop(PlayerNumber, new Level2()));
What message are you getting?
ExLegendHD ExLegendHD

2016/3/20

#
constructor error
danpost danpost

2016/3/21

#
ExLegendHD wrote...
constructor error
Try: new Level2(PlayerName)
ExLegendHD ExLegendHD

2016/3/21

#
i have another question. It is possible to remove 10 objects of the same actor and let 10 oter actor spawn on ther positions?
danpost danpost

2016/3/21

#
ExLegendHD wrote...
i have another question. It is possible to remove 10 objects of the same actor and let 10 oter actor spawn on ther positions?
Of course it is possible. A simple 'while' loop might be sufficient here. Something like this:
int changes = 0;
int actorCount = getObjects(ClassA.class).size();
while (actorCount > 0 && changes < 10)
{
    Actor actor = (Actor)getObjects(ClassA.class).get(Greenfoot.getRandomNumber(actorCount));
    addObject(new ClassB(), actor.getX(), actor.getY());
    removeObject(actor);
    actorCount--;
    changes++;
}
If this is to be coded in an Actor subclass, all World class methods must be preceded by 'getWorld().'.
ExLegendHD ExLegendHD

2016/3/21

#
public static int ActivateGhost = 0;
    private int ActTimer = 0;
    int changes = 0;
    int actorCount = getWorld().getObjects(Point1.class).size();
    
    public void act()
    {
        ActTimer++;
        if (ActTimer == 40)
        {
            if (ActivateGhost == 0)
            {
                ActivateGhost = 1;
            }
            else
            {
                ActivateGhost = 0;
            }
            ActTimer = 0;
        }
        if (ActivateGhost == 1)
        {
            while (actorCount > 0 && changes < 10)
            {
                Point1 actor = (Point1)getWorld().getObjects(Point1.class).get(Greenfoot.getRandomNumber(actorCount));
                getWorld().addObject(new Ghost1(), actor.getX(), actor.getY());
                getWorld().removeObject(actor);
                actorCount--;
                changes++;
            }
            ActTimer++;
        }
        else
        {
            while (actorCount > 0 && changes < 10)
            {
                Ghost1 actor = (Ghost1)getWorld().getObjects(Ghost1.class).get(Greenfoot.getRandomNumber(actorCount));
                getWorld().addObject(new Point1(), actor.getX(), actor.getY());
                getWorld().removeObject(actor);
                actorCount--;
                changes++;
            }
        }
why it doesnt work?
Super_Hippo Super_Hippo

2016/3/21

#
Seriously... it doesn't work... why don't you say what happens, what should happen or if there is any error message, show it? In this case, line 4 shouldn't work because 'getWorld' will return 'null'.
ExLegendHD ExLegendHD

2016/3/21

#
its a typ of pac man. i have points on the map ( Point 1 / 2 / 3 / 4 ) which shall go all 30 sec to ( Ghost 1 / 2 / 3 / 4) for 10 sec after 10 sec they shall get back to Point 1 / 2 / 3 / 4
Super_Hippo Super_Hippo

2016/3/21

#
Then you should compare 'ActTimer' to 30*55 and 40*55 (there are about 55 act-cycles each second) and use lines 23-30 and 35-41 in their if-block.
ExLegendHD ExLegendHD

2016/3/21

#
What shall i do with the ActTimer? An Line 23-30 into what?
There are more replies on the next page.
1
2
3