Page 1 of 1
I'm doing something wrong here with my random spawning...
Posted: Mon Jan 21, 2013 8:54 pm
by dementia_patient
The code is supposed to spawn spawn a new entity at a random location when the spawnTimer = 0 but it doesn't. It's probably a derp mistake but I can't seem to figure it out. File is attached.
Re: I'm doing something wrong here with my random spawning..
Posted: Mon Jan 21, 2013 9:38 pm
by ejmr
The code sets ent.x to a random number but this only happens once. So when ent_create() calls ent_spawn() it is always using the same ent.x value generated by the one call to math.random(). You probably want something like:
Code: Select all
function ent_create()
if spawn == true then
ent_spawn(math.random(10, 390),ents.y,id)
end
end
Re: I'm doing something wrong here with my random spawning..
Posted: Mon Jan 21, 2013 9:42 pm
by dementia_patient
I knew it was something easy like that >.<
Okay now...The draw function...If I use ents.x or the math.random(10, 390) it's going to spawn the entity one place and the image another...How do I avoid that?
Re: I'm doing something wrong here with my random spawning..
Posted: Mon Jan 21, 2013 10:13 pm
by ejmr
When you're building your table of entities use math.random() for the X coordinate of each, and then just use that value for both positioning the entity and drawing the image, which you could do by looping over that same table in both cases.