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

2016/4/7

My code is adding Infinite objects while I only want to add one

madeirense madeirense

2016/4/7

#
    //Check level
    private void verificanivel()
    {
        // check if level is over, if YES then it sapwns a portal, if portal is not present then it goes to next level
        if (getObjects(Asteroid.class).isEmpty() && getObjects(SmallAsteroid.class).isEmpty() && getObjects(Ship2.class).isEmpty() && getObjects(IceAsteroid.class).isEmpty() && getObjects(IceAsteroid2.class).isEmpty())
        {
            Portal miniportal = new Portal();
            int x = Greenfoot.getRandomNumber(1366);
            int y = Greenfoot.getRandomNumber(768);
            addObject(miniportal, x, y); 
            if(getObjects(Portal.class).isEmpty())
            {
                nextLevel(this);
            }           
        }
    }
It is for some reason spawning Infitine Portals while I just need one. I'll post the rest of the code here of my World anyway if you need to see it.
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;

public class AsteroidWorld extends World
{
    int counter = 0;
    //int music = 1;
    
    Score score;
    Bonus bonus;
    Shield shield;
    
    static Actor levelDisplay;
    static int level;
    
    private boolean gameOver = false;
    
    GreenfootSound bgm = new GreenfootSound("bgm.mp3");
    
    /**
     * Constructor for objects of class AsteroidWorld.
     * 
     */
    public AsteroidWorld()
    {
        super(1366, 768, 1, false); 
        
        //Adicona a nossa nave ao mundo.
        //Ship bob = new Ship();
        //addObject(bob, getWidth()/2, getHeight()/2);
        
        //Adicona a pontuação ao mundo.
        score = new Score();
        addObject(score, 60, 25);
        
        //Adicona o bónus ao mundo.
        bonus = new Bonus();
        addObject(bonus, 170, 25);
        
        //Adicona o escudo ao mundo(nave).
        shield = new Shield();
        addObject(shield, 683, 25);
        
        //Nível, ou seja, a dificuldade inicial.
        level = 0;
        levelDisplay = new Generic();
        nextLevel(this);
        addObject(levelDisplay, 1270, 740);
        
        //Coloca estrelas de cor Preta no mundo.
        createStars(750);
    }
    
    // Adiciona as naves inimigas.
    private void addShip2()
    {
        int x = Greenfoot.getRandomNumber(1366);
        int y = Greenfoot.getRandomNumber(768);
        addObject(new Ship2(), x, y);
    }
    
    //Cria estrelas de cor preta em posições aleatórias.
    private void createStars(int number) 
    {
        GreenfootImage background = getBackground();             
        for(int i=0; i < number; i++) 
        {
            int x = Greenfoot.getRandomNumber( getWidth());
            int y = Greenfoot.getRandomNumber( getHeight());
            int color = Greenfoot.getRandomNumber(175) + 50;
            background.setColor(new Color(0,0,0));
            background.fillOval(x, y, 3, 3);
        }
    }
    
    //Se já não houver inimigos no mundo o nível, ou seja a dificuldade, aumenta
    public void act()
    {
        /*if(music == 1)
        {
            Greenfoot.playSound("bgm.mp3");
            music = 0;
        }*/
        
        counter++;
        
        for(int i=0; i<2 ; i++)
        {
            verificanivel();
            verificapup();
        }
    }
    
    //verifica o nivel para adicionar o PowerUP
    private void verificapup()
    {
        // Verifica se o nível acabou.
        if(level == 2)
        {
            PowerUP pup = new PowerUP();
            int x = Greenfoot.getRandomNumber(1366);
            int y = Greenfoot.getRandomNumber(768);
            addObject(pup, x, y);
        }
    }
    
    //verifica o nivel
    private void verificanivel()
    {
        // Verifica se o nível acabou.
        if (getObjects(Asteroid.class).isEmpty() && getObjects(SmallAsteroid.class).isEmpty() && getObjects(Ship2.class).isEmpty() && getObjects(IceAsteroid.class).isEmpty() && getObjects(IceAsteroid2.class).isEmpty())
        {
            Portal miniportal = new Portal();
            int x = Greenfoot.getRandomNumber(1366);
            int y = Greenfoot.getRandomNumber(768);
            addObject(miniportal, x, y); 
            if(getObjects(Portal.class).isEmpty())
            {
                nextLevel(this);
            }           
        }
    }
    
    // Adiciona Asteroide
    private void addAsteroid()
    {
        for (int i=0; i<level+1; i++)
        {
            //int x = (Greenfoot.getRandomNumber(2)+400)%401+50;
            //int y = (Greenfoot.getRandomNumber(2)+400)%401+50;
            int x = Greenfoot.getRandomNumber(1366);
            int y = Greenfoot.getRandomNumber(768);
            addObject(new Asteroid(), x, y);
        }
    }
    
    // Adiciona a nossa nave ao mundo.
    private void addShip()
    {
        addObject(new Ship(), getWidth()/2, getHeight()/2);
    }
    
    // Adiciona Asteroides de gelo.
    private void addIceAsteroid()
    {
        int x = Greenfoot.getRandomNumber(1366);
        int y = Greenfoot.getRandomNumber(768);
        addObject(new IceAsteroid(), x, y);
    }
    
    // level changer/displayer
    public static void nextLevel(AsteroidWorld space)
    {
        level++;
        levelDisplay.setImage(new GreenfootImage("Difficulty: "+level, 36, java.awt.Color.black.brighter(), null));
        space.addAsteroid();
        space.addShip2();
        space.addIceAsteroid();
        space.addShip();
    }
}
madeirense madeirense

2016/4/7

#
The comments are not in English as you can see, if there's something you need to know just ask me :)
Super_Hippo Super_Hippo

2016/4/7

#
You execute the verificanivel-method twice each act-cycle. And if there is no asteroid (of any type) and no ship2, it spawns a portal at random position. After adding the portal to the world, you check if there is a portal in the world to see if you can advance to the next level. Of course there is a portal in the world (it was just added). In the next execution of the method, the condition is still passed and a new portal is added. You could merge line 11 into line 5, so you only add one. Lines 11-14 can be removed then. Then you can check an intersecting with the portal in the Ship class and advance to the next level after that. Don't use static variables and methods when there is no reason for it. Since I did that as well when I started programming, I guess you added the static modifier after seeing a error message like 'non-static method xy cannot be...'. I don't know how it was exactly. If you see that, you need to get a reference to the object to call a method on and not the class. Just make a method/variable static for no reason is not good.
You need to login to post a reply.