I'm doing something wrong here with my random spawning...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
dementia_patient
Prole
Posts: 7
Joined: Sun Jan 06, 2013 2:50 am

I'm doing something wrong here with my random spawning...

Post 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.
Attachments
game.love
(9.02 KiB) Downloaded 82 times
User avatar
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..

Post 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
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
dementia_patient
Prole
Posts: 7
Joined: Sun Jan 06, 2013 2:50 am

Re: I'm doing something wrong here with my random spawning..

Post 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?
User avatar
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..

Post 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.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests