I am attempting to have my player move from world to world since it contains all of the data on its score and it's lives, when the new world loads the player does not appear in the new world and the score and lives are reset, can anyone shed any light on this issue?
Player Code:
Level_1 Code:
Level_2 Code:
Score Code:
Lives Code:
I know the code is a bit messy for the levels, but if anyone can spot how to resolve this issue it would be a massive help thanks!
import greenfoot.*;
import java.awt.Color;
public class Player extends Actor
{
private int frame = 0;
private int direction = 1;
private int jumpStrength = 12;
private boolean jump = false;
private double gravityStrength = 1;
private double vSpeed = 0;
private int animationCounter = 0;
private int attackCounter = 0;
private int score = 0;
private int lives = 1;
private boolean hasKey = false;
private Color transparent = new Color(0, 0, 0, 0);
/**
* These are all of the variables which are defined on the inital load up of the game
*/
public Player()
{
setImage("Hero_1.png");
}
public void act()
{
attackCounter --;
animationCounter++;
Movement();
floorCollision();
checkIfFalling();
collectLoot();
killed();
if(getWorld() != null){surfaceAbove();}
if(getWorld() != null){getKey();}
if(getWorld() != null){openDoor();}
if(getWorld() != null){openDoor2();}
if(getWorld() != null){openDoor3();}
}
/**
* Detects interactions
*/
public boolean detect(Class cls)
{
Actor detect = getOneObjectAtOffset(0, 0, cls);
return detect != null;
}
/**
* Removes interacted object
*/
public void remove(Class cls)
{
Actor remove = getOneObjectAtOffset(0, 0, cls);
if(remove != null)
{
getWorld().removeObject(remove);
}
}
public void collectLoot()
{
if(detect(Coin.class))
{
score = score + 10;
addScore();
remove(Coin.class);
}
}
public void getKey()
{
if(detect(Key.class))
{
remove(Key.class);
hasKey = true;
}
}
/**
* This is the code that I have been testing the system with
*/
public void openDoor()
{
Player player = new Player();
World world = new Level_2();
if(detect(Door.class) && hasKey == true)
{
score = score + 25;
addScore();
world.addObject(this, 30, 335);
Greenfoot.setWorld(new Level_2());
hasKey = false;
}
}
public void openDoor2()
{
if(detect(Door2.class) && hasKey == true)
{
score = score + 25;
addScore();
Greenfoot.setWorld(new Level_3());
hasKey = false;
}
}
public void openDoor3()
{
if(detect(Door3.class) && hasKey == true)
{
score = score + 25;
addScore();
Greenfoot.setWorld(new Level_Home());
}
}
/**
* This checks whether or not the player is in contact with the floor
*/
public boolean floorCollision()
{
int playerHeight = getImage().getHeight();
int fallDistance = (int) (playerHeight/2) + 10;
Actor surface = getOneObjectAtOffset(0, getImage().getHeight()/2, Surface.class);
if(surface == null)
{
jump = true;
return false;
}
else
{
Gravity(surface);
return true;
}
}
/**
* This causes the player to fall if they're jumping or in the air
*/
public void Fall()
{
setLocation(getX(), getY() + (int) vSpeed);
if (vSpeed <= 9)
{
vSpeed = vSpeed + gravityStrength;
}
jump = true;
}
/**
* This makes the player jump
*/
public void Jump()
{
vSpeed = vSpeed - jumpStrength;
jump = true;
Fall();
}
public void Gravity(Actor surface)
{
int surfaceHeight = surface.getImage().getHeight();
int newY = surface.getY() - (surfaceHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
jump = false;
}
public void Movement()
{
if(Greenfoot.isKeyDown("right"))
{
move(3);
direction = 1;
if(animationCounter % 4 == 0)
{
Animation();
}
}
if(Greenfoot.isKeyDown("left"))
{
move(-3);
direction = -1;
if(animationCounter % 4 == 0)
{
Animation();
Turn();
}
}
if(Greenfoot.isKeyDown("up") && jump == false && attackCounter <= 0)
{
Jump();
}
if(Greenfoot.isKeyDown("space") && jump == false && attackCounter <= 0)
{
attackCounter = 40;
if(direction == 1)
{
Attack_Right();
}
else if(direction == -1)
{
Attack_Left();
}
}
}
private void checkIfFalling()
{
if(floorCollision())
{
vSpeed = 0;
}
else
{
Fall();
}
}
/**
* This causes the sprite to flip horizontally so they walk towards the correct direction
*/
public void Turn()
{
getImage().mirrorHorizontally();
}
/**
* This is what causes the animation to play when the playes is walking
*/
public void Animation()
{
frame++;
if(frame == 0){setImage("Hero_1.png");}
else if(frame == 1){setImage("Hero_2.png");}
else if(frame == 2){setImage("Hero_3.png");}
else if(frame == 3){setImage("Hero_4.png");}
else if(frame == 4){setImage("Hero_5.png");}
else if(frame == 5){setImage("Hero_6.png");}
else if(frame == 6){setImage("Hero_7.png");}
else if(frame == 7){setImage("Hero_8.png");
frame = 0;
}
}
public void Attack_Right()
{
Attack newAttackRight;
getWorld().addObject(new Attack_Right(), getX(), getY());
attackCounter = 20;
}
public void Attack_Left()
{
Attack newAttackLeft;
getWorld().addObject(new Attack_Left(), getX(), getY());
attackCounter = 20;
}
/**
* This causes the mobs to kill the player upon collision and forces the player to lose a life
*/
public void killed()
{
Player player = new Player();
World world = getWorld();
if(detect(Mob.class))
{
loseLife();
world.removeObject(this);
world.addObject(player, 30, 335);
}
}
public void bopHead(Actor ceiling)
{
int ceilingHeight = ceiling.getImage().getHeight();
int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
}
public boolean surfaceAbove()
{
int spriteHeight = getImage().getHeight();
int yDistance = (int)(spriteHeight/-2);
Actor ceiling = getOneObjectAtOffset(0, yDistance, Surface.class);
if(ceiling != null)
{
vSpeed = 1;
bopHead(ceiling);
return true;
}
else
{
return false;
}
}
/**
* Increases score to the score counter
*/
public void loseLife()
{
Level lvl = (Level) getWorld();
Lives counter = lvl.getLives();
counter.loseLives(lives);
}
/**
* Increases score to the score counter
*/
public void addScore()
{
Level lvl = (Level) getWorld();
Score counter = lvl.getScore();
counter.addScore(score);
}
public int getScore()
{
return score;
}
public int getLives()
{
return lives;
}
}import greenfoot.*;
public class Level_1 extends Level
{
public Level_1()
{
prepare();
}
public void prepare()
{
Player player = new Player();
addObject(player, 54, 356);
flr2 floor1 = new flr2();
addObject(floor1, 16, 385);
flr2 floor2 = new flr2();
addObject(floor2, 48, 385);
flr2 floor3 = new flr2();
addObject(floor3, 80, 385);
flr2 floor4 = new flr2();
addObject(floor4, 112, 385);
flr2 floor5 = new flr2();
addObject(floor5, 144, 385);
flr2 floor6 = new flr2();
addObject(floor6, 176, 385);
flr2 floor7 = new flr2();
addObject(floor7, 208, 385);
flr2 floor8 = new flr2();
addObject(floor8, 240, 385);
flr2 floor9 = new flr2();
addObject(floor9, 272, 385);
flr2 floor10 = new flr2();
addObject(floor10, 304, 385);
flr2 floor11 = new flr2();
addObject(floor11, 336, 385);
flr2 floor12 = new flr2();
addObject(floor12, 368, 385);
flr2 floor13 = new flr2();
addObject(floor13, 400, 385);
flr2 floor14 = new flr2();
addObject(floor14, 432, 385);
flr2 floor15 = new flr2();
addObject(floor15, 464, 385);
flr2 floor16 = new flr2();
addObject(floor16, 496, 385);
flr2 floor17 = new flr2();
addObject(floor17, 528, 385);
flr2 floor18 = new flr2();
addObject(floor18, 560, 385);
flr2 floor19 = new flr2();
addObject(floor19, 592, 385);
Coin coin = new Coin();
addObject(coin, 220, 347);
Slime slime = new Slime();
addObject(slime, 429, 336);
Slime slime2 = new Slime();
addObject(slime2, 523, 339);
removeObject(slime);
removeObject(slime2);
removeObject(coin);
flr2 flr220 = new flr2();
addObject(flr220, 560, 307);
flr2 flr221 = new flr2();
addObject(flr221, 528, 307);
flr2 flr222 = new flr2();
addObject(flr222, 496, 308);
flr221.setLocation(528, 307);
flr222.setLocation(496, 307);
Coin coin2 = new Coin();
addObject(coin2, 563, 274);
Ghost ghost = new Ghost();
addObject(ghost, 498, 279);
flr2 flr223 = new flr2();
addObject(flr223, 446, 248);
flr2 flr224 = new flr2();
addObject(flr224, 414, 248);
flr2 flr225 = new flr2();
addObject(flr225, 510, 181);
flr225.setLocation(513, 171);
Key key = new Key();
addObject(key, 514, 133);
flr2 flr226 = new flr2();
addObject(flr226, 31, 306);
flr2 flr227 = new flr2();
addObject(flr227, 62, 307);
flr2 flr228 = new flr2();
addObject(flr228, 97, 307);
flr227.setLocation(63, 306);
flr228.setLocation(95, 306);
flr2 flr229 = new flr2();
addObject(flr229, 130, 220);
flr229.setLocation(152, 227);
flr2 flr230 = new flr2();
addObject(flr230, 87, 172);
flr2 flr231 = new flr2();
addObject(flr231, 57, 173);
flr2 flr232 = new flr2();
addObject(flr232, 250, 132);
flr2 flr233 = new flr2();
addObject(flr233, 282, 133);
flr2 flr234 = new flr2();
addObject(flr234, 328, 132);
flr233.setLocation(282, 132);
flr234.setLocation(314, 132);
flr229.setLocation(181, 234);
Loot loot = new Loot();
addObject(loot, 54, 157);
flr231.setLocation(55, 188);
flr230.setLocation(87, 189);
flr230.setLocation(87, 188);
Slime slime3 = new Slime();
addObject(slime3, 87, 162);
Troll troll = new Troll();
addObject(troll, 33, 280);
troll.setLocation(32, 275);
troll.setLocation(31, 278);
Troll troll2 = new Troll();
addObject(troll2, 212, 356);
Ghost ghost2 = new Ghost();
addObject(ghost2, 356, 356);
ghost2.setLocation(354, 358);
Door door = new Door();
addObject(door, 282, 89);
removeObject(loot);
Coin coin3 = new Coin();
addObject(coin3, 56, 157);
}
}
import greenfoot.*;
public class Level_2 extends Level
{
public Level_2()
{
flr2 floor1 = new flr2();
addObject(floor1, 16, 385);
flr2 floor2 = new flr2();
addObject(floor2, 48, 385);
flr2 floor3 = new flr2();
addObject(floor3, 80, 385);
flr2 floor4 = new flr2();
addObject(floor4, 112, 385);
flr2 floor5 = new flr2();
addObject(floor5, 144, 385);
flr2 floor6 = new flr2();
addObject(floor6, 176, 385);
flr2 floor7 = new flr2();
addObject(floor7, 208, 385);
flr2 floor8 = new flr2();
addObject(floor8, 240, 385);
flr2 floor9 = new flr2();
addObject(floor9, 272, 385);
flr2 floor10 = new flr2();
addObject(floor10, 304, 385);
flr2 floor11 = new flr2();
addObject(floor11, 336, 385);
flr2 floor12 = new flr2();
addObject(floor12, 368, 385);
flr2 floor13 = new flr2();
addObject(floor13, 400, 385);
flr2 floor14 = new flr2();
addObject(floor14, 432, 385);
flr2 floor15 = new flr2();
addObject(floor15, 464, 385);
flr2 floor16 = new flr2();
addObject(floor16, 496, 385);
flr2 floor17 = new flr2();
addObject(floor17, 528, 385);
flr2 floor18 = new flr2();
addObject(floor18, 560, 385);
flr2 floor19 = new flr2();
addObject(floor19, 592, 385);
prepare();
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
flr2 flr235 = new flr2();
addObject(flr235, 100, 303);
flr2 flr236 = new flr2();
addObject(flr236, 133, 303);
flr2 flr237 = new flr2();
addObject(flr237, 167, 303);
flr236.setLocation(132, 303);
flr237.setLocation(164, 303);
flr2 flr238 = new flr2();
addObject(flr238, 20, 250);
flr2 flr239 = new flr2();
addObject(flr239, 58, 251);
flr238.setLocation(10, 250);
flr239.setLocation(42, 250);
flr2 flr240 = new flr2();
addObject(flr240, 44, 164);
flr240.setLocation(39, 165);
flr240.setLocation(41, 166);
flr2 flr241 = new flr2();
addObject(flr241, 122, 106);
flr2 flr242 = new flr2();
addObject(flr242, 155, 107);
flr242.setLocation(153, 107);
flr242.setLocation(154, 106);
flr2 flr29 = new flr2();
addObject(flr29, 458, 294);
flr2 flr210 = new flr2();
addObject(flr210, 496, 295);
flr210.setLocation(490, 294);
flr2 flr211 = new flr2();
addObject(flr211, 526, 240);
flr2 flr212 = new flr2();
addObject(flr212, 562, 240);
flr211.setLocation(530, 237);
flr211.setLocation(530, 240);
flr2 flr213 = new flr2();
addObject(flr213, 411, 150);
flr2 flr214 = new flr2();
addObject(flr214, 444, 151);
flr214.setLocation(441, 150);
flr214.setLocation(443, 150);
Coin coin = new Coin();
addObject(coin, 462, 263);
Coin coin2 = new Coin();
addObject(coin2, 487, 263);
Coin coin3 = new Coin();
addObject(coin3, 568, 204);
Coin coin4 = new Coin();
addObject(coin4, 150, 72);
coin4.setLocation(155, 72);
coin4.setLocation(152, 73);
coin2.setLocation(489, 258);
coin.setLocation(456, 260);
coin.setLocation(456, 259);
coin3.setLocation(561, 203);
Ghost ghost = new Ghost();
addObject(ghost, 530, 209);
Slime slime = new Slime();
addObject(slime, 459, 264);
Troll troll = new Troll();
addObject(troll, 8, 224);
troll.setLocation(121, 67);
troll.setLocation(121, 73);
Key key = new Key();
addObject(key, 42, 134);
Ghost ghost2 = new Ghost();
addObject(ghost2, 132, 276);
Slime slime2 = new Slime();
addObject(slime2, 162, 274);
Troll troll2 = new Troll();
addObject(troll2, 102, 273);
flr213.setLocation(410, 162);
flr213.setLocation(411, 164);
flr214.setLocation(443, 164);
Door2 door2 = new Door2();
addObject(door2, 331, 91);
door2.setLocation(418, 119);
}
}
import greenfoot.*;
import java.awt.Color;
public class Score extends Actor
{
private int point = 0;
public Color transparent = new Color(0, 0, 0, 0);
public Score()
{
setImage(new GreenfootImage("Score: 0", 20, Color.WHITE, transparent));
}
/**
* Increase the total amount displayed on the counter, by a given amount.
*/
public void addScore(int score)
{
point += score;
setImage(new GreenfootImage("Score: " + point, 20, Color.WHITE, transparent));
}
}
import greenfoot.*;
import java.awt.Color;
public class Lives extends Actor
{
private int lives = 5;
public Color transparent = new Color(0, 0, 0, 0);
public Lives()
{
setImage(new GreenfootImage("Lives: 5", 20, Color.WHITE, transparent));
}
/**
* Increase the total amount displayed on the counter, by a given amount.
*/
public void loseLives(int life)
{
lives -= life;
setImage(new GreenfootImage("Lives: " + lives, 20, Color.WHITE, transparent));
if(lives == 0)
{
Greenfoot.stop();
}
}
}

