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

2016/3/20

Going to the next level by a door

bilibull bilibull

2016/3/20

#
So what I am trying to do is to move to the next level by by just entering a door. I don't want to be able to return back just move on to the next level. I want this to happen for at least 2 levels; so lets say I would go from dungeon to dungeon_2 and dungeon_3. My only concern that using the nextlevel() command will force me in having to create a duplicate actor named player2 in order for that actor to work in the new world (dungeon_2). (at least this is what I have observed has happened so far from my collogues). I am not very sure if this is making any sense what so ever! but in order to easy this confusion that I have created. What I want to do: 1. make a door that when my actor passes a new level loads up. 2. the new level to accept my other classes without conflicting. So I can use the same enemies class for all three levels (if it is plausible) 3. I need this to be 1 or more doors (what ever is easier to understand for a newbie such as myself) for all the other 2 levels. (dungeon_2 & dungeon_3) And please do explain to me how the code works so I can understand what is going on. Thank you for your time, and sorry for the confusing mess.
Super_Hippo Super_Hippo

2016/3/20

#
Do it step by step. You can start like this: Create a new subclass of Actor, name it Door and select an image for it. In the player's act method, you can check if it is touching the door. And if it is, go to the next level:
if (isTouching(Door.class))
{
    Greenfoot.setWorld(new Dungeon_2());
}
Instead of line 3, you could use the following to make sure you create the correct world:
if (getWorld() instanceof Dungeon) Greenfoot.setWorld(new Dungeon_2());
if (getWorld() instanceof Dungeon_2) Greenfoot.setWorld(new Dungeon_3());
bilibull bilibull

2016/3/20

#
Cool, so what does the if (getWorld() instanceof Dungeon) Greenfoot.setWorld(new Dungeon_2()); code exactly mean? I would like to understand how it works so i can use it in the future without having to "copy paste" the code and hope it will work.
danpost danpost

2016/3/20

#
bilibull wrote...
Cool, so what does the if (getWorld() instanceof Dungeon) Greenfoot.setWorld(new Dungeon_2()); code exactly mean? I would like to understand how it works so i can use it in the future without having to "copy paste" the code and hope it will work.
It does exactly what it would appear to do. Every time you use the 'new' keyword, you are creating an instance of a Class. For example 'new Dungeon_2()' creates an instance of the Dungeon_2 class (or, an object of type Dungeon_2). Using 'getWorld' on an actor does not in itself allow determination of what type of subclass that World object was created from. The bottom section of this page discusses this subject.
bilibull bilibull

2016/3/20

#
Thank you very much both of you once again for helping me out! Hopefully in the future I can be the person helping other with code :P
You need to login to post a reply.