1. Shooting Multiple Bullets
- Posted by Icy_Viking Feb 07, 2020
- 1027 views
Hello all,
I am having a slight problem. I am trying to shoot multiple bullets, but I can only fire one.
atom Width=1024 atom Height=720 constant TRUE=1 constant FALSE=0 InitWindow(Width,Height,"Shump") SetTargetFPS(60) atom black = bytes_to_int({0,0,0,0}) atom red = bytes_to_int({255,0,0,255}) atom bullet_x = GetScreenWidth() / 2 atom bullet_y = GetScreenHeight() / 2 atom bullet_speed = 0.030 integer bullet_visible = FALSE sequence bullets = {} while not WindowShouldClose() do if IsKeyPressed(KEY_SPACE) and bullet_visible = FALSE then bullet_visible = TRUE end if if (bullet_visible) then bullet_x += bullet_speed end if if (bullet_x >= GetScreenWidth()) then bullet_visible = FALSE end if BeginDrawing() ClearBackground(black) if (bullet_visible = TRUE) then DrawCircleV(bullet_x,bullet_y,20,red) end if EndDrawing() end while CloseWindow()
2. Re: Shooting Multiple Bullets
- Posted by Maccuq Feb 08, 2020
- 987 views
atom Width=1024 atom Height=720 constant X = 1, Y = 2 InitWindow(Width,Height,"Shump") SetTargetFPS(60) atom black = bytes_to_int({0,0,0,0}) atom red = bytes_to_int({255,0,0,255}) atom bullet_speed = 0.030 sequence bullets = {} while not WindowShouldClose() do if IsKeyPressed(KEY_SPACE) then atom bullet_x = GetScreenWidth() / 2 atom bullet_y = GetScreenHeight() / 2 bullets = append(bullets, {bullet_x, bullet_y}) end if for i = length(bullets) to 1 by -1 do bullets[i][X] += bullet_speed if (bullets[i][X] >= GetScreenWidth()) then bullets = remove(bullets, i) continue end if BeginDrawing() ClearBackground(black) DrawCircleV(bullets[i][X], bullets[i][Y], 20, red) EndDrawing() end for end while CloseWindow()
3. Re: Shooting Multiple Bullets
- Posted by Icy_Viking Feb 08, 2020
- 933 views
Thank you. However I had to fix a couple of things. Like putting the for loop in between the begin and end drawing functions. Also, when the last bullet of the array is done, there is a subscript value out of bounds error.
Value 1 is out of bounds, reading from a sequence length of 0 in subscript #1 of "bullets"
without warning include std/machine.e include std/get.e include std/convert.e include EuRaylib.ew include flags.e atom Width=1024 atom Height=720 constant TRUE=1 constant FALSE=0 InitWindow(Width,Height,"Shump") SetTargetFPS(60) atom black = bytes_to_int({0,0,0,0}) atom red = bytes_to_int({255,0,0,255}) atom bullet_speed = 5.0 integer bullet_visible = FALSE constant X=1,Y=2 sequence bullets = {} while not WindowShouldClose() do if IsKeyPressed(KEY_SPACE) then atom bullet_x = GetScreenWidth() / 2 atom bullet_y = GetScreenHeight() / 2 bullets = append(bullets,{bullet_x,bullet_y}) end if BeginDrawing() ClearBackground(black) 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 EndDrawing() end while CloseWindow()
4. Re: Shooting Multiple Bullets
- Posted by Maccuq Feb 09, 2020
- 925 views
Thank you. However I had to fix a couple of things. Like putting the for loop in between the begin and end drawing functions.
Yes, sorry i had no way to test my code and it was not the thing you asked about so i overlooked that.
Also, when the last bullet of the array is done, there is a subscript value out of bounds error. Value 1 is out of bounds, reading from a sequence length of 0 in subscript #1 of "bullets"
This statement is not true. Copy my code without changing anything (don't remove continue) except from moving BeginDraw/ClearBackground/EndDraw functions outside for loop :)
5. Re: Shooting Multiple Bullets
- Posted by SDPringle Feb 09, 2020
- 897 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
6. Re: Shooting Multiple Bullets
- Posted by Icy_Viking Feb 09, 2020
- 905 views
My apologies. I overlooked the continue keyword in the for loop. Adding that and it works fine now. Thank you both.