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

2017/2/25

How do I create an inventory?

1
2
3
4
danpost danpost

2017/3/2

#
SallySanban wrote...
I would put them in the parameter of the door. I just removed them because it didn't work at the time.
Well, the door will not find its key if you manually add one into the world later. It is never told what key to look for. For testing, do this: (1) manually add a StarKey object into the world; (2) right click on the key and select 'Inspect...'; (3) manually add a JanitorDoor object into the world using the name of the key (at the very top of the 'Inspect' frame); (4) continue with normal testing (have player collect that key and enter that door) That should prove the test.
SallySanban SallySanban

2017/3/2

#
danpost wrote...
SallySanban wrote...
If I add in from the World class it still shows up when I go back to that background, even if I clicked it already
When you create a new background, you can immediately remove anything that is already in the inventory:
Alleyway4 aw4 = new Alleyway4(type, lastType);
aw4.removeObjects(inventory.collectedObjects);
Wouldn't that delete the items inside the list every time I change background?
danpost danpost

2017/3/2

#
SallySanban wrote...
Alleyway4 aw4 = new Alleyway4(type, lastType);
aw4.removeObjects(inventory.collectedObjects);
Wouldn't that delete the items inside the list every time I change background?
It would only remove objects from the world -- the list will stay intact. "every time": -- that would depend on how, or where, you are using it. If that is what you want, you could just add the following line at the end of your world constructor (the one with the two int parameters):
removeObjects(inventory.collectedObject);
SallySanban SallySanban

2017/3/3

#
What is the difference between that code and the inventory.collectedObjects.clear(); one?
SallySanban SallySanban

2017/3/3

#
danpost wrote...
SallySanban wrote...
I would put them in the parameter of the door. I just removed them because it didn't work at the time.
Well, the door will not find its key if you manually add one into the world later. It is never told what key to look for. For testing, do this: (1) manually add a StarKey object into the world; (2) right click on the key and select 'Inspect...'; (3) manually add a JanitorDoor object into the world using the name of the key (at the very top of the 'Inspect' frame); (4) continue with normal testing (have player collect that key and enter that door) That should prove the test.
This works! However, after I click the door, get out of the room (switch the background back to original) and then try to click the door again, it doesn't work anymore. Also, would the contains() also work if I add the key into the World without doing it manually? How would I add the key (or any clue) into the World without doing it manually so that if I click it already (and it adds to the collectedObjects list), it won't show up in that spot in the background again?
danpost danpost

2017/3/3

#
SallySanban wrote...
What is the difference between that code and the inventory.collectedObjects.clear(); one?
The 'inventory.collectedObjects.clear()' one removes everything from the inventory; but the 'removeObjects(inventory.collectedObjects)' one removes all listed object that are currently in the world from the world (not from the list). The first one clears the inventory of all listed objects and the second one removes only the objects that are both in the world and in the list from the world.
danpost danpost

2017/3/3

#
SallySanban wrote...
This works! However, after I click the door, get out of the room (switch the background back to original) and then try to click the door again, it doesn't work anymore.
Is it the same door and you adding the listed key back into the world or another one? You could remove the key from the list when the door is used and add a fresh set of door and key when returning to that room.
Also, would the contains() also work if I add the key into the World without doing it manually?
Yes ... well, the contains() will always work; but, to have it work the way you want, you need to keep things in order by making sure that the key in the world is assigned to that door so the door can see if it was collected or not. If it is the wrong key or it is not assigned properly, then the door will not find it in the list.
How would I add the key (or any clue) into the World without doing it manually so that if I click it already (and it adds to the collectedObjects list), it won't show up in that spot in the background again?
When you create the door, you assign a key to it. You could actually have the door create the key for itself. Since the door has a reference to the key, it is available to add into the world whenever conditions are correct for it. If you place the 'removeObjects' line:
removeObjects(inventory.collectedObject);
at the end of your Alleyway4(int, int) constructor block, all items that were already collected and put back in the world will be removed again (like they were never actually put back in the world).
I believe I have a clear understanding of the issue now. Hence, the strike over the last part. Hoping to come up with a viable solution.
danpost danpost

