I'm doing something wrong here with my random spawning...
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 7
- Joined: Sun Jan 06, 2013 2:50 am
I'm doing something wrong here with my random spawning...
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.
- Attachments
-
- game.love
- (9.02 KiB) Downloaded 82 times
- ejmr
- Party member
- Posts: 302
- Joined: Fri Jun 01, 2012 7:45 am
- Location: South Carolina, U.S.A.
- Contact:
Re: I'm doing something wrong here with my random spawning..
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
-
- Prole
- Posts: 7
- Joined: Sun Jan 06, 2013 2:50 am
Re: I'm doing something wrong here with my random spawning..
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?
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?
- ejmr
- Party member
- Posts: 302
- Joined: Fri Jun 01, 2012 7:45 am
- Location: South Carolina, U.S.A.
- Contact:
Re: I'm doing something wrong here with my random spawning..
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.
Who is online
Users browsing this forum: No registered users and 8 guests