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

2020/11/6

How do i add objects randomly?

MrSkyPanda MrSkyPanda

2020/11/6

#
I'm trying to add random coins to my project for players to go and get but i have no clue how to do the code. Does anyone know how?
OfficialMajonaise OfficialMajonaise

2020/11/6

#
Something like this?
import greenfoot.*;
import java.util.Random;

public class CoinSpawner extends Actor
{
    Random random = new Random();
            spawnCooldown=Master.spawnDroneDelay;
        }    
    }
    
    public void spawnCoin() {
        getWorld().addObject(new Coin(), randomInt(<minX>, <maxX>), randomInt(<minY>, <maxY>));
    }
    
    public int randomInt(int min, int max) {
        return random.nextInt(max-min)+min;
    }
}
danpost danpost

2020/11/6

#
OfficialMajonaise wrote...
Something like this? << Code Omitted >>
'spawnCoolDown' is not defined; 'Master' is an unrecognized class; Anyway, using an Actor object just to spawn actors is not best. Spawning of coins would be a game behavior and should be done in the game World object.
MrSkyPanda MrSkyPanda

2020/11/9

#
so how would i code that?
danpost danpost

2020/11/9

#
MrSkyPanda wrote...
I'm trying to add random coins to my project for players to go and get but i have no clue how to do the code. Does anyone know how?
Question is vague. Are coins to be added prior to game play or during? If during, at set intervals of time or sporadic? And what restriction are there as to where to place a coin?
MrSkyPanda MrSkyPanda

2020/11/9

#
i'd like it to be added during and prior
danpost danpost

2020/11/9

#
MrSkyPanda wrote...
i'd like it to be added during and prior
And -- answers to my other questions?
You need to login to post a reply.