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/3

#
SallySanban wrote...
That worked! I don't really understand the code, though. Could you explain it, please?
Line 2 creates an array for World object -- its size is set to the number of background images you have in the 'bgImages' String array. Line 5 through 13 is a world constructor that creates a pixel-sized unbounded world and is passed a reference to the current world. Its only function is to keep all the objects in a particular world until such time as you create a new world of that type. So all objects in the world passed to it are placed in it at the same locations (that is what lines 8 through 12 do). Line 18 was added to the 'changeBackground' method to save the world that you are leaving. Line 23 checks to see if you were in the room now being constructed. If you were not, then the switch will add the new objects; if you were, then the objects saved in the saver world are added back into the newly constructed world. If implemented properly, both the door and the key will be the same exact ones you originally had and the door should be able to see that the key is still in the inventory. Make sure that you do not have the door removing the key from the inventory when entered into.
SallySanban SallySanban

2017/3/4

#
I'm not removing the key from the inventory. I don't know what happened, but now the door won't switch background anymore if I've already entered the room once.
danpost danpost

2017/3/4

#
SallySanban wrote...
I'm not removing the key from the inventory. I don't know what happened, but now the door won't switch background anymore if I've already entered the room once.
Please show your revised world class code.
SallySanban SallySanban

2017/3/4

