Continuous flow of objects

Hello

I have a question i cant resolve that is likely very trivial. I am making a road simulation, and i am currently at a state where i have a road with lanes and cars overtaking etc. I create the cars on the road by clicking a swing button that calls : carid = new ACar().makeCar(1,speed,cars,carid,0);



I want to have cars appear in a continuous flow, but if i put any kind of delay (such as sleep etc) in a loop that creates them, the whole application just freezes for the duration of the delay then lots of cars appear at once.



Sorry if this is trivial but i cant figure out a way around this.



Any help appreciated.




call the method inside update and surround it with

if (Math.random() < 0.01) {your creation code here}



adjust the number if nessesary and multiply it with your time per frame or whatever you need to do to make it run properly



the random number will give you a continous, but a little more natural flow (sometimes more, sometimes less cars)

imho a bad idea: on faster cpu/gpu much more cars would be casted. rather create a variable which stores the last time a car was castet and use System.nanos() or another timer-function to set a delay (check for delta every frame). U still can vary the spawning with random but this way it's speed independent! :slight_smile:

Math.random()constanttimePerFrameInSecondsAsFloat

good idea! this would still vary a little (except you update the TimePerSecond… every few frames) but is much easier to code  :slight_smile:

i know this is somewhat unrelated to you original question, but anyway :slight_smile:

you are calling

carid = new ACar().makeCar(1,speed,cars,carid,0);





if makeCar() is a Factory Method, which returns a new ACar Object, then maybe you want to change the line to:

carid = ACar.makeCar(...);


declare makeCar() public and static if needed.

i dont really c whats the problem here.



i create a method which adds a car node to the root node.



and call that method with ur GUI handler if the button is pressed.



y do u need to sleep the thread?

Thank you all for your advice, i have a nice continuous flow of cars now :slight_smile:



Gareth.