Page 1 of 1

[SOLVED] Shooting

Posted: Sat May 24, 2014 9:02 pm
by lalorobot
After trying (and failing) before I have realized bullets hate me.
The problem: it doesn't work, at all. The bullets seem to teleport

main.lua:

Code: Select all

HC = require 'hardoncollider'

function on_collide(dt, shape_a, shape_b)
	
end

function love.load()
	Collider = HC(100, on_collide)
	
	bullets = {}
	pData = {x = 400, y = 300}
	player = Collider:addRectangle(pData.x, pData.y, 32, 32)
    bullet = {}
    bullet.radius = 5
    bullet.x = pData.x
    bullet.y = pData.y
	bullet.xspeed = 0
	bullet.yspeed = 0
	bullet.speed = 500
	bullet.bul = Collider:addCircle(bullet.x, bullet.y, bullet.radius)
	table.insert(bullets, bullet)
end

function love.update(dt)
	Collider:update(dt)
	for i,v in ipairs(bullets) do
		v.x = v.x + v.xspeed * dt
		v.y = v.y + v.yspeed * dt
	end
end

function love.draw()
	for i,v in ipairs(bullets) do
		v.bul:draw('line')
	end
	player:draw("line")
end

function love.keypressed(key)
	if key == "left" then
		bullet.bul = Collider:addCircle(bullet.x, bullet.y, bullet.radius)
		bullet.xspeed = -bullet.speed
		table.insert(bullets, bullet)
	elseif key == "right" then
		bullet.bul = Collider:addCircle(bullet.x, bullet.y, bullet.radius)
		bullet.xspeed = bullet.speed
		table.insert(bullets, bullet)
	end
end
Any help will be apreciated

Re: Shooting

Posted: Sat May 24, 2014 10:27 pm
by davisdude
The problem here is that you keep on compounding the value, making it get larger and smaller with ever key-press. I changed some things to make it work better. Also, I added an image, as I could not see the bullet. Feel free to remove it, but I spent a lot of time on it. ;)

Re: Shooting

Posted: Sun May 25, 2014 12:54 am
by lalorobot
Thanks! :awesome: You have saved me from hours of thinking and trying stuff without any results

PS: 'thing.png' shall be featured in the game!

Re: [SOLVED] Shooting

Posted: Sun May 25, 2014 1:31 am
by davisdude
Yes! Hours of photoshop finally paid off! :)