#
Here you go:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Alleyway4 extends World
{
    public int type;
    private static final UserWalk1 player = new UserWalk1(new EloraWalk1());
    private static final Inventory inventory = new Inventory();
    DiamondKey diamondkey = new DiamondKey();
    private int changeLeft = 91, changeRight = 519;
    private static final String[] bgImages =
        {
            "Alleyway 4.PNG", //0
            "Kalachuchi Walk 1.JPG", //1
            "School Entrance.JPG", //2
            "Lockers 1.JPG", //3
            "Lockers 2.JPG", //4
            "Cafeteria Entrance.JPG", //5
            "Cafeteria 1.JPG", //6
            "Cafeteria 2.PNG", //7
            "Cafeteria Entrance Stairs.JPG", //8
            "Classroom Entrance.JPG", //9
            "Classroom 1.jpg", //10
            "Student Bathroom Entrance.JPG", //11
            "Student Bathroom.JPG", //12
            "Classroom Entrance Stairs.JPG", //13
            "Library Entrance.JPG", //14
            "Library 1.JPG", //15
            "Library 2.JPG", //16
            "Faculty Study Entrance.PNG", //17
            "Faculty Study.JPG", //18
            "Principal's Office.JPG", //19
            "Teacher Bathroom Entrance.JPG", //20
            "Female Teacher Bathroom.JPG", //21
            "Male Teacher Bathroom.JPG", //22
            "Basement Entrance.JPG", //23
            "Janitor's Closet.JPG", //24
            "Dark Room.PNG" //25
        };
    private static World[] worlds = new World[bgImages.length];

    public Alleyway4()
    {
        this(0, -1);
        inventory.collectedObjects.clear();
    } 

    public Alleyway4(World world)
    {
        super(1, 1, 1, false);
        for(Object obj: world.getObjects(Clues.class))
        {
            Actor actor = (Actor)obj;
            addObject(actor, actor.getX(), actor.getY());
        }
    }

    public Alleyway4(int worldType, int lastWorldType)
    {
        super(600, 400, 1);
        type = worldType;
        setBackground(new GreenfootImage(bgImages[type]));
        int x, y = 243; //where to add User
        int xElora, yElora = 270; //where to add Elora

        if(lastWorldType < worldType) //moved right
        {
            x = 154;
            xElora = 33;
        }
        else //moved left
        {
            x = 500;
            xElora = 578;
        }

        if (worlds[type] == null)
        {
            switch(type)
            {
                case 2: addObject(new Gate(), 553, 306); break;
                case 5: addObject(new CafeteriaDoor(), 429, 146); break;
                case 8: addObject(new CafeteriaStairs(), 536, 323); addObject(new CafeteriaDoor2(), 234, 176); break;
                case 9: addObject(new ClassroomDoor(), 368, 202); break;
                case 11: if(!inventory.inventoryObjects.contains(diamondkey)){addObject(diamondkey, 100, 100);}; addObject(new StudentDoor(diamondkey), 505, 195); break;
                case 13: addObject(new ClassroomStairs(), 505, 307); break;
                case 14: addObject(new LibraryDoor(), 303, 198); break;
                case 17: addObject(new FacultyDoor(), 76, 177); addObject(new PrincipalDoor(), 498, 175); break;
                case 20: addObject(new GirlDoor(), 265, 187); addObject(new BoyDoor(), 66, 189); break;
                case 23: addObject(new JanitorDoor(), 102, 208); addObject(new BasementDoor(), 478, 210); break;
            }
        }
        else
        {
            for(Object obj: worlds[type].getObjects(Clues.class))
            {
                Actor actor = (Actor) obj;
                addObject(actor, actor.getX(), actor.getY());
            }
        }

        addObject(player, x, y);
        addObject(player.getEloraWalk(), xElora, yElora);
        addObject(inventory, 45, 361);
        setPaintOrder(Texts.class, TextBox.class, Characters.class, Props.class, Overlaps.class, UserWalk1.class, EloraWalk1.class, SipmaMini.class, Doors.class);
    }

    public static Inventory getInventory()
    {
        return inventory;
    }

    public void changeBackground(int change) //1 for right, -1 or left
    {
        //Greenfoot.setWorld(new Alleyway4(type + change, type));
        worlds[type] = new Alleyway4(this);
        Greenfoot.setWorld(new Alleyway4(type+change, type));
    }

    public boolean atRightEdge()
    {
        if(player.getX() == 510 && type == 5)
        {
            return true;
        }
        else if(player.getX() == 510 && type == 10)
        {
            return true;
        }
        else if(player.getX() == 510 && type == 12)
        {
            return true;
        }
        else if(player.getX() == 510 && type == 14)
        {
            return true;
        }
        else if(player.getX() == 510 && type == 18)
        {
            return true;
        }
        else if(player.getX() == 510 && type == 19)
        {
            return true;
        }
        else if(player.getX() == 510 && type == 21)
        {
            return true;
        }
        else if(player.getX() == 510 && type == 22)
        {
            return true;
        }
        else if(player.getX() == 510 && type == 24)
        {
            return true;
        }
        else if(player.getX() == 510 && type == 25)
        {
            return true;
        }
        return false;
    }

    public boolean atLeftEdge()
    {
        if(player.getX() == 100 && type == 8)
        {
            return true;
        }
        return false;
    }

    public void act()
    {
        int x = player.getX();

        if(x < changeLeft && type == 0) //Stopping rule for FIRST background
        {
            player.setLocation(91, 243);
        }
        else if(x < changeLeft && type != 0) //General moving rule to the LEFT 
        {
            changeBackground(-1);
            if(type == 11)
            {
                changeBackground(-2);
            }
            else if(type == 13)
            {
                changeBackground(-2);
            }
            else if(type == 19)
            {
                changeBackground(-2);
            }
            else if(type == 20)
            {
                changeBackground(-3);
            }
            else if(type == 22)
            {
                changeBackground(-2);
            }
            else if(type == 23)
            {
                changeBackground(-3);
            }
            else if(type == 25)
            {
                changeBackground(-2);
            }
        }
        else if(x > changeRight && type != 23) //General moving rule to the RIGHT
        {
            changeBackground(1);
            if(type == 9)
            {
                changeBackground(2);
            }
            else if(type == 11)
            {
                changeBackground(2);
            }
            else if(type == 17)
            {
                changeBackground(3);
            }
            else if(type == 20)
            {
                changeBackground(3);
            }
        }
        else if(x > changeRight && type == 23) //Stopping rule for LAST background
        {
            player.setLocation(519, 243);
        }
    }
}
I just found out that the door isn't added to the World when I return backgrounds. It happens to every background. If I move right and then go back to the previous background in the left, the objects in the switch statement aren't added to the World. The same goes for when I enter a room by clicking a door and go back to the original room. It seems the switch statement doesn't add the objects if I go back a background, because it's not null anymore.
danpost danpost

2017/3/5

#
Remove the 'if' condition from line 90 (just add the objects). Also, on line 100, change 'Clues.class' to 'null'.
SallySanban SallySanban

2017/3/5

