Page 1 of 2
Spawning an object
Posted: Fri Jan 15, 2016 6:42 pm
by CalebAnder
Hello, I have just started using love and I am really enjoying using it. In my game I am making, the player can move a plane with the arrow keys. I have done that successfully, but now I need bombs, so whenever someone presses the space bar key, a bomb spawns on the plane and falls down. How do I spawn the bomb when key pressed and how do I make it fall down? Thank you.
Re: Spawning an object
Posted: Fri Jan 15, 2016 8:03 pm
by s-ol
Could give you a working solution, but you will need to understand Lua in somewhat of it's entirety sooner or later anyway:
https://www.love2d.org/wiki/love.keypressed
http://www.lua.org/pil/1.html
You will need to keep track of a list of bullets (use a table here) and update (move) them every frame. When the key is pressed add a new one. Also make sure to remove them at some point.
Löve is a framework, not an egine, so to have them "fall down" there is no physics system or anything, you have to move them by yourself every frame by changing the position to draw each one to.
Re: Spawning an object
Posted: Fri Jan 15, 2016 9:42 pm
by bobbyjones
Well we do have a physics module but most are scared of it.
Re: Spawning an object
Posted: Sat Jan 16, 2016 12:31 am
by s-ol
bobbyjones wrote:Well we do have a physics module but most are scared of it.
It's not that we are scared of it, it just isn't the solution to every problem that involves moving stuff. In fact it is the solution to only a few rather select problems really.
Re: Spawning an object
Posted: Sat Jan 16, 2016 5:22 am
by CalebAnder
Well thanks for the help. It's okay if you give me a lot of code that is a bit advanced. I always just read the code and try to understand it. Could you give me some code on how to spawn an object? Thanks I really appreciate it.
Re: Spawning an object
Posted: Sat Jan 16, 2016 2:51 pm
by Beelz
The people around here are very generous and are usually willing to help... However, its nice to see that you put effort into trying to write the code yourself first, as opposed to just having everyone else solve your issues. You should either paste the part of your code that your having trouble with inside of a
Code: Select all
block, or upload a copy of the file(s).
Re: Spawning an object
Posted: Sat Jan 16, 2016 3:08 pm
by CalebAnder
Thanks Beelz. I always see if I can do it and I try before I ask. The problem is I'm not having trouble with my code. It's just that I really don't know how to spawn an object. I have researched it but have found no answer to my question.
Re: Spawning an object
Posted: Sat Jan 16, 2016 3:45 pm
by s-ol
Like I said, just keep track of all the objects in a list/table.
Code: Select all
bombs = {}
function love.update(dt)
for i,bomb in ipairs(bombs) do
bomb.y = bomb.y + 100*dt
end
end
function love.keypressed(key)
if key == " " then
bombs[#bombs+1] = {x=player.x, y=player.y}
end
end
function love.draw()
for i,bomb in ipairs(bombs) do
love.graphics.draw(bomb_sprite, bomb.x, bomb.y)
end
end
A very basic solution that doesn't ever remove bombs. You will need to use table.remove to properly remove bombs.
Re: Spawning an object
Posted: Sat Jan 16, 2016 4:08 pm
by CalebAnder
Thanks so much! But how do I make the bomb spawn on the plane? Sorry about asking to much you seem really helpful. I appreciate it
Re: Spawning an object
Posted: Sat Jan 16, 2016 4:17 pm
by zorg
When spawning a bomb, make the bomb's position (x and y coordinates) equal to the position where the plane is.