Maybe you found out a new way. For me, it would be too annoying to put this counter every time you add an object. But nevertheless, you did find a solution!
public class world1 extends World
{
Score slot0 = new Score(); //Score digits
Score slot1 = new Score();
Score slot2 = new Score();
Score slot3 = new Score();
Score slot4 = new Score();
Score slot5 = new Score();
Score slot6 = new Score();
Score slot7 = new Score();
int slotdigit1; //The most left digit of the score
int slotdigit2;
int slotdigit3;
int slotdigit4;
int slotdigit5;
int slotdigit6; //The most right digit of the score
int scorenumber = 0; //The actual score
public world1(){
addObject(slot0, 621, 100);
addObject(slot1, 638, 100);
addObject(slot2, 655, 100);
addObject(slot3, 672, 100);
addObject(slot4, 689, 100);
addObject(slot5, 706, 100);
addObject(slot6, 723, 100);
addObject(slot7, 740, 100);
slot0.digit = -1; //These slots are empty
slot7.digit = -1;
slot0.setImage("nr_empty.png");
slot7.setImage("nr_empty.png");
}
void score(){
slot1.digit = Math.round((scorenumber % 1000000)/100000);
slot2.digit = Math.round((scorenumber % 100000)/10000);
slot3.digit = Math.round((scorenumber % 10000)/1000);
slot4.digit = Math.round((scorenumber % 1000)/100);
slot5.digit = Math.round((scorenumber % 100)/10);
slot6.digit = Math.round((scorenumber % 10));
}
//Example of points being added.
public void game(){
if(!eatpizza){
if((pizza.getImage().getTransparency() == 0)){
scorenumber += 150;
removeObject(pizza);
eatpizza = true;
}
//add some more score counters for example (make a boolean sharkhitboat in the constructor, starting it at false).
if(!sharkhitboat)
if(shark/**Your name of the shark must be at the left side of this comment*/.getImage().getTransparency()== 0)){
scorenumber += 50; //50 score is added.
removeObject(/**sharkname*/);
sharkhitboat = true;
}
//call all methods.
public void act(){
int playerx = player.getX();
score();
game();
}
}
}
public void act()
{
if(digit == 0){
setImage("nr_0.png");
}
if(digit == 1){
setImage("nr_1.png);
}
if(digit == 2){
setImage("nr_2.png");
}
if(digit == 3){
setImage("nr_3.png");
}
if(digit == 4){
setImage("nr_4.png");
}
if(digit == 5){
setImage("nr_5b.png");
}
if(digit == 6){
setImage("nr_6.png");
}
if(digit == 7){
setImage("nr_7.png");
}
if(digit == 8){
setImage("nr_8.png");
}
if(digit == 9){
setImage("nr_9.png");
}
}
}
int score = 0;
public void act()
{
setImage(new GreenfootImage("Score : " + score, 30, Color.BLACK, null));
}
public void addScoreShark()
{
score -= 15;
}
public void addScoreKratten()
{
score += 40;
}
public void addScoreFout()
{
score -= 70;
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class GameOver here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class GameOver extends Actor
{
/**
* Act - do whatever the GameOver wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public GameOver()
{
setImage(new GreenfootImage("GAME OVER", 60, Color.RED, null));
Greenfoot.playSound("fail-trombone-03.wav");
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class GAMEOVERSCHERM here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class GAMEOVERSCHERM extends World
{
/**
* Constructor for objects of class GAMEOVERSCHERM.
*
*/
GreenfootSound backgroundMusic = new GreenfootSound("emerald.wav");
public GAMEOVERSCHERM()
{
super(400,300,1);
Greenfoot.playSound("fail-trombone-03.mp3");
addObject(new TRYAGAIN(), 200,80);
if (backgroundMusic.isPlaying())
{
backgroundMusic.stop();
}
}
}