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

2023/4/2

need help with transferring the score counter to a new level

Merten Merten

2023/4/2

#
so i am trying to have a score counter on Level_1 and Level_2 but when you go to the next level the counter resets. it does work here but it just doesn't copy the amount from the past level. i dont know how to fix this so i would really like some help. Level_1
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Level_1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level_1 extends World
{
    public static int AantalPuntenLevel1;
    Teller teller = new Teller();
    Harry harry = new Harry();
    Barry barry = new Barry();
    Groen groen = new Groen();
    Level_2 level_2 = new Level_2();
    private int Ronde_1Punten = 8;
    public static boolean ronde_1;
    public static int welke_avatar;
    public static int xposSpeler;
    public static int yposSpeler;
    public static int Welk_Level;
    Gezondheid gezondheid = new Gezondheid();
    
    public static int TotGezondheid;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Level_1()
    {    
        super(1366, 768, 1); 
        prepare();
        teller = new Teller();
        addObject(teller, 40, 340);
        addObject(gezondheid, 555, 10);  
    }

    public void act()
    {
        barry();
        ronde_1();
        ronde_1Klaar();
    }

    public Teller krijgTeller()
    {
        return teller;
    }

    public Gezondheid krijgGezondheid()
    {
        return gezondheid;
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {       
        addObject(barry,115,302);
        addObject(harry, 781, 702);
        barry_start();
        
    }

    public void fighter_avatar()
    {
        addObject(new Speler_Fighter(), 1000, 600);
        welke_avatar = 1;
    }

    public void hunter_avatar()
    {
        addObject(new Speler_Hunter(), 1000, 600);
        welke_avatar = 2;
    }

    public void tank_avatar()
    {
        addObject(new Speler_Tank(), 1000, 600);
        welke_avatar = 3;
    }

    public void barry_start()
    {
        ronde_1 = false;
    }

    public void barry()
    {
        if (Greenfoot.mouseClicked(barry)) {
            ronde_1 = true;
        }
    }

    //alles wat er gebeurd tijdens ronde 1
    public void ronde_1()
    {
        if (ronde_1 == true) {
            if (Greenfoot.getRandomNumber(350) < 4) {
                addObject(new Groen(), Greenfoot.getRandomNumber(1020) + 180 ,Greenfoot.getRandomNumber(410) + 160);
            }
        }
    }

    public void ronde_1Klaar()
    {
        if (teller.totaalAantalPunten >= Ronde_1Punten) {
            ronde_1 = false;
            ronde_1Einde();
            /*if (getObjects(Groen.class) == null){
            ronde_1Einde();
            test1 = true;
            }*/
        }
    }

    //einde ronde
    public void ronde_1Einde() 
    {
        Welk_Level = 1;
        Greenfoot.setWorld(level_2);
        if (welke_avatar == 1){
            level_2.fighter_avatar(xposSpeler, yposSpeler);
        }
        else if (welke_avatar == 2){
            level_2.hunter_avatar(xposSpeler, yposSpeler);
        }
        else if (welke_avatar == 3){
            level_2.tank_avatar(xposSpeler, yposSpeler);
        }
    }
}
Level_2
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Level_2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level_2 extends World
{

    Teller teller = new Teller();
    Barry barry = new Barry();
    Harry harry = new Harry();
    Groen groen = new Groen();
    private int Ronde_2Punten = 8;
    public static boolean ronde_2;
    Slime_Koning slime_koning = new Slime_Koning();
    public static int xposSpeler;
    public static int yposSpeler;
    Level_3 level_3 = new Level_3();
    Gezondheid gezondheid = new Gezondheid();
    /**
     * Constructor for objects of class Level_2.
     * 
     */
    public Level_2()
    {    

        super(1366, 768, 1); 
        prepare();
        teller = new Teller();
        addObject(teller, 40, 340);
        addObject(gezondheid, 555, 10);   
    }

    public void act()
    {
        ronde_2();
        barry();
        ronde_2Klaar();
    }

    public Teller krijgTeller()
    {
        return teller;
    }
    
    public Gezondheid krijgGezondheid()
    {
        return gezondheid;
    }

    private void prepare()
    {
        addObject(barry,115,302);
        addObject(harry, 781, 702);
        barry_start();
        
    }

    public void fighter_avatar(int x, int y)
    {
        addObject(new Speler_Fighter(), x, y);

    }

    public void hunter_avatar(int x, int y)
    {
        addObject(new Speler_Hunter(), x, y);

    }

    public void tank_avatar(int x, int y)
    {
        addObject(new Speler_Tank(), x, y);

    }

    public void barry_start()
    {
        ronde_2 = false;
    }
    
    public void barry()
    {
        if (Greenfoot.mouseClicked(barry)) {
            ronde_2 = true;
        }
    }

    //alles wat er gebeurd tijdens ronde 2
    public void ronde_2()
    {
        if (ronde_2 == true) {
            if (Greenfoot.getRandomNumber(300) < 4) {
                addObject(new Groen(), Greenfoot.getRandomNumber(1020) + 180 ,Greenfoot.getRandomNumber(410) + 160);
                addObject(new Blauw(), Greenfoot.getRandomNumber(1020) + 180 ,Greenfoot.getRandomNumber(410) + 160);
            }
        }
    }

    public void ronde_2Klaar()
    {
        if (teller.totaalAantalPunten >= Ronde_2Punten) {
            ronde_2 = false;
            ronde_2Einde();
            /*if (getObjects(Groen.class) == null){
            ronde_1Einde();
            }*/
        }

    }

    //einde ronde (alles wordt klaargezet voor de volgende ronde)
    public void ronde_2Einde() 
    {
        Greenfoot.setWorld(level_3);
        if (Level_1.welke_avatar == 1){
            level_3.fighter_avatar(xposSpeler, yposSpeler);
        }
        else if (Level_1.welke_avatar == 2){
            level_3.hunter_avatar(xposSpeler, yposSpeler);
        }
        else if (Level_1.welke_avatar == 3){
            level_3.tank_avatar(xposSpeler, yposSpeler);
        }
        Level_1.Welk_Level = 2;
    }
}
Kogel
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Kogel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Kogel extends beweeg
{
    private Speler speler;
    private int kogelSnelheid = 5;
    private int richting;
    boolean aanraak = false;
    private int punten_groen = 2;
    private int punten_blauw = 5;
    /**
     * Act - do whatever the Kogel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        geschoten();
        move(kogelSnelheid);

    }

    public void geschoten()
    {
        int ypos = getY();
        int xpos = getX();

        if (ypos < 5 || xpos < 5 || ypos > 763 || xpos > 1361){
            getWorld().removeObject(this);
        }
        else  {
            Actor groen = getOneIntersectingObject(Groen.class);
            Actor blauw = getOneIntersectingObject(Blauw.class);
            if (groen != null) {
                updateScore(punten_groen);
                getWorld().removeObject(groen);
                getWorld().removeObject(this);
            }
            if (blauw != null) {
                updateScore(punten_blauw);
                getWorld().removeObject(blauw);
                getWorld().removeObject(this);
            }
        }
    }

    public Kogel(int rot)
    {
        setRotation(rot);
        if (rot > 0 && rot < 90) {
            setRotation(0);
        }
        if (rot > 90 && rot < 180) {
            setRotation(90);
        }
        if (rot > 180 && rot < 270) {
            setRotation(180);
        }
        if (rot > 270 && rot < 360) {
            setRotation(270);
        }
    }

    public void updateScore(int aantal)
    {
        if (Level_1.Welk_Level == 0){
            Level_1 level_1 = (Level_1) getWorld();
            Teller teller = level_1.krijgTeller();
            teller.bumpCount(aantal);
        }
        if (Level_1.Welk_Level == 1){
            Level_2 level_2 = (Level_2) getWorld();
            Teller teller = level_2.krijgTeller();
            teller.bumpCount(aantal);
        }
        if (Level_1.Welk_Level == 2){
            Level_3 level_3 = (Level_3) getWorld();
            Teller teller = level_3.krijgTeller();
            teller.bumpCount(aantal);
        }
    }

}
Teller
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Teller here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Teller extends Actor
{
    public int totaalAantalPunten;

    public Teller()
    {
        setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLUE));
    }
    public void bumpCount(int aantal)
    {
        totaalAantalPunten += aantal;
        setImage(new GreenfootImage("" + totaalAantalPunten, 20, Color.WHITE, Color.BLACK));
    }
}
danpost danpost

2023/4/2

#
Merten wrote...
so i am trying to have a score counter on Level_1 and Level_2 but when you go to the next level the counter resets. it does work here but it just doesn't copy the amount from the past level. i dont know how to fix this so i would really like some help. << Codes Omitted >>
When changing levels, do not create a new counter. Just take the counter that is in the current world and put it in the next world. For example, when going from level 1 to level 2, change line 12 in the Level_2 class to:
Teller teller;
and also,remove lines 32 and 33 from the Level_2 class. Then, insert the following at line 124 in the Level_1 class:
level_2.addObject(teller, 40, 340);
Do similarly when going to higher levels.
Merten Merten

2023/4/4

#
i have done that but now at the end of level 2 the game crashes when i hit an enemy. error: java.lang.NullPointerException. this is at class Kogel, line 80
danpost danpost

2023/4/4

#
Merten wrote...
i have done that but now at the end of level 2 the game crashes when i hit an enemy. error: java.lang.NullPointerException. this is at class Kogel, line 80
Along with the addObject line you inserted above, also add the following line:
Level_2.teller = teller;
(and similiarly with other level changes)
You need to login to post a reply.