Spawning an object

General discussion about LÖVE, Lua, game development, puns, and unicorns.
CalebAnder
Prole
Posts: 16
Joined: Fri Jan 15, 2016 6:33 pm

Spawning an object

Post 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. :awesome:
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Spawning an object

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Spawning an object

Post by bobbyjones »

Well we do have a physics module but most are scared of it.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Spawning an object

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
CalebAnder
Prole
Posts: 16
Joined: Fri Jan 15, 2016 6:33 pm

Re: Spawning an object

Post 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.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Spawning an object

Post 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).

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
CalebAnder
Prole
Posts: 16
Joined: Fri Jan 15, 2016 6:33 pm

Re: Spawning an object

Post 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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Spawning an object

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
CalebAnder
Prole
Posts: 16
Joined: Fri Jan 15, 2016 6:33 pm

Re: Spawning an object

Post 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 :awesome:
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Spawning an object

Post by zorg »

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 :3True 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests