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

2019/1/11

Score not showing up on separate worlds

Nate2002 Nate2002

2019/1/11

#
I'm trying to get a score to save on 2 separate worlds and this saved score from the previous world would add onto any points you get on the second world. Here's what I tried (World1):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    public int time = 0;
    public int Score;
    static final GreenfootSound music = new GreenfootSound("Mushroom Bridge.mp3"); 
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1); 
        GreenfootImage background = getBackground();
        background.setColor(java.awt.Color.cyan);
        background.fill();
        prepare();
        Score++;
    }

    public void act()
    {
        countTime();
        if(Greenfoot.getRandomNumber(600) < 5)
        {
            addObject(new Cloud(), (Greenfoot.getRandomNumber(400) + 100), (0)); 
        }
        if(Greenfoot.getRandomNumber(600) < 5)
        {
            addObject(new Cloud2(), (Greenfoot.getRandomNumber(300) + 700), (0)); 
        }
        if(Greenfoot.getRandomNumber(7000) < 5)
        {
            addObject(new ETank(), (Greenfoot.getRandomNumber(200) + 400), (0)); 
        }
        if(Greenfoot.getRandomNumber(700) < 5)
        {
            addObject(new Plane(), (5), (Greenfoot.getRandomNumber(500) + 450)); 
        }
        if(Greenfoot.getRandomNumber(800) < 5)
        {
            addObject(new Helicopter(), (Greenfoot.getRandomNumber(900) + 100), (0)); 
        }
        if(Greenfoot.getRandomNumber(800) < 5)
        {
            addObject(new Vmissile(), (Greenfoot.getRandomNumber(900) + 100), (1000)); 
        }
        if(Greenfoot.getRandomNumber(800) < 5)
        {
            addObject(new Blimp(), (1000), (Greenfoot.getRandomNumber(900) + 100)); 
        }
        if(Greenfoot.getRandomNumber(20000) < 5)
        {
            addObject(new Helipad(), (500), (0)); 
        }
        music.playLoop();
    }
    public void addScore(int points)
    {
        Score = Score + points;
        
        showScore();
        

    }
    public void showScore()
    {
        showText("Pts:  " + Score, 925, 50);
        

    }
}
and here's what i did for World2:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class World2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class World2 extends World
{
    public int time = 0;
    public int score= Score;
    static final GreenfootSound music = new GreenfootSound("Mushroom Bridge.mp3"); 
    /**
     * Constructor for objects of class World2.
     * 
     */
    public World2()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1); 
        GreenfootImage background = getBackground();
        background.setColor(java.awt.Color.cyan);
        background.fill();
        setPaintOrder(Flash.class, Fash.class, Car.class, Explosion2.class, SafeHouse.class, VCar.class, Luigi.class, VMissile2.class, Bullet.class, Bullet2.class, CannonBall.class, Building.class, Smoke.class, ETank2.class, Grass.class);
        prepare();
        time++;
    }
    
    public void act()
    {
        countTime();
        if(time%20==1)
       {
         addObject (new Grass(),1000, 490);  
        }
        if(Greenfoot.getRandomNumber(1000) < 5)
        {
            addObject(new VCar(), (1000), (400)); 
        }
        if(Greenfoot.getRandomNumber(1000) < 5)
        {
            addObject(new VPlane2(), (1000), (100)); 
        }
        if(Greenfoot.getRandomNumber(1000) < 5)
        {
            addObject(new Luigi(), (Greenfoot.getRandomNumber(500) + 400), (400)); 
        }
        if(Greenfoot.getRandomNumber(1000) < 5)
        {
            addObject(new VMissile2(), (1000), (0)); 
        }
        if(Greenfoot.getRandomNumber(30000) < 5)
        {
            addObject(new SafeHouse(), (1000), (300)); 
        }
        if(Greenfoot.getRandomNumber(10001) < 5)
        {
            addObject(new ETank2(), (1000), (400)); 
        }
       
        music.playLoop();
    }
    public void addScore(int points)
    {
        score = score + points + Score;
        
        showScore();
        

    }
    public void showScore()
    {
        showText("Pts:  " + score, 925, 50);
        

    }
    public void countTime()
    {
        time++;
        // showTime();
    }
}
If anyone knows what i should fix please let me know and thank you in advance.
danpost danpost

2019/1/11

#
You could add a "public int previousScore" to all your world classes and add the current world score to it and set the new world field to the new value (when changing worlds).
Nate2002 Nate2002

2019/1/13

#
Can you please show me the code for it?
danpost danpost

2019/1/13

#
Nate2002 wrote...
Can you please show me the code for it?
// in one world
public int prevScore;
private int score;

// changing to next world
WorldTwo w2 = new WorldTwo();
w2.prevScore = score+prevScore;
Greenfoot.setWorld(w2);
Nate2002 Nate2002

2019/1/14

#
When i put it in it gave me an <identifier> expected error. Here's what i tried anyway MyWorld:
public int prevScore;
private int score;
World2:
  public int time = 0;
private int score2;
 WorldTwo w2 = new WorldTwo();
w2.prevScore = score2+prevScore;
Greenfoot.setWorld(w2);
Also when the main actor touches the actor that transports him to World2:
  if (isTouching(Helipad.class))
        {
            MyWorld world = (MyWorld)getWorld();

            WorldTwo w2 = new WorldTwo();
            w2.prevScore = score2+prevScore;
            Greenfoot.setWorld(w2);

            score = score2;

        }
If I would get rid of the World2 version of it then i'd recieve errors in my main actor's version and if i deleted the main actor's version then the identifier expected errors would still be there
danpost danpost

2019/1/14

#
Now your world isn't called WorldTwo now is it.
Nate2002 Nate2002

2019/1/14

#
No it's called World2, I just changed all those WorldTwo's into World2 instead, but I keep getting the identifier expected error
danpost danpost

2019/1/14

#
Nate2002 wrote...
World2:
  public int time = 0;
private int score2;
 WorldTwo w2 = new WorldTwo();
w2.prevScore = score2+prevScore;
Greenfoot.setWorld(w2);
Really? The first two lines are fields -- the others are execution code. And why would you create a World2 object from within a World2 world? Remove lines 3 through 5 here in World2.
You need to login to post a reply.