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

2013/11/17

Need help with collecting a key to unlock a door for my game

alicija alicija

2013/11/17

#
Hello, so for a school project, I need to create a game using Greenfoot. I have decided to create a platformer game where the player collects a key that unlocks the door, which advances you to the next level. I will also be adding enemies in soon but for now, I need help with this: I would like so that when my player comes in contact with the key, the key is removed, but then when the player reaches the door, the door will be unlocked, allowing the player to go through it and advance to the next level. I also have two images for my door. One is a closed door and the other is open. So the door will be closed until the player comes into contact with the key. After that, the door opens. I hope all this makes sense. I am still rather new to greenfoot and have no clue as to how I would code this. thank you! :)
Add a boolean to your player that determines whether or not it has come in contact with the key at all. Then make it that the world changes as the player intersects the door, AND the boolean is true.
alicija alicija

2013/11/17

#
How would I code that? I've got no idea haha :( Thanks for the reply though!
//Not in any method
private boolean touchedKey = false;

//in your player class
public void act()
{
    if (!touchedKey)
        touchedKey = getOneIntersectingObject(Key.class) != null;
    if (getOneIntersectingObject(Door.class) != null && touchedKey)
        changeWorld();
}
This is the code just for the key and "opening" a door.
alicija alicija

2013/11/18

#
I put that code in but it says "cannot find symbol - method changeWorld()" Do I need to change that to the name of my world? Or something else?
You need to make a method called changeWorld() and change your world in that method.
danpost danpost

2013/11/18

#
An easier way is to do the following when creating the level. First create the door (using 'new Door'), then create the key (using 'new Key') and inform the key as to which door it should "unlock". This can be done by either passing the door object as an argument in the constructor call for the key or by calling a 'setDoor' method after creating the key. Either way, you will need an instance object (Door) field in the Key class to hold a reference to the door it opens. When the key is encountered, use the field to get which door it opens and "open" that door.
alicija alicija

2013/11/18

#
Thanks for the reply! It kind of makes sense to me. How would I code that into my game? And for different levels, do I need to create different worlds? Thanks!
danpost danpost

2013/11/18

#
Different levels can be achieved by either using different worlds or by clearing and re-creating the current world. Ways to code a linking between two object could be:
// when creating world (possibly in world class constructor or level constructor)
Door door = new Door();
Key key = new Key(door);
// in Key class
// instance field
private Door door;
// sample constructor
public Key(Door linkedDoor)
{
    door = linkedDoor;
}
// with the above, the linking is complete; the rest puts it to use
// in Player class 'act' method (or in a method it calls)
Key key = (Key)getOneIntersectingObject(Key.class);
if (key != null) key.encountered();
// in Key class
public void encountered()
{
    door.open();
    getWorld().removeObject(this);
}
// in Door class
// instance field
private boolean opened;
// the 'open' method
public void open()
{
    // change image of door
    opened = true;
}
// the 'isOpened' method
public boolean isOpened()
{
    return opened;
}
// in Player class 'act' method (or in a method it calls)
Door door = (Door)getOneIntersectingObject(Door.class);
if (door != null && door.isOpened()) nextLevel();
The 'nextLevel' method in the player class should lead to the construction of the next level (whether done right there or by calling a world class method to do it in).
alicija alicija

2013/11/20

#
Thank you! The code is working fine at the moment! Just a couple of things though: 1. How do I write the code for the 'nextLevel' method. and 2. I have a separate image of the door where it is open. How do I write the code so that as my character grabs the key, the image of the door changes to the open one? Thanks :)
danpost danpost

2013/11/20

#
See line 28 above (this would be in the ''open' method of the Door class). Use the Actor class method 'setImage'. How you create the next level is up to you. You have as yet to show any code you have put together on your own. Also, I even showed where in the code above to change the image of the door, and you asked how to code that, which should be something you would learn early. I realize you may be fairly new, but if you are doing keys and doors, I would think you should at least already know how to set an image. I am just a bit confused on this; maybe you can set me straight.
alicija alicija

2013/11/20

#
Sorry, I didn't take notice of line 28. I've got it all sorted now! Thank you heaps! :)
You need to login to post a reply.