SallySanban wrote...
That worked! I don't really understand the code, though. Could you explain it, please?
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);
}
}
}
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);
}private static World[] worlds;
worlds = new World[bgImages.length];
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)