This site requires JavaScript, please enable it in your browser!
Greenfoot back
wslade
wslade wrote ...

2021/1/11

Passing the score from one world onto the next world

wslade wslade

2021/1/11

#
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
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

    }
}
This is the Scene2 subclass World code
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
        
    }
}
I have created a Scoreboard actor class to update 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); 
      
    }   
}
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
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();
        }

    }

}
This is my Continue actor code
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());
        }
    }   
}

danpost danpost

2021/1/11

#
Replace line 9 in Continue code with the following:
Scenes nowScene = (Scenes) getWorld();
Scenes nextScene = null;
if (nowScene instanceof Scene1) nextScene = new Scene2();
else ; // to be continued with more scenes
nextScene.sb.score = nowScene.sb.score;
Greenfoot.setWorld(nextScene);
wslade wslade

2021/1/11

#
When I try this I get an error "java.lang.NullPointerException at Continue.act(Continue.java:13)". This is the code I currently have
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)
        {   
            Scenes nowScene = (Scenes) getWorld();
            Scenes nextScene = null;
            if (nowScene instanceof Scene1) nextScene = new Scene2();
            else ; // to be continued with more scenes
            nextScene.sb.score = nowScene.sb.score;
            Greenfoot.setWorld(nextScene);
        }
    }   
}
danpost danpost

2021/1/12

#
wslade wrote...
When I try this I get an error "java.lang.NullPointerException at Continue.act(Continue.java:13)".
Sorry, I misunderstood your arrangement of World subclasses. Try this: Scenes class:
import greenfoot.*;

public class Scenes extends World
{
    public Scoreboard sb = new Scoreboard();

    public Scenes()
    {    
        super(1300, 600, 1);
    }
 
    public void updateScore() 
    {
        sb.changeScore();
    }
}
Scene1 class:
import greenfoot.*;

public class Scene1 extends Scenes
{
    public Scene1()
    {    
        super();  
        addObject (new Platform(), 885, 440); 
        addObject (new Villian(), 37, 505); 
        addObject (new Continue(), 1263, 509); 
        addObject (new Heart(), 850, 374); 
        addObject (sb, 45, 35); 
    }
}
Scene2 class:
import greenfoot.*;

public class Scene2 extends Scenes
{
 
    public Scene2()
    {
        super();
        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 Continue(), 1260, 500); 
        addObject (sb, 45, 35); 
    }
}
Manually create a Scene1 world after making changes. Also, delete your Continue2 class (not needed).
wslade wslade

2021/1/12

#
Got it. That works much better now. There is one small glitch though. When I move to Scene2 the score initially shows as 0 even though the score was more in Scene1. As soon as the villian captures a heart in scene2 it correctly shows the appropriate number of hearts in the score though. Any ideas on how I could fix that?
danpost danpost

2021/1/12

#
wslade wrote...
There is one small glitch though. When I move to Scene2 the score initially shows as 0 even though the score was more in Scene1. As soon as the villian captures a heart in scene2 it correctly shows the appropriate number of hearts in the score though. Any ideas on how I could fix that?
Change Continue code to:
Scenes nowScene = (Scenes) getWorld();
Scenes nextScene = null;
if (nowScene instanceof Scene1) nextScene = new Scene2();
else ; // to be continued with more scenes
nextScene.sb.score = nowScene.sb.score-1;
nextScene.sb.changeScore();
Greenfoot.setWorld(nextScene);
wslade wslade

2021/1/12

#
That worked! Thanks so much for your help!
You need to login to post a reply.