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

2012/5/22

Re-entering levels at different x,ys.

dlanni dlanni

2012/5/22

#
I can't figure out where or how to write some needed code in my project 'Lana's House'. When she leaves the bedroom to the right side of screen, she re-appears in the living room in her game-starting position -- on the left side of the room. I want it to look more natural ie, re-entering living room on right side. Is this possible? Also, I tried re-naming my project to see if it would upload properly, but it still only opens if you press 'Open in Greenfoot'.
danpost danpost

2012/5/22

#
Give Lana an instance int variable indicating the 'lastLevel' she was in. Initialize 'lastLevel' at with level number of room to the left (since she starts on the left). Then in the constructor of the middle room level only (since left and right rooms she always starts at the same location), use:
if (lana.lastLevel == 2) setLocation(...);
else setLocation(...);
lastLevel = 1;
In the other two levels, after setting her location in the constructor, set the 'lastLevel' value to the number of that room.
dlanni dlanni

2012/5/22

#
In class Lana, I added var 'lastLevel' and initialized it to Level3 ( but I think this may be wrong. She starts to the left IN Level1 ). I have 2 constructors in class Level1 : Level1( ) and Level1( Lana lana ). I added the suggested code to Level1() : public Level1() { if (lana.lastLevel == 2) // This and the next 3 lines are additions setLocation(774,191); else setLocation(49,278); lastLevel = 1; this (new Lana()); setPaintOrder(Wine.class,Glass.class, Seen.class,Hall.class,Lana.class ); } Note: I have a setLocation for Lana in the other constructor at the same x,y. When I press compile I get this error message: No suitable constructor found for World() constructor Greenfoot.World.World(int,int,int,boolean) is not applicable..... What did I do wrong? I'll re-load the code with this addition commented out so you can see if I added it to the right place.
danpost danpost

2012/5/22

#
You tried the suggested code in the wrong constructor (you need the one where lana is the parameter). Besides, you tried the code at a place where the Lana class object was not yet created. Anyway, to make the exiting and entering of rooms logical, do this: In Lana class, add these two methods:
// allows another class (the world classes) access to the value of 'lastLevel'
public int getLastLevel()
{
    return lastLevel;
}

// allows another class (the world classes) the ability to change the value of 'lastLevel'
public void setLastLevel(int lev)
{
    lastLevel = lev;
}
Next: in Level1 class, constructor = public Level1(Lana lana) as last statements.
if (lana.getLastLevel() == 2) lana.setLocation(774,191); else lana.setLocation(49,278);
lana.setLastLevel(1);
Finally: in other Level# classes; constructor = public Level#(Lana lana) as last statement.
lana.setLastLevel(#);
dlanni dlanni

2012/5/23

#
Yay! Lana now leaves Level2 Bedroom correctly !! I put all the code from this string and the other in. A few new problems: 1. Lana can't get out the front door at all. When she gets close to it ( probably y<100 ) she is flung back to beginning position (approx x=100, y=450.) 2. When this happens the front door closes!? 3. When she enters the Level 3 bedroom she can't get out ( well, probably she could get out by continuing left down the x axis and end up as if she just came from Level2) So I think that I need to add more to each levels code than setLastLevel. By the way, should I set the LastLevel to the level they are currently occupying or to there LastLevel, which in this construction is always Level1.
You need to login to post a reply.