[SOLVED] Shooting

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
User avatar
lalorobot
Prole
Posts: 16
Joined: Sat Apr 19, 2014 12:06 am
Contact:

[SOLVED] Shooting

Post 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
Attachments
bullet.love
(19.39 KiB) Downloaded 151 times
Last edited by lalorobot on Sun May 25, 2014 12:54 am, edited 1 time in total.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Shooting

Post 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. ;)
Attachments
bullet.love
(19.83 KiB) Downloaded 164 times
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
lalorobot
Prole
Posts: 16
Joined: Sat Apr 19, 2014 12:06 am
Contact:

Re: Shooting

Post 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!
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: [SOLVED] Shooting

Post by davisdude »

Yes! Hours of photoshop finally paid off! :)
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest