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

2013/9/12

Counting the number of a certain class in the world

TobyW TobyW

2013/9/12

#
Hi everyone. I'm making a platform game that involves collecting keys to open a door that leads to the next level. I have a number of ideas of how to implement this, however I am extremely new to Green foot and don't know how to execute them. My first idea was to check to see the number of the actor 'key' that there is in the world, because as the player comes into contact with a key, the key it comes into contact with disappears. Alternatively, I thought of 2 variables 'foundKeys'- created in the player class and incremented by one as it comes into contact with a key and 'totalKeys'- created in the world class and incremented by one every single time the world class creates a key (my world class constructs the level at the beginning of the game). While I've worked out a way for the door class to access the value of foundKeys from the player class, I am unaware of what code I would need for the door class to access the 'totalKeys' variable. Either solution would be great, thanks for your time.
SPower SPower

2013/9/12

#
Well, all you need to store is essentially just an integer, containing information about the key. So, it would be easiest to do something like this: In the key class:
1
2
3
4
5
6
7
8
9
10
11
12
private int tag;
 
// I assume that your class is named 'Key'
public Key(int t)
{
    tag = t;
}
 
public int getTag()
{
    return tag;
}
In your door class:
1
2
3
4
5
6
7
8
9
10
11
12
private int tag;
 
// I assume your class is named 'Door'
public Door(int t)
{
    tag = t;
}
 
public int getTag()
{
    return tag;
}
In the player class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// where you declare your variables:
// replace limit with the limit of keys you want to have picked up in total.
private int[] foundKeys = new int[limit];
private int numKeys = 0;
 
// in the part where you remove a key on contact:
// where key is the key object you picked up
foundKeys[numKeys] == key.getTag();
numKeys++;
 
// you can then use the foundKeys array to check if you've got the needed key, like this:
int keyTag = door.getTag();
for (int i = 0; i < numKeys; i++) {
    if (foundKeys[i] = keyTag) {
        // open door
    }
}
If you need more help, you can always check out the Greenfoot documentation or ask here.
TobyW TobyW

2013/9/12

#
Hey SPower, thanks for taking the time to write that code While it looks great, I'm really sorry, but I don't think I made myself clear enough in my initial post. Each level contains multiple keys, but only one door. The aim was, that once every single key in the level had been collected, the door opened.
SPower SPower

2013/9/12

#
Oh, sorry, maybe I just didn't read your post well enough (that's usually the problem with me and ... reading ... (I love programming but reading is just ....) ) Well, then we'll need to start over. I first suggest you get started yourself on the collision detection using the documentation, but if you can't get it to work, I'll help you of course. Good luck!
TobyW TobyW

2013/9/12

#
I'm actually (I think) almost done. Currently I have 2 variables, 'totalKeys' (which is made in the World.class) and 'foundKeys' which is found in the Player.class. My Player can run around in the level just fine, and when it comes into contact with the key it removes the key from the game world just fine too. Inspecting both the game world (while testing the level) and the Player.class tells me that the World.class accurately records the number of keys in the level, and upon contact with each Key the 'foundKeys' variable is correctly increased by one. The problem only begins when we're talking about the Door.class. I don't know what to write so the Door.class has access to the 'totalKeys' variable from the World.class. Once I know how to do this, I'm fairly confident I know what I'll need to do to get everything else working. Right now literally all I need help with is code that pulls the value of that one variable out of the World.class, so that I can use it later in the code of Door.class
SPower SPower

2013/9/12

#
Have a look at this tutorial, it will probably solve your problem.
SPower SPower

2013/9/12

#
Although, if I understand you correctly, it may be alot easier to do this: In your Door class:
1
2
3
4
5
6
7
8
9
10
11
private Door door;
 
public Key(Door d)
{
    door = d;
}
 
public Door getDoor()
{
    return door;
}
In your player class:
1
2
3
4
// where you pickup a key
Door d = key.getDoor();
getWorld().removeObject(d);
// remove key
And then when you create a Key object, you'll have to do something like this:
1
2
3
Door d1 = new Door();
Key k1 = new Key(d1);
// so picking up k1 will cause d1 to disappear
You can of course change the line where you remove the door, to something where you 'open' it instead.
danpost danpost

2013/9/12

#
If any basic key can unlock any basic door, then all you need to do is:
1
2
3
4
5
if (getWorld().getObjects(Key.class).size() < getWorld().getObjects(Door.class).size())
{
    Actor door = getOneIntersectingObject(Door.class);
    if (door != null) getWorld().removeObject(door);
}
With this, you do not need to keep track of ANY amounts. If there is a special key and door, you can use separate classes for those classes and work them separately.
You need to login to post a reply.