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

2012/11/5

Change to next level

1
2
sirius sirius

2012/11/5

#
Hello, Actually I`m trying to change the level of my world, when I reach en energy of >=50 I tried it a lot, and I also don`t get an error but the level however does not change:S My first world is called Teletubbieswelt and the level/world I want to go to when I reach energy>=50 is called Teletubbieswelt 2 I tried to do it like that:
private void checkNextLevel()
    {
        if (energy >=50) 
        {
            if (level == 1) 
            {
                level = 2;
               
            }
            else {
                level = 1;
               
            }
        }
    }
In the public void act method I did checkNextLevel();
erdelf erdelf

2012/11/5

#
everytime you change the level to two, you change the the level back to one
sirius sirius

2012/11/5

#
Is it because of my else order or why and how do I have to change it?
erdelf erdelf

2012/11/5

#
well, why do you need the else order?
sirius sirius

2012/11/5

#
I don`t know. I just thaught that if I do not have 50 energy, I should stay in level 1. However now I deleted the else order but the level still does not change even if I have mor than 50 energy
erdelf erdelf

2012/11/5

#
a question so I dont do the wrong; you want a new Word class to be active?
sirius sirius

2012/11/5

#
Yes. I want to have add more objects and an enemy into it.
erdelf erdelf

2012/11/5

#
oh, use
    private void checkNextLevel()  
        {  
            if (energy >=50)   
            {  
                if (level == 1)   
                {  
                    greenfoot.setLevel(new Teletubbieswelt2()); 
                     
                }  
                else {  
                    level = 1;  
                     
                }  
            }  
        }  
danpost danpost

2012/11/5

#
Assigning a different number to an 'int' variable ('level') does nothing as far as changing the world! If you have a new world for each level, then having a 'level' variable is not what you need. Just change the world when the value of energy is greater than or equal to fifty (in the first world).
if (energy >= 50) Greenfoot.setWorld(new Teletubbiewelt2());
You will probably have to transfer your actor to that world before exiting the current code block; so, better would be
if (energy >= 50)
{
    Teletubbiewelt2 ttw2 = new Teletubbiewelt2();
    ttw2.addObject(this, "x-location", "y-location");
    Greenfoot.setWorld(ttw2);
}
If your worlds are really the same, but with different actor counts, then it would be better to have a 'level' variable that can be used to restrict the number of different actors within the same world (using just one world).
sirius sirius

2012/11/5

#
I thank you a lot but this time I get an error: cannot find symbol - method setLevel(Teletubbieswelt2()) Shall I show you the code ?
danpost danpost

2012/11/5

#
Make sure that 'greenfoot.' is 'Greenfoot.'; starting with an uppercase letter.
sirius sirius

2012/11/5

#
@ danpost Thank you too. I`m going to try it out:)
sirius sirius

2012/11/5

#
Yeahhhhh thank youu both soo much:)
sirius sirius

2012/11/10

#
I worked on my new world and declared also some new actors but now there I get a new problem and I thought I should do it into here. When I start my game it works without a problem. The level changing is also ok but after a moment of gaming in the new world a terminal Window is opening; java.lang.ClassCastException: Teletubbieswelt2 cannot be cast to Teletubbieswelt at Sonnekind.eatTeletubby1(Sonnekind.java:91) at Sonnekind.act(Sonnekind.java:65) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) Could you help me maybe once again?
danpost danpost

2012/11/10

#
Will need to see the code for Sonnekind (especially with regard to the code around line 91 ). EDIT: also, is the Sonnekind actor placed in both worlds (Teletubbywelt and Teletubbywelt2) or just one of them?
There are more replies on the next page.
1
2