Re: Shooting Multiple Bullets
- Posted by SDPringle Feb 09, 2020
- 839 views
for i = length(bullets) to 1 by -1 do bullets[i][X] += bullet_speed if (bullets[i][X] >= GetScreenWidth()) then bullets = remove(bullets,i) end if DrawCircleV(bullets[i][X],bullets[i][Y],20,red) end for
If you remove from an array (bullets) when you have a for loop iterating over it, you shouldn't try to draw that bullet in the DrawCircleV line:
for i = length(bullets) to 1 by -1 do bullets[i][X] += bullet_speed if (bullets[i][X] >= GetScreenWidth()) then bullets = remove(bullets,i) -- bullets[i] may no longer exist or -- it refers to something that has already been drawn. -- Go to the next iteration now! continue end if DrawCircleV(bullets[i][X],bullets[i][Y],20,red) end for