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

Report as inappropriate.

meazalplaq
meazalplaq presents ...

2014/11/9

Mouse Click Game: Keys and Doors

Hello,
I am new to Greenfoot and programming. I am trying to start off with a simple project that involves acquiring a key to open a door. Right now, the player can get the key, but it does not matter. The door will open and proceed to the next scene regardless of the key. My question is how do I do I get the door to only open after the player has obtained the key? Are there any scenarios similar to this that I can study to understand the process? Any examples or guidance would be appreciated.

Thank you,

2800 views / 18 in the last 7 days

Tags: with-source

open in greenfoot
Your browser does not support the canvas tag.
A new version of this scenario was uploaded on Sun Nov 09 20:33:04 UTC 2014
A new version of this scenario was uploaded on Sun Nov 09 20:33:43 UTC 2014
danpostdanpost

2014/11/9

You could use the extra condition (besides the door being clicked on) that no key object be in the world: if ( getWorld().getObjects(Key.class).isEmpty() && Greenfoot.mouseClicked(this) )
meazalplaqmeazalplaq

2014/11/9

Hello danpost, It worked! So the statement is basically saying that if Key.class is no longer in the scene, the code will execute and load the next location? . Thank you for your help. Thanks,
A new version of this scenario was uploaded on Sun Nov 09 22:12:14 UTC 2014
A new version of this scenario was uploaded on Mon Nov 10 04:01:51 UTC 2014 Hello, I am trying to create a scenario where the door will only open if the wires are placed in their correct locations (red connected to the red button, blue connected to the blue button, and yellow connected to the yellow button) What is a good place to start implementing this mechanism for opening the door? Could I use the same approach with the key? Should I use a series of Boolean statements? Are there any examples from Greenfoot projects that I could look at? Thank you for any help or guidance.
A new version of this scenario was uploaded on Thu Nov 13 13:50:03 UTC 2014
A new version of this scenario was uploaded on Thu Nov 13 13:50:17 UTC 2014
meazalplaqmeazalplaq

2014/11/13

Thank you for your help; however, I have noticed that the code only seems to work if the key.class and the location.class are populated from the World.class at the start of the game. Is there any way to adjust the code so the door will unlock if the key is added from anywhere in the game? For example, there is a door on Location_03, but the player has to go to Location_04 or 05 to get the key and then go back to Location_03 to unlock the door. How would I approach creating code for that scenario? Any ideas? Thank you,
danpostdanpost

2014/11/13

You can start a new discussion thread on that issue (it is easier to view code there).

Want to leave a comment? You must first log in.

Who likes this?

No votes yet.