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

2012/5/22

How to exit only when door is open.

dlanni dlanni

2012/5/22

#
I just added an outside to Lana's House. I want players to only be able to enter if front door is open (image2) In my checkNextLevel method I added: if (getY() < 100) { if (level == 1) { if (getWorld.getDoor =(image 2)) // This is the line I need help with. { level = 4; getWorld().removeObject(this); Greenfoot.setWorld(new Level4(this));} else { level = 1; getWorld().removeObject(this); Greenfoot.setWorld(new Level1(this));} } } Do I need to get to Door class to find out if it is open? Can I go a shorter route ie, getActor.getDoor? Is there a method that asks for image condition? Thanks.
dlanni dlanni

2012/5/22

#
Maybe I should just cut out a piece of the door at the top, set it as a collision, so that she cannot get to y=100 (or whatever), then remove both pieces when the mouse is clicked. But there must be a better way to do this.
IsVarious IsVarious

2012/5/22

#
Just use a LookforActor code, and then setup the conditions you want to happen next. Also you'll want to use a boolean variable to be your switch, if it's set to true, the door is open, if it's false, the door is closed. That code might look something like this...
public void lookForPlayer()
{       // You'll want to change the actor class to whatever it is that is the person, or main actor which the player controls.
      if ( canSee(Actor.class))
      {
          if (doorOpen == true)
       {
         setDoorOpenImage         
      // have player go through door
       }                  
}
This is mostly epsudo code, but the logic is here.
danpost danpost

2012/5/22

#
I change the end of Level1 constructor to the following to allow for level 4:
if (lana.getLastLevel() == 2) lana.setLocation(774,191);
else if (lana.getLastLevel() == 4) 
{
    Greenfoot.playSound("door-open-7.wav");
    lana.setLocation(390,105);
}
else lana.setLocation(49,278);
lana.setLastLevel(1);
Next: in the Door class, add the following method:
// allow other classes access to the value of 'opened'
public boolean getOpened()
{
    return opened;
}
Now, to get through the door, in Lana class (in checkNextLevel method), make the first 'if' block (where y < 100):
if (getY() < 100)
{
    if (level == 1 && ((Door) getWorld().getObjects(Door.class).get(0)).getOpened()){
        level = 4;
        getWorld().removeObject(this);
        Greenfoot.setWorld(new Level4(this));
    }
    else {
        level = 1;
        getWorld().removeObject(this);
        Greenfoot.setWorld(new Level1(this));
    }
}
dlanni dlanni

2012/5/22

#
WOW!! Thanks so much.
dlanni dlanni

2012/5/23

#
The door code compiled. I added the first code at the end of Level1( ) constructor and got error message, 'There is no getLastLevel method'. So, I went to class Lana and made 2 new var; getLastLevel and setLasLevel, and set them both to 3. Next, I wrote method stubs for them and ended up just making them all self referential. ie 'return lastLevel;' for body of method, Still wouldn't compile so I tried , putting constructor code in Other Level1(Lana lana) constructor. Got same error message, No method setLastLevel etc. So I declared variables of them in Level1 constructor to no avail. When I substituted the new 'getY( )<100', 2 odd things happened. The first was that whenever she got close to the open front door, she would be transported back to her starting point at lower left of screen. Also, the front door closes. I commented out all the code that didn't work plus my attempts to fix it. It's too bad that your code: && ((Door) getWorld().getObjects(Door.class).get(0)).getOpened()) isn't working for me. Anyway, all the code is in Lana and Level1 and in BLUE. When you get a chance, would you take another look at the code to see what I've done wrong? Also, I tried to add the first step of wine drinking. If click on glass, GUI should query whether you wish to drink. I copied the code from a project I worked on a month ago, imported the java.awt, and it still errored me , saying 'What is getBackground?' I am too tired to look up the documentation tonight. Is there a really simple button or check box program that you know of, where the words go away after clicking answer? Thanks again for your generous help.
dlanni dlanni

2012/5/23

#
Pay no attention to last bit. I had overlooked your further answer on setting up get and set LastLevel in response to my other post. I will go back to this and do it all from the beginning. Thanks,Thanks,Thanks.
dlanni dlanni

2012/5/23

#
I think the problem is with the first line 7: 'else lana.setLocation(49,278);' I think it may need a 'if lastLevel = 3 setLocation(49,278); else if level=level 1, proceed to outdoors. I don't know why exiting the Level3 has stopped?
danpost danpost

2012/5/23

#
One problem is that you 'setLastLevel(1)' in all for levels. To fix, the last statement in each of the level constructors that take Lana as a parameter, in the setLastLevel statement, change the parameter from '1' to that level's number (level 1 would stay the same, of course). In Lana class, change the '> 820' in line 262 to '== getWorld().getWidth() - 1'. Should look like this:
if (getX() ==  getWorld().getWidth() - 1 ) {
Also, change line 38 to 'lastLevel = 1;'.
danpost danpost

2012/5/23

#
I wish I had time to figure out the front door problem right now, but I really have to go. Sorry. :(
dlanni dlanni

2012/5/23

#
That's OK. I have a clunky solution. I'll go back to where she could leave door frreely and add a piece of door that will act as a collision detection, keeping her in. Then when the door and Lintel are removed, she can go out. Thanks for helping me get my project working well.
danpost danpost

2012/5/23

#
I decided to stick around. In your Level1 class lines 42 and 43 need deleted! That will fix the door issue. Also, I re-wrote your checkNextLevel() method in the Lana class and came up with this:
public void checkNextLevel()
{
    int newLevel = 0;
    if (level == 1 && getY() < 100 && ((Door) getWorld().getObjects(Door.class).get(0)).getOpened()) newLevel = 4;
    if (level == 1 && getX() == getWorld().getWidth() - 1) newLevel = 2;
    if (level == 1 && getX() == 0) newLevel = 3;
    if (level == 2 && getX() ==  0) newLevel = 1;
    if (level == 3 && getX() == getWorld().getWidth() - 1) newLevel = 1;
    if (level == 4 && getX() < 260) newLevel = 1;
    if (newLevel == 0) return;
    level = newLevel;
    getWorld().removeObject(this);
    switch (level) {
        case 1: Greenfoot.setWorld(new Level1(this)); break;
        case 2: Greenfoot.setWorld(new Level2(this)); break;
        case 3: Greenfoot.setWorld(new Level3(this)); break;
        case 4: Greenfoot.setWorld(new Level4(this)); break;
    }
 }
Much cleaner and easier to read! OK, now I feel better about leaving!
dlanni dlanni

2012/5/24

#
Thanks so much. I'll get to work on this .
You need to login to post a reply.