However, if you add a call to changeBackground after line 357, it may do what it should.
private int type;
private Actor player;
private int changeLeft=91, changeRight=519;
private static final GreenfootImage[] bgImages =
{
new GreenfootImage("Alleyway 4.PNG"),
new GreenfootImage("Kalachuchi Walk 1.JPG"),
new GreenfootImage("School Entrance.JPG")
//....
};
public MyWorld()
{
this(new UserWalk1(), 0, -1);
}
public MyWorld(Actor p, int worldType, int lastWorldType)
{
super(..........);
player = p;
type = worldType;
setBackground(bgImages[type]);
int x, y=243; //where to add the player
if (lastWorldType < worldType) //moved right
{
x=125;
}
else //moved left
{
x=500;
}
switch (type)
{
case 2: changeRight=464; break;
case 5: addObject(new CafeteriaDoor(), 429, 126); break;
}
addObject(player, x, y);
}
public void changeBackground(int change) //1 for right, -1 or left
{
Greenfoot.setWorld(new MyWorld(player, type+change, type));
}
public void act()
{
int x = player.getX();
if (x<changeLeft && type!=0) changeBackground(-1);
else if (x>changeRight && type!=25) changeBackground(1);
}public void act()
{
if (Greenfoot.mouseClicked(this)) //I usually use mousePressed but that is obviously your choice
{
((MyWorld) getWorld()).changeBackground(1);
}
}private static final GreenfootImage[] costumeImages =
{
new GreenfootImage("User Walk 1.gif"),
new GreenfootImage("User Walk 2.gif"),
new GreenfootImage("User Walk 3.gif"),
new GreenfootImage("User Walk 4.gif")
};
//when moving right
setImage(costumeImages[Greenfoot.getRandomNumber(2)]);
//when moving left
setImage(costumeImages[Greenfoot.getRandomNumber(2)+2]);int bgNumber = background % totalBG;
else if(bgNumber == 6)
{
getWorld().setBackground("Cafeteria 1.JPG");
}this(new UserWalk1(), 0, -1);
public Alleyway4() //the World in my original code
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
prepare();
}
public void prepare()
{
EloraWalk1 elorawalk1 = new EloraWalk1();
addObject(elorawalk1, 95, 270);
UserWalk1 userwalk1 = new UserWalk1(elorawalk1); //I passed EloraWalk1 to UserWalk1
addObject(userwalk1, 125, 249);
userwalk1.setLocation(245, 243);
Inventory inventory = new Inventory();
addObject(inventory, 45, 361);
Text2 text2 = new Text2();
addObject(text2, 304, 283);
Color transparent = new Color(0, 0, 0, 0);
text2.setImage(new GreenfootImage(" ", 22, Color.BLACK, transparent));
text2.setLocation(304, 283);
setPaintOrder(Texts.class, TextBox.class, Props.class, Characters.class, Walk.class, Doors.class);
}
public void followUser()
{
if(Greenfoot.isKeyDown("right"))
{
if(elorawalk1.getX() < this.getX())
{
elorawalk1.move(1);
costumeElora = Greenfoot.getRandomNumber(2) + 1;
if(costumeElora == 1)
{
switchCostumeElora(2);
}
else if(costumeElora == 2)
{
switchCostumeElora(1);
}
}
}
if(Greenfoot.isKeyDown("left"))
{
if(elorawalk1.getX() > this.getX())
{
elorawalk1.move(-1);
costumeElora = Greenfoot.getRandomNumber(2) + 3;
if(costumeElora == 3)
{
switchCostumeElora(4);
}
else if(costumeElora == 4)
{
switchCostumeElora(3);
}
}
}
}
/**
* Changes the costume of Elora to make her look like she's walking.
*/
public void switchCostumeElora(int costumeNumber)
{
GreenfootImage costume1 = new GreenfootImage("Elora Walk 1.gif");
GreenfootImage costume2 = new GreenfootImage("Elora Walk 2.gif");
GreenfootImage costume3 = new GreenfootImage("Elora Walk 3.gif");
GreenfootImage costume4 = new GreenfootImage("Elora Walk 4.gif");
if(costumeNumber == 1)
{
elorawalk1.setImage(costume2);
}
else if(costumeNumber == 2)
{
elorawalk1.setImage(costume1);
}
else if(costumeNumber == 3)
{
elorawalk1.setImage(costume4);
}
else if(costumeNumber == 4)
{
elorawalk1.setImage(costume3);
}
}this(new UserWalk1(), 0, -1);
addObject(player, x, y);
this(new UserWalk1(new EloraWalk1()), 0, -1);
private EloraWalk1 elorawalk;
public UserWalk1(EloraWalk1 e)
{
elorawalk = e;
//...
}
public EloraWalk1 getEloraWalk() {return elorawalk;}