Bullet's speed mysteriously dependant on amount of bullets?

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
JohnnyC
Prole
Posts: 16
Joined: Mon Aug 15, 2016 11:39 pm

Bullet's speed mysteriously dependant on amount of bullets?

Post by JohnnyC »

So I have a shmup and right now in my code, the enemy just fires every second.

Every second the enemy fires the function triSpreadAimed()

Code: Select all

function triSpreadAimed(enemy,amount,spread,speed)
	local v = enemies[enemy]	
	if v == nil then
		return
	end
	
	if v ~= nil then
		for i = 1, amount do	
			local a = math.deg(math.atan2(player.y - v.y, player.x - v.x)) + 360 -- angle
			a2 = a + spread
			a3 = a - spread
			bulletsCreate(v.x, v.y, a,20,speed, i) -- v.x = enemy x coordinate
			bulletsCreate(v.x, v.y, a2,20, speed, i) -- v.y = enemy y coordinate
			bulletsCreate(v.x, v.y, a3,20,speed, i) -- i = bullet ID
			wait(0.2)
		end
	end
end
It then goes to bulletsCreate()

Code: Select all

function bulletsCreate(x,y,angle,speed,img,id)
	bulletCount = bulletCount +1
	bullet = {}
	bullet.img = img
	bullet.x = x
	bullet.y = y
	bullet.s = speed
	bullet.a = angle
	bullet.p = 'test'
	bullet.id = id
	
	table.insert(bullets, bullet)
end
Which is called by updateBullet()

Code: Select all

function updateBullets(dt)
	for i,b in ipairs(bullets) do
		fireTest(b,dt)
		bulletRemove(i,b)
	end
end
Which fires well fireTest()

Code: Select all

function fireTest(b,dt)
	for i = 1, b.id do
		b.x = b.x + math.cos(math.rad(b.a)) * (dt * b.s)
		b.y = b.y + math.sin(math.rad(b.a)) * (dt * b.s)
	end
end
I think my eyes hurt from working on this the last few hours but I can't see exactly what is wrong. Anyone have an idea?
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Bullet's speed mysteriously dependant on amount of bullets?

Post by Plu »

What's that "wait(0.2)" function?

And can you include a full .love? That's a lot easier to debug.
JohnnyC
Prole
Posts: 16
Joined: Mon Aug 15, 2016 11:39 pm

Re: Bullet's speed mysteriously dependant on amount of bullets?

Post by JohnnyC »

Plu wrote:What's that "wait(0.2)" function?

And can you include a full .love? That's a lot easier to debug.
I'm actually quite embarrassed of having my full code out because I'm new and still learning so I refrained earlier. I'm sorry, it is attached now.

As for that wait code, that is actually old leftover that I cleaned when I noticed it in the code. However forgot to clean it in the actual thread. The idea was that I would fire 3 bullets then wait a bit before firing the next 3 bullets.

edit: I figured out my issue, it is the for loop in fire test. Essentially there's a for loop in a for loop that shouldn't be there. However fixing that makes all the bullets now stack up per id. To stop bullets from stacking up I moved the for loop all the way back in the level code and put a wait.

Code: Select all

for i = 1, 8 do
	tripleSpreadAimed(y,1,15,400) 
	wait(0.05)
end
However now I have to clean up the code cause the second value(amount) is no longer used.

EDIT: 2
I changed everything entirely. I now have bullets generated by the enemy during enemyUpdate(). From there it calls different patterns based on the enemy and the frequency they fire at using cron.timer. This solves all the headache of errors from calling bullets for enemies that no longer exist.
Attachments
Shmup.rar
(368.81 KiB) Downloaded 80 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 5 guests