Please show the class code where you declare and create the Inventory object (in the class where 'new Inventory()' is located).
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
{
private int type;
private static final UserWalk1 player = new UserWalk1(new EloraWalk1());
private static final Inventory inventory = new Inventory();
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
};
public Alleyway4()
{
this(0, -1);
}
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;
}
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(), 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; //change x and y coordinates once you get the actual door
case 23: addObject(new JanitorDoor(new StarKey()), 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);
}
public void changeBackground(int change) //1 for right, -1 or left
{
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);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.awt.Color;
import java.util.ArrayList;
/**
* Write a description of class Inventory here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Inventory extends Props
{
public static List<Actor> collectedObjects = new ArrayList(0); //the hints which were collected and should show up in the inventory
public boolean inventoryShowing = false; //true if the inventory is showing right now
public List<Actor> inventoryObjects = new ArrayList(0); //all actors which are added when x is pressed so they can be removed when x is pressed again
//CaramelApple apple = new CaramelApple();
/**
* Act - do whatever the Inventory wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
clue();
if(Greenfoot.isKeyDown("x"))
{
showInventory();
}
if(Greenfoot.mouseClicked(this))
{//remove inventory
getWorld().removeObjects(inventoryObjects); //remove all objects relating to the inventory
inventoryShowing = false;
inventoryObjects = new ArrayList(0);
}
}
public void clue()
{
//collectedObjects.add(apple);
for(Clues h: getWorld().getObjects(Clues.class))
{
if (Greenfoot.mouseClicked(h))
{
collectedObjects.add(h); //add the hint to the list of collected hints
getWorld().removeObject(h); //remove the clicked hint from the world
}
}
}
public void showInventory()
{
if(!inventoryShowing)
{
int i = 0, numItems = collectedObjects.size();
for(i = 0; i < numItems; i++)
{
Actor item = collectedObjects.get(i);
item.setImage(new GreenfootImage(item.getImage())); //.scale(10, 10));
int xItem = (i + 2) * 60;
getWorld().addObject(item, xItem, getY());
inventoryObjects.add(item);
}
i++;
}
inventoryShowing = true;
}
}
private static Inventory inventory;
if (worldType == 0 && lastWorldType == -1) inventory = new Inventory();
inventory.collectedObjects.clear();
public int isFirstTime = 1;
public boolean isCollected = false;
/**
* Act - do whatever the StarKey wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(Greenfoot.mouseClicked(this) && isFirstTime == 1)
{
isCollected = true;
isFirstTime = 0;
}
} private StarKey star;
public JanitorDoor(StarKey s)
{
star = s;
}
/**
* Act - do whatever the JanitorDoor wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(Greenfoot.mouseClicked(this) && star.isCollected)
{
((Alleyway4) getWorld()).changeBackground(1); //changeBackground sets the background to the one inside the door (as if the player enters the room)
}
} public class StarKey extends greenfoot.Actor {}if (Greenfoot.mouseClicked(this) && Alleyway4.getInventory().collectedObjects.contains(star))
public static Inventory getInventory()
{
return inventory;
}public boolean atRightEdge()
{
if(player.getX() == 510))
{
switch (type)
{
case 5: case 10: case 12: case 14: case 18: case 19: case 21: case 22: case 24: case 25:
return true;
}
}
return false;
}if (type==5 || type==10 || type==12 || type==14 || type==18 || type==19 || type==21 || type==22 || type==24 || type==25)
{
return true;
}public boolean atRightEdge()
{
return player.getX()==510 && (type==5 || type==10 || type==12 || type==14 || type==18 || type==19 || type==21 || type==22 || type==24 || type==25);
}public class StarKey extends greenfoot.Actor {}if (Greenfoot.mouseClicked(this) && Alleyway4.getInventory().collectedObjects.contains(star))
public static Inventory getInventory()
{
return inventory;
}public boolean atRightEdge()
{
if(player.getX() == 510))
{
switch (type)
{
case 5: case 10: case 12: case 14: case 18: case 19: case 21: case 22: case 24: case 25:
return true;
}
}
return false;
}if (type==5 || type==10 || type==12 || type==14 || type==18 || type==19 || type==21 || type==22 || type==24 || type==25)
{
return true;
}public boolean atRightEdge()
{
return player.getX()==510 && (type==5 || type==10 || type==12 || type==14 || type==18 || type==19 || type==21 || type==22 || type==24 || type==25);
}if (Greenfoot.mouseClicked(this))
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;
}
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(), 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; //change x and y coordinates once you get the actual door
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);
}public void act()
{
if(Greenfoot.mouseClicked(this) && ((Alleyway4) getWorld()).getInventory().collectedObjects.contains(DiamondKey.class))
{
((Alleyway4) getWorld()).changeBackground(1);
}
} public void act()
{
if(Greenfoot.mouseClicked(this))
{
((Alleyway4) getWorld()).getInventory().collectedObjects.add(this);
((Alleyway4) getWorld()).removeObject(this);
}
}Alleyway4 aw4 = new Alleyway4(type, lastType); aw4.removeObjects(inventory.collectedObjects);