Log In  

Cart #52509 | 2018-05-07 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

I've submitted this cartridge to the WIP section because it is not finished, it does not have sound and it starts lagging like crazy after a minute. I would appreciate it if someone could tell me a way to store a table with the bullets and remove them once they are no longer visible. I have tried using the DEL() function, but it just changes a value into nil, which bugs the code.

How are the bullets stored in a table?

This is pretty much the way they are stored:

if btn(4) then
 -- storing the X so it stays there
 add(bullets, player_x)
 -- storing the Y, so it can be changed
 add(bullets, 100)
end

And this is how the table is read:

for i=1, #bullets do
 if i % 2 == 0 then

  -- change the y so that they go up
  bullets[i-1] -= 2

 end
end

So I really need help finding a better way to store them and removing the unnecessary bullets.

P#52510 2018-05-07 16:02 ( Edited 2018-05-07 20:41)

That's an interesting take on Pong.

P#52512 2018-05-07 16:37 ( Edited 2018-05-07 20:37)

You should store tables (e.g. struct or classes in other languages) and update attributes.
(never checked, but not sure you can remove values - as opposed to references - from a lua array)

In your case, something like:

if btn(4) then
 -- storing the X so it stays there
 -- storing the Y, so it can be changed
 add(bullets, {x=player_x, y=100})
end
...
for i=1, #bullets do
 local b=bullets[i]
 if b.y<200 then
  b.y-=2
 else
  del(bullets,b)
 end
end
P#52513 2018-05-07 16:41 ( Edited 2018-05-07 20:42)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 18:30:41 | 0.020s | Q:17