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

2014/2/12

Level problems

casper4444 casper4444

2014/2/12

#
Hello, I'm programming a game where the counter starts with 32. Everytime an enemy enters the field, it withdraws 1 (it says: "Enemies left: .."). If the counter is 0, I want the game to move forward to Level 2 but I don't know how to write it. I have the code
    public void nextLevel()
    {
        if (counter.getScore() == 0) {
            if (level == 1) {
                level = 2;
                Greenfoot.setWorld(new Level2());
            }
            else {
                level = 1;
                Greenfoot.setWorld(new Level1());
            }
        }
    }
and the 'private int level' & 'level = 1' (<-- in the act method) in my Level1 class and I have this
    /**
     * Return the current counter value.
     */
    public int getScore()
    {
        return score;
    }
in my Counter class. but it doesn't work
casper4444 casper4444

2014/2/12

#
Anyone know how to fix this?
danpost danpost

2014/2/12

#
Wait, this code is in the Level1 class, then just hard-code it to go to a new Level2 world and hard-code in the Level2 class for it to go back to a new Level1 world.
casper4444 casper4444

2014/2/12

#
But it doesn't do anything. If the score reaches 0, nothing happens
danpost danpost

2014/2/12

#
Please show the entire Level1 class code.
casper4444 casper4444

2014/2/13

#
public class Level1 extends World
{
    private int timer = 900;
    private Counter counter;
    private int level;
    public void act()
    {    
        nextLevel();
        Enemy1 enemy1 = new Enemy1();
        Enemy2 enemy2 = new Enemy2();
        Enemy3 enemy3 = new Enemy3();
        Enemy4 enemy4 = new Enemy4();
        if (timer>0)  
        /* Enemies have to be between 77-136, 182-242, 288-347 (Y-axis)
         */                  
        {  
            timer--;  
            if(timer == 800) {addObject(enemy2, 800, 77); score(); } 
            if(timer == 800) {addObject(enemy3, 800, 136);score(); }   
            if(timer == 800) {addObject(enemy4, 800, 190);score(); } 
            if(timer == 800) {addObject(enemy1, 800, 320);score(); } 
            if(timer == 750) {addObject(enemy2, 800, 100);score(); } 
            if(timer == 700) {addObject(enemy3, 800, 347);score(); } 
            if(timer == 650) {addObject(enemy4, 800, 240);score(); } 
            if(timer == 600) {addObject(enemy1, 800, 80); score(); } 
            if(timer == 600) {addObject(enemy2, 800, 190);score(); } 
            if(timer == 600) {addObject(enemy3, 800, 288);score(); } 
            if(timer == 600) {addObject(enemy4, 800, 340);score(); } 
            if(timer == 550) {addObject(enemy1, 800, 88); score(); } 
            if(timer == 500) {addObject(enemy2, 800, 199);score(); } 
            if(timer == 450) {addObject(enemy3, 800, 302);score(); } 
            if(timer == 400) {addObject(enemy4, 800, 182);score(); } 
            if(timer == 400) {addObject(enemy1, 800, 242);score(); } 
            if(timer == 400) {addObject(enemy2, 800, 100);score(); } 
            if(timer == 400) {addObject(enemy3, 800, 310);score(); } 
            if(timer == 350) {addObject(enemy4, 800, 302);score(); } 
            if(timer == 300) {addObject(enemy1, 800, 199);score(); } 
            if(timer == 250) {addObject(enemy2, 800, 88); score(); } 
            if(timer == 200) {addObject(enemy3, 800, 340);score(); } 
            if(timer == 200) {addObject(enemy4, 800, 288);score(); } 
            if(timer == 200) {addObject(enemy1, 800, 190);score(); } 
            if(timer == 200) {addObject(enemy2, 800, 80); score(); } 
            if(timer == 150) {addObject(enemy3, 800, 240);score(); } 
            if(timer == 100) {addObject(enemy4, 800, 347);score(); } 
            if(timer == 50) {addObject(enemy1, 800, 100); score(); } 
            if(timer == 0) {addObject(enemy2, 800, 320);  score(); } 
            if(timer == 0) {addObject(enemy3, 800, 190);  score(); } 
            if(timer == 0) {addObject(enemy4, 800, 136);  score(); } 
            if(timer == 0) {addObject(enemy1, 800, 77);   score(); }
        }  
    }

    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1(Counter counter)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 403, 1); 
        level = 1;
        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        counter = new Counter();
        addObject(counter, 754, 389);

        Text text = new Text();
        addObject(text, 105, 386);
        Castle castle3 = new Castle();
        addObject(castle3, 55, 316);
        Hero hero = new Hero();
        addObject(hero, 160, 220);
        Castle castle2 = new Castle();
        addObject(castle2, 55, 211);
        Castle castle = new Castle();
        addObject(castle, 55, 106);

    }

    private  void score()
    {
        counter.addScore();
    }

    public void nextLevel()
    {
        if (counter.getScore() == 0) {
            if (level == 1) {
                level = 2;
                Greenfoot.setWorld(new Level2());
            }
            else {
                level = 1;
                Greenfoot.setWorld(new Level1(counter));
            }
        }
    }
}
danpost danpost

2014/2/13

#
Insert at line 62 (before the call to 'prepare'): this.counter = counter; Change line 71 to: if (counter == null) counter = new Counter(); Curious: what does the 'addScore' method of the Counter class look like? Also, why is not the counter being passed back to the new Level2 worlds? or, why is the counter being passed at all? and where is the counter being set to 32?
danpost danpost

2014/2/13

#
Is it that you are incrementing the counter each time you add an Enemy object? and you are decrementing it each time your player eliminates one? Change line 8 to: if (timer == 0) nextLevel();
danpost danpost

2014/2/13

#
Actually, you do not even need to use the counter. Just use the actual state of the game:
if (timer == 0 && getObjects(Enemy.class).isEmpty())
or if the Enemy classes are not superclassed as such:
if (timer == 0 && getObjects(Enemy1.class).isEmpty() &&
                  getObjects(Enemy2.class).isEmpty() &&
                  getObjects(Enemy3.class).isEmpty() &&
                  getObjects(Enemy4.class).isEmpty())
You need to login to post a reply.