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

2017/5/19

Greenfoot Glitch

n_ibra n_ibra

2017/5/19

#
Hi, I am trying to do a project for my computer class but even though the code is correct, the program continues to glitch. In the project, we have to make a car appear at a 3% probability and we are using these strings of code: World myWorld = getWorld(); if(Greenfoot.getRandomNumber(100)< 3) { myWorld.addObject(new Vehicle(), Greenfoot.getRandomNumber(200)+200, 0); } Whenever the program runs though, many cars appear and cover the entire screen. Could you please help me?
danpost danpost

2017/5/19

#
Move the code to your World subclass (into its act method -- create one if not already there). Remove the first line and the first part of the fourth ('myWorld.').
Yehuda Yehuda

2017/5/19

#
Since the code is in an Actor the code is being run in every Actor that's in the World, so the more Actor's that are in the World the more this code is being run, so the amount of Vehicles multiplies (exponentially).
You need to login to post a reply.