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

2019/5/11

New Level Based on Score

oltmanac oltmanac

2019/5/11

#
I am trying to go to a new level once I reach 20 points. The new level is just a new background and will run the same as my first level. Right now it won't switch to the new level.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class OceanWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class OceanWorld extends World
{
    public int score;
    /**
     * Constructor for objects of class OceanWorld.
     * 
     */
    public OceanWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 400, 1); 
        prepare();
        setPaintOrder(Border.class, Seal.class, Seaweed.class, BigFish.class, Starfish.class, 
        Coral.class, Turtle.class);
        showScore();
        nextLevel2();
        score=0;
        getScore();
        
    }
    
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) < 2)
        {
            addObject(new Sand(), 799, 372);
        }
        
         if (Greenfoot.getRandomNumber(100) < 2)
        {
            addObject(new Waves(), 799, 28);
        }
        
         if (Greenfoot.getRandomNumber(400) < 1)
        {
            addObject(new Coral(), 799, 350);
        }
        
         if (Greenfoot.getRandomNumber(255) < 1)
        {
            addObject(new Seaweed(), 799, 350);
        }
        
         if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Starfish(), 799, Greenfoot.getRandomNumber(360));
        }
        
         if (Greenfoot.getRandomNumber(100) < 2)
        {
            addObject(new BigFish(), 799, Greenfoot.getRandomNumber(360));
        }
        
         if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Turtle(), 799, Greenfoot.getRandomNumber(360));
        }
    }
    
   public int getScore()

    {
       return score; 
    }
    
    public void prepare()
    {
        Seal seal = new Seal();
        addObject(seal, 150, 190);
        
        Sand sand = new Sand();
        addObject(sand, 477, 372);
        Sand sand2 = new Sand();
        addObject(sand2, 4, 372);
        Sand sand3 = new Sand();
        addObject(sand3, 229, 369);
        Sand sand4 = new Sand();
        addObject(sand4, 750, 373);
        
        Waves waves = new Waves();
        addObject(waves, 122, 28);
        Waves waves2 = new Waves();
        addObject(waves2, 369, 28);
        Waves waves3 = new Waves();
        addObject(waves3, 594, 28);
        Waves waves4 = new Waves();
        addObject(waves4, 753, 28);
        
        Border border = new Border();
        addObject(border, 0, 200);
        Border border2 = new Border();
        addObject(border2, 730, 200);
    }
    
    public void addScore(int points)
    {
        score = score + points;
        showScore();
    }
    
    private void showScore()
    {
        showText("Score:" + score, 80, 25);
    }
    
    public void nextLevel2()
    {
        
        if(score>=20)
        {
            Greenfoot.setWorld(new Level2());
        }
    }
    
    
}
Zamoht Zamoht

2019/5/12

#
You have to move the "nextLevel2" method call from the constructor to the "act" method, so the check is performed during each act cycle.
You need to login to post a reply.