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

2013/5/26

transitioning through worlds

Neon147 Neon147

2013/5/26

#
Hey, i have a problem in my game where the levels do not transitition, like from 1 to 2, and 2 to 3. I can get my ship to proceed to level 2, but after that it wont go to level 3. Can someone please help me figure this out this is my code to figure out what level i am currently on, and which level to proceed too based on that. public int lvl=2; public void checkLevel() { if (getY() == getWorld().getHeight()- getWorld().getHeight()+1) { if (lvl == 2) { getWorld().removeObject(this); Greenfoot.setWorld(new doodleLVL2(this)); lvl =3; } else if (lvl ==3) { lvl =+1; getWorld().removeObject(this); Greenfoot.setWorld(new doodleLVL3Boss(this)); } } }
danpost danpost

2013/5/26

#
It would be better to use something like this:
public void checkLevel()
{
    if (getY()==1)
    {
        World newWorld = null;
        if (getWorld() instanceof doodleLVL1) newWorld = new doodleLVL2(this);
        if (getWorld() instanceof doodleLVL2) newWorld = new doodleLVL3Boss(this);
        if (getWorld() instanceof doodleLVL3Boss) newWorld = new doodleLVL2(this);
    }
    if (newWorld != null) Greenfoot.setWorld(newWorld);
}
Your outer 'if' condition adds and subtracts the height of the world, which cancel each other, so I changed it to 'getY()==1'. Also, you do not need to remove 'this' from the world; as soon as you 'addObject' it into the new world, it will have been removed from this one.
Neon147 Neon147

2013/5/26

#
Thanks, but its still looping around level 2 once it enters level 2 it wont proceed to level 3, even if i change if (getWorld() instanceof doodleLVL1) { newWorld = new doodleLVL2(this); } To -> if (getWorld() instanceof doodleLVL1) { newWorld = new doodleLVL3Boss(this); }
danpost danpost

2013/5/26

#
Run a 'Find' for 'new doodleLVL2' in all your classes. If you find one other than what you posted above, that is probably part of your problem.
Neon147 Neon147

2013/5/26

#
Just did it and new doodleLVL2 is only in my spaceShip class in one instance where it is suppose to be
Neon147 Neon147

2013/5/26

#
I repast what you posted before again just double check i didn't copy wrong and now i am having an issue where my ship just disappears from the screen once getY()==1, i am really confused on what's going here. And i dont know if this really makes a difference but doodleLVL1 is subWorld class, while the other lvl2 and lvl3Boss are world classes
Neon147 Neon147

2013/5/26

#
Nvm i got it, i change the second if statement to an else if statement instead and it worked
danpost danpost

2013/5/27

#
Yeah, I guess your world constructors are adding 'this' into the world immediately and therefore 'getWorld' for the next 'if' will return the new world.
You need to login to post a reply.