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):
and here's what i did for World2:
If anyone knows what i should fix please let me know and thank you in advance.
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); } }
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(); } }