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

2022/2/15

Variable for all worlds

jdkdoen05 jdkdoen05

2022/2/15

#
is it possible to create a variable that contains all worlds and if so how?
jdkdoen05 jdkdoen05

2022/2/15

#
public void hitGegnerBullet()
    {
        Actor GegnerBullet = getOneIntersectingObject(GegnerBullet.class);
        if(GegnerBullet != null)
        {
            
            World myWorld2= getWorld();
            Level1 level1 = (Level1)myWorld2;
            HealthBar healthbar =level1.getHealthBar();
            if(hitGegnerBullet == false)
            {
                healthbar.loseHealth();
                hitGegnerBullet = true;
                if(healthbar.health <=0)
                {
                    leben = 0;
                }
            } else {
                hitGegnerBullet = false;
            }
        }
    
    }
so that we don't just limit ourselves to level 1 with this method, but also include all other levels.
danpost danpost

2022/2/15

#
jdkdoen05 wrote...
is it possible to create a variable that contains all worlds and if so how?
Have all your "level" worlds extends from the same World subclass. Refer to my Super Level Support Class scenario.
jdkdoen05 jdkdoen05

2022/2/18

#
So can I just replace this "Level1" with the parent world?
danpost danpost

2022/2/18

#
jdkdoen05 wrote...
So can I just replace this "Level1" with the parent world?
I believe you understand correctly. Just make sure that the health bar codes are moved to the parent class (at least, the field for the HealthBar object and its getter method). Also, make sure you remove the HealthBar fields and getter methods from the child classes.
jdkdoen05 jdkdoen05

2022/2/22

#
Because my next question has to do with variables again, I'll ask it here in this discussion. Is it possible to use a variable to set the number of enemies that spawn in my world so that the game only stops when all enemies have been destroyed?
danpost danpost

2022/2/23

#
jdkdoen05 wrote...
Because my next question has to do with variables again, I'll ask it here in this discussion. Is it possible to use a variable to set the number of enemies that spawn in my world so that the game only stops when all enemies have been destroyed?
Of course.
jdkdoen05 jdkdoen05

2022/2/23

#
How is that possible, because I can't write, for example: "if addObject Gegner" and that would be the only thing that would come to my mind.
danpost danpost

2022/2/23

#
jdkdoen05 wrote...
How is that possible, because I can't write, for example: "if addObject Gegner" and that would be the only thing that would come to my mind.
int gegnersSpawned;
int maxGegners = 10; // adjust as needed

public void addGegner()
{
    if (gegnersSpawned < maxGegners)
    {
        addObject(new Gegner(), 100, 100); // wherever
        gegnersSpawned++;
    }
}

public void checkEndWave()
{
    if (gegnersSpawned == maxGegners && getObjects(Gegner.class).isEmpty())
    {
        // end wave
    }
}
jdkdoen05 jdkdoen05

2022/2/23

#
Thank you for your Help. One last Question: what does "end wave" mean?
danpost danpost

2022/2/23

#
jdkdoen05 wrote...
what does "end wave" mean?
It means all the enemies that are supposed to spawn have been spawned AND removed from the world. Code for what you do in that situation (whether end game or not) would be inserted there at line 17.
jdkdoen05 jdkdoen05

2022/2/24

#
Oh okay thank you very much. This Code has to be in the World subclass, am I right?
danpost danpost

2022/2/24

#
jdkdoen05 wrote...
Oh okay thank you very much. This Code has to be in the World subclass, am I right?
That would be the appropriate place for game control.
jdkdoen05 jdkdoen05

2022/3/1

#
This code doesn't work for us. Maybe it's because we have a ScrollWorld (the one they helped me with in another discussion). Do you maybe have a suggestion for a solution?
danpost danpost

2022/3/1

#
jdkdoen05 wrote...
This code doesn't work for us. Maybe it's because we have a ScrollWorld (the one they helped me with in another discussion). Do you maybe have a suggestion for a solution?
You will need to show your current ending code and be specific and complete as to what you want to happen.
You need to login to post a reply.