2017/3/3

#
The problem is a tenuous one. If the worlds were retained so that they can be returned to, as is, then there will be an awful lot of memory being used for the background images of the worlds. You could try to save the worlds so that you can return to them; but I have a feeling that you will run into some memory issues. There may be a way to "save" the world set-up without saving the actual world. It would involve a little manipulating, but it should help as far as memory issues are concerned. The idea is to create an unbounded world of size (1, 1, 1) -- (pixel-sized) -- and add all objects currently in the current world at the same location in this new world. Then do the reverse when going back to the current world. These saving worlds can be retained in a static array. It would be something like this:
// after the background image array
private static World[] worlds = new World[bgImages.length];

// new world constructor for saving worlds
public Alleyway4(World world)
{
    super(1, 1, 1, false);
    for (Object obj : world.getObjects())
    {
        Actor actor = (Actor)obj;
        addObject(actor, actor.getX(), actor.getY());
    }
}

// modify your 'changeBackground' method
public void changeBackground(int change)
{
    worlds[type] = new Alleyway4(this);
    Greenfoot.setWorld(new Alleyway4(type+change, type);
}

// do this with the switch in the Allyway4(int, int) constructor
if (worlds[type] == null)
{
    switch....
}
else
{
    for (Object obj : worlds[type].getObjects())
    {
        Actor actor = (Actor) obj;
        addObject(actor, actor.getX(), actor.getY());
    }
}
I think I got that right (I did not test it).
SallySanban SallySanban

2017/3/3

#
danpost wrote...
SallySanban wrote...
This works! However, after I click the door, get out of the room (switch the background back to original) and then try to click the door again, it doesn't work anymore.
Is it the same door and you adding the listed key back into the world or another one? You could remove the key from the list when the door is used and add a fresh set of door and key when returning to that room.
I believe it was another one. When I went back to the room, I think I forgot to manually add the door with the key parameter, so that's probably why it didn't work. Right now, though, if I add the key and the door through the World class and not manually, I cannot click on the door and the background doesn't switch. Ah, does the order of adding the objects matter? I added the key before I added the door from the World and it became clickable. It didn't work when I added the door before the key.
SallySanban SallySanban

2017/3/3

#
To add the key to the World only if it's not inside the collectedObjects list, I tried this in the World class:
public Alleyway4(int worldType, int lastWorldType)
    {
        //etc.
        DiamondKey diamondkey = new DiamondKey();

        switch(type)
        {
            //etc.
            case 11: if(!inventory.inventoryObjects.contains(diamondkey)){addObject(diamondkey, 100, 100);}; addObject(new StudentDoor(diamondkey), 505, 195); break;
            //etc.
        }
    }
It does not work, though. The key is still added when I go back to the original room after clicking the door and entering the other room, even though when I press 'x', the key is shown.
danpost danpost

2017/3/3

#
That is because you are creating a new key for each world, one that is not in the list.
SallySanban SallySanban

2017/3/3

#
danpost wrote...
That is because you are creating a new key for each world, one that is not in the list.
How do I fix it?
danpost danpost

2017/3/3

#
SallySanban wrote...
How do I fix it?
See my previous post (the one immediately after the strike-out),
SallySanban SallySanban

2017/3/3

#
That worked! I don't really understand the code, though. Could you explain it, please?
SallySanban SallySanban

2017/3/3

#
I am now able to collect the key. It can unlock the door assigned to it, but when I switch backgrounds (go left or right, for example) and then try to click the door again, it doesn't work. I'm wondering if the door that I click after I go left or right is assigned to a new key which is not in the list again?
There are more replies on the next page.
1
2
3
4