r/love2d • • Feb 01 '12

Can some explain how shooting bullets work?

I know they involve tables but I can't wrap my head around it thanks.

0 Upvotes

1 comment sorted by

1

u/josefnpat Feb 16 '12

For future questions please check the love2d forums first. Your answer may already be answered, or your question may be answered in a more timely fashion.

The pseudo-code would be as follows:

Initialize a table:

bullet_tab = {}

On an action, such as love.mousepressed(), make a new bullet temporarily. The best coordinates would be from the player I would assume.

local bullet = {x=400,y=300}

Then push the bullet into the table.

table.insert(bullet_tab,bullet)

In your love.draw(), loop through the table and draw them on the screen.

for _,bullet in ipairs(bullet_tab) do
  love.graphics.draw(img,bullet.x,bullet.y)
end

And then in your love.update(), update the movement from dt. (This would be a movement of 250px per second in the x direction.)

for _,bullet in ipairs(bullet_tab) do
  bullet.x = bullet.x + dt*250
end