#
Here's what I did:
public Alleyway4(World world)
    {
        super(1, 1, 1, false);
        for(Object obj: world.getObjects(Clues.class))
        {
            Actor actor = (Actor)obj;
            addObject(actor, actor.getX(), actor.getY());
        }
    }

    public Alleyway4(int worldType, int lastWorldType)
    {
        super(600, 400, 1);
        type = worldType;
        setBackground(new GreenfootImage(bgImages[type]));
        int x, y = 243; //where to add User
        int xElora, yElora = 270; //where to add Elora
        
        if(lastWorldType < worldType) //moved right
        {
            x = 154;
            xElora = 33;
        }
        else //moved left
        {
            x = 500;
            xElora = 578;
        }

        if (worlds[type] == null)
        {
            switch(type)
            {
                case 2: addObject(apple, 200, 336); break;
                case 11: addObject(diamondkey, 100, 100); break;
            }
        }
        else
        {
            for(Object obj: worlds[type].getObjects(null))
            {
                Actor actor = (Actor) obj;
                addObject(actor, actor.getX(), actor.getY());
            }
        }
        
        switch(type)
        {
            case 2: addObject(new Gate(), 553, 306); break;
            case 5: addObject(new CafeteriaDoor(), 429, 146); break;
            case 8: addObject(new CafeteriaStairs(), 536, 323); addObject(new CafeteriaDoor2(), 234, 176); break;
            case 9: addObject(new ClassroomDoor(), 368, 202); break;
            case 11: addObject(new StudentDoor(diamondkey), 505, 195); addObject(new SipmaMini(), 381, 227); break;
            case 13: addObject(new ClassroomStairs(), 505, 307); break;
            case 14: addObject(new LibraryDoor(), 303, 198); break;
            case 17: addObject(new FacultyDoor(), 76, 177); addObject(new PrincipalDoor(), 498, 175); break;
            case 20: addObject(new GirlDoor(), 265, 187); addObject(new BoyDoor(), 66, 189); break;
            case 23: addObject(new JanitorDoor(), 102, 208); addObject(new BasementDoor(), 478, 210); break;
        }

        addObject(player, x, y);
        addObject(player.getEloraWalk(), xElora, yElora);
        addObject(inventory, 45, 361);
        setPaintOrder(Texts.class, TextBox.class, Characters.class, Props.class, Overlaps.class, UserWalk1.class, EloraWalk1.class, SipmaMini.class, Doors.class);
    }
I separated the switch for the clues and the doors, etc. The doors stay now, but it still cannot be entered twice... The clues don't add if the background is null, but it shows up in the inventory if I click it. However, if the player missed the clue in one background and then had to go back rooms to find it, it won't be there anymore despite that they did not click it yet. Also, how do I initialize that the worlds become null when I reset the scenario?
danpost danpost

2017/3/5

#
Drop the changes you just made and go back to the complete class code above. Then, refer to that code for the following:
SallySanban wrote...
how do I initialize that the worlds become null when I reset the scenario?
Change line 45 (in the complete class code above) to:
private static World[] worlds;
and add the following line after line 50 (in the complete class code above):
worlds = new World[bgImages.length];
Change 'Clues.class' in lines 56 and line 100 to 'null' (I missed one of those last time). Remove the 'if' condition on line 90 (just add the object on that line). Then report back as to how it works (without making any other changes, as yet).
SallySanban SallySanban

2017/3/5

#
It makes a NullPointerException and I cannot run it.
danpost danpost

2017/3/5

#
SallySanban wrote...
It makes a NullPointerException and I cannot run it.
What does the output read? Copy/paste the entire error trace here to determine what needs fixed.
SallySanban SallySanban

2017/3/5

#
java.lang.NullPointerException
	at Alleyway4.<init>(Alleyway4.java:85)
	at Alleyway4.<init>(Alleyway4.java:51)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at greenfoot.core.Simulation.newInstance(Simulation.java:617)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:425)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:502)
	at greenfoot.core.Simulation.maybePause(Simulation.java:305)
	at greenfoot.core.Simulation.runContent(Simulation.java:218)
	at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2017/3/5

#
Okay, remove that line I had you put in after line 50 and add the following line at line 81 (using the line numbers of the full class code above):
if (lastWorldType == -1) worlds = new World[bgImages.length];
You need to login to post a reply.
1
2
3
4