Spawning an object
-
- Prole
- Posts: 16
- Joined: Fri Jan 15, 2016 6:33 pm
Spawning an object
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
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.
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.
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: Spawning an object
Well we do have a physics module but most are scared of it.
Re: Spawning an object
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.bobbyjones wrote:Well we do have a physics module but most are scared of it.
-
- Prole
- Posts: 16
- Joined: Fri Jan 15, 2016 6:33 pm
Re: Spawning an object
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
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).
-
- Prole
- Posts: 16
- Joined: Fri Jan 15, 2016 6:33 pm
Re: Spawning an object
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
Like I said, just keep track of all the objects in a list/table.
A very basic solution that doesn't ever remove bombs. You will need to use table.remove to properly remove bombs.
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
-
- Prole
- Posts: 16
- Joined: Fri Jan 15, 2016 6:33 pm
Re: Spawning an object
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
- zorg
- Party member
- Posts: 3468
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Spawning an object
When spawning a bomb, make the bomb's position (x and y coordinates) equal to the position where the plane is.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Who is online
Users browsing this forum: Bing [Bot] and 4 guests