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

2020/10/20

how to make nonrenewable collectables?

wisetrap11 wisetrap11

2020/10/20

#
I have code to make collectable pizzas, but I want to keep them from respawning once they're collected. How do I do that?
public void createPizza()
    {
        //creates collectable pizzas
        pizza yummy = new pizza();
        GreenfootImage image = yummy.getImage();
        int numOfPizzas = Greenfoot.getRandomNumber(10) + 8;
        if(getObjects(pizza.class).size() < numOfPizzas)
        {
           addObject(yummy, Greenfoot.getRandomNumber(551), Greenfoot.getRandomNumber(354));
        }
    }
DatHeroAndy DatHeroAndy

2020/10/20

#
I have put the provided method to the world class:
import lang.stride.*;
import java.util.*;
import greenfoot.*;

public class MyWorld extends World
{
    private int numOfPizzas = Greenfoot.getRandomNumber(10) + 8;
    
    public MyWorld()
    {
        super(600, 400, 1);
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
        this.createPizza();
    }

    public void createPizza()
    {
        Pizza yummy =  new  Pizza();
        GreenfootImage image = yummy.getImage();
        if (getObjects(Pizza.class).size() < numOfPizzas) {
            addObject(yummy, Greenfoot.getRandomNumber(551), Greenfoot.getRandomNumber(354));
        }
    }
}
I have moved the numOfPizzas Variable to the top and the createPizza() method is in the constructor 18 times (because this is the maximum amount of pizzas that can spawn) Now the pizzas can be collected without respawning again.
wisetrap11 wisetrap11

2020/10/20

#
I'm sorry, but this didn't work. The program functions exactly how it did before (spawning a new pizza in for every pizza collected).
DatHeroAndy DatHeroAndy

2020/10/20

#
Show me all the codes of the classes please.
wisetrap11 wisetrap11

2020/10/21

#
public class background extends World
{
    private static final GreenfootImage sky = new GreenfootImage("sky.png");
    private protagonist player = new protagonist();
    private ground dirt = new ground();
    private store pizzaParlor = new store();
    private score scoreCounter = new score();
    /*
     * Constructor for objects of class background.
     * 
     */
    public background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        //add the sky as the background
        setBackground(sky);
        //add objects to the world
        populate();
        //draw objects on top of each other correctly
        setPaintOrder(score.class, ground.class, pizza.class, protagonist.class, store.class);
    }
    
    public void act()
    {
        createPizza();
    }
    
    public void populate()
    {
        addObject(player, 20, 353);
        addObject(dirt, 300, getHeight()-10);
        addObject(pizzaParlor, 575, 340);
        addObject(scoreCounter, getWidth() / 2 + 25, 30);
        scoreCounter.setScore(0);
    }
    
    public void createPizza()
    {
        //spawns in up to 18 pizzas which can be collected
        pizza yummy = new pizza();
        GreenfootImage image = yummy.getImage();
        int numOfPizzas = Greenfoot.getRandomNumber(10) + 8;
        if(getObjects(pizza.class).size() < numOfPizzas)
        {
           addObject(yummy, Greenfoot.getRandomNumber(551), Greenfoot.getRandomNumber(354));
        }
    }
    
    public score getScore()
    {
        return scoreCounter;
    }
}
DatHeroAndy DatHeroAndy

2020/10/21

#
You're supposed to put the 18 createPizza()'s into the constructor of your class and not in the act() method.
DatHeroAndy DatHeroAndy

2020/10/21

#
And I would recommend to put the numOfPizzas Variable to the top with the other variables (protagonist, ground, store etc.) because every time you run the createPizza() method, a new number in the variable will be generated. At the top it will only generate a number once.
danpost danpost

2020/10/21

#
wisetrap11 wrote...
I have code to make collectable pizzas, but I want to keep them from respawning once they're collected. How do I do that?
Whar are you wanting? Pick one: (1) all pizzas to appear at once; (2) one pizza at a time; (3) something else (specify)
wisetrap11 wisetrap11

2020/10/21

#
Actually, I've figured it out now. Thanks for the help, though.
You need to login to post a reply.