When I move from one world onto the next my score resets back to zero. I have a superclass Scenes world and then subclass Scene2 and Scene3 worlds.
Here is my Scenes superclass world code
This is the Scene2 subclass World code
I have created a Scoreboard actor class to update the score
When my Villian actor intersects the Heart actor the score updates but when I move to the next world by using the Continue actor to move to the next world my score resets back to zero. I want to keep the score moving from one level to the next.
This is my Villian actor code
This is my Continue actor code
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Scenes extends World
{
public Scoreboard sb;
public Scenes()
{
super(1300, 600, 1);
addObject (new Platform(), 885, 440);
addObject (new Villian(), 37, 505);
addObject (new Continue(), 1263, 509);
addObject (new Heart(), 850, 374);
sb = new Scoreboard();
addObject (sb, 45, 35);
}
public void updateScore()
{
sb.changeScore(); // change the score
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Scene2 extends Scenes
{
public Scene2()
{
addObject (new Platform(), 200, 100);
addObject (new Platform(), 600, 200);
addObject (new Platform(), 800, 130);
addObject (new Villian(), 30, 500);
addObject (new Heart(), 1100, 80);
addObject (new Continue2(), 1260, 500);
sb = new Scoreboard(); addObject (sb, 45, 35);
}
public void updateScore()
{
sb.changeScore(); // change the score
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Scoreboard extends Actor
{
public int score = 0;
public Scoreboard() // constructor for scoreboard
{
GreenfootImage img = new GreenfootImage(100, 30);
img.setColor(new Color (0,250,0));
img.drawString("Score: " + score, 5, 25);
setImage(img);
}
public void changeScore() // when called it needs to change the score
{
score = score + 1;
GreenfootImage img = getImage();
img.clear();
img.drawString("Score: " + score, 5, 25);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Villian extends Actor
{
private int speed = 7;
public void act()
{
checkKeys();
Capture();
}
private void checkKeys()
{
setImage("Standing.png"); // sets image
if (Greenfoot.isKeyDown("a")) // if your pressing a
{
setImage("Runningleft.png"); // Sets image
moveLeft(); //initiates moveLeft code
}
if (Greenfoot.isKeyDown("d"))
{
setImage("Running.png"); // Sets image
moveRight(); //initiates moveRight code
}
}
public void moveRight()
{
setLocation (getX() + speed, getY());
}
public void moveLeft()
{
setLocation (getX() - speed, getY());
}
public void Capture()
{
Heart heart = (Heart)getOneIntersectingObject(Heart.class);
if (heart != null)
{
getWorld().removeObject(heart);
Scenes world = (Scenes)getWorld();
world.updateScore();
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Continue extends Actor
{
public void act()
{
Villian villian = (Villian)getOneIntersectingObject(Villian.class);
if (villian != null)
{
Greenfoot.setWorld(new Scene2());
}
}
}
