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

2013/6/17

made 3 levels and level 1 and level 2 start fine, but level 3 won't start?

dangamecreator dangamecreator

2013/6/17

#
This is my code:
  private void checkNextLevel()
    {
        if (getX() == getWorld().getWidth()-1) {
            if (level == 1) {
                level = 2;
                getWorld().removeObject(this);
                Greenfoot.setWorld(new Rocks(this));
            }
            else {
                level = 3;
                getWorld().removeObject(this);
                Greenfoot.setWorld(new Desert(this));
            }
            if (level == 2){
                level = 3;
                getWorld().removeObject(this);
                Greenfoot.setWorld(new Level3(this));
            }
        }
    }
JetLennit JetLennit

2013/6/17

#
Try
private void checkNextLevel()  
  {  
      if (getX() == getWorld().getWidth()-1) {  
          if (level == 1) {  
              level = 2;  
              getWorld().removeObject(this);  
              Greenfoot.setWorld(new Rocks(this));  
          }  
          else if (level == 2) {  
              level = 3;  
              getWorld().removeObject(this);  
              Greenfoot.setWorld(new Desert(this));  
          }  
          else if (level == 3){  
              level = 4;  
              getWorld().removeObject(this);  
              Greenfoot.setWorld(new Level3(this));  
          }  
      }  
  }  
dangamecreator dangamecreator

2013/6/17

#
thanks, but now I can't even go to level 2
JetLennit JetLennit

2013/6/17

#
I think i see the problem..
private void checkNextLevel()    
  {    
      if (getX() == getWorld().getWidth()-1) {    
          if (level == 0) {    
              level = 1;    
              getWorld().removeObject(this);    
              Greenfoot.setWorld(new Rocks(this));    
          }    
          else if (level == 1) {    
              level = 2;    
              getWorld().removeObject(this);    
              Greenfoot.setWorld(new Desert(this));    
          }    
          else if (level == 2){    
              level = 3;    
              getWorld().removeObject(this);    
              Greenfoot.setWorld(new Level3(this));    
          }    
      }    
  }   
dangamecreator dangamecreator

2013/6/17

#
it just resets my level, but I think the problem is the else if.
danpost danpost

2013/6/17

#
First, you do not have to remove 'this' from one world before adding it to another. Second, it is better to rely on what world is actually active, than to rely on a field that may or may not be set properly.
private void checkNextLevel()
{
    if (getX() == getWorld().getWidth()-1)
    {
        if (getWorld() instanceof Desert) Greenfoot.setWorld(new Level3(this));
        if (getWorld() instanceof Rocks) Greenfoot.setWorld(new Desert(this));
        if (getWorld() instanceof FirstWorld) Greenfoot.setWorld(new Rocks(this));
    }
}
Replace 'FirstWorld' with the name of the world that precedes the 'Rocks' world (whatever level '0' refers to).
dangamecreator dangamecreator

2013/6/17

#
Typed in the code, but there was a problem. Go to my scenario at: http://www.greenfoot.org/scenarios/8782. You will see the problem as soon as you go to the end of the world.
danpost danpost

2013/6/17

#
I looks like it instantiates the same world over and over. Maybe you should upload the source and let someone help you with it.
dangamecreator dangamecreator

2013/6/17

#
Thanks for helping, I managed to fix the problem :)
dangamecreator dangamecreator

2013/6/17

#
This is what I had to do private void checkNextLevel() { if (getX() == getWorld().getWidth()-1) { if (getWorld() instanceof Desert) Greenfoot.setWorld(new Level3(this)); if (getWorld() instanceof Rocks) Greenfoot.setWorld(new Desert(this)); if (getWorld() instanceof Desert) Greenfoot.setWorld(new Level3(this)); } }
You need to login to post a reply.