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

2015/3/4

getWorld() == ??

0gener 0gener

2015/3/4

#
I want to make a method that when my player reaches the top edge of the world, he changes worlds and the player "respawns" at the location i want.... I've got this so far:
public void playerChangeWorld()
{
     if (getY() == 0)
     {
          if (getWorld() instanceof World1)
          {
               Greenfoot.setWorld(new World2());
               setLocation(500,500);
          }  
     }
     if (getY() == 799)
     {
          if (getWorld() instanceof World2)
          {
               Greenfoot.setWorld(new World1());
               setLocation(500,500);
          }
      }
}
He changes the world and spawn the player but somehow the setLocation() doesnt work
davmac davmac

2015/3/4

#
By convention, your class names should begin with capitals - so it should be PokeWorld and PokeWorld2, not pokeWorld and pokeWorld2. Other than that the thing you are missing is adding the player ('this') into the new world. So for example at ilines 7-8 where you have:
               Greenfoot.setWorld(new pokeWorld2());
               setLocation(500,500);
you actually need:
            World newWorld = new pokeWorld2();   
            Greenfoot.setWorld(newWorld);
            newWorld.addObject(this, 500, 500);
And similarly for lines 15-16.
0gener 0gener

2015/3/4

#
Thank you very much, it worked :D
You need to login to post a reply.