bug removing items from a table

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
TitioLasanha
Prole
Posts: 1
Joined: Tue Mar 09, 2021 8:58 pm

bug removing items from a table

Post by TitioLasanha »

I have been working on a top-down shoter game and I have a gun that shoots bullets. the problem here is that i want them to disappear when they hit a wall, they disappear but end up removing other bullets that are not hitting the wall. I would like to know how to fix this bug (i'm brazilian so the name of some variables can be in portuguese)

Code: Select all

function bullet_load()
--=======parametros balas========
	bulletSpeed = 500
	bulletCadencia=0.2
	bullets = {}
	bulletDx=0
    bulletDy=0
    tiro = love.graphics.newImage("tiro.png")
--=======parametros arma========
    arma_angle=0
    arma_positionX=16
    arma_positionY=3
    pistola = love.graphics.newImage("pistola.png")
    canShoot = true
    dog.shoot=false
    
    local startX=0
    local startY=0
end



function bullet_update(dt)
--=====spaw balas======
   if dog.direcao == "frente" or dog.direcao == "esquerda" then
	   startX = dog.x+(bulletDx*17)+arma_positionX 
   else
	   startX = dog.x+(bulletDx*17)+(arma_positionX-arma_positionX*2)
   end
   startY = dog.y+(bulletDy*17)-arma_positionY
--=====controles======
   if joy.pressed[2] then
        bulletDx, bulletDy= getRadianosAngle(joy.angle[2])
        arma_angle=joy.angle[2]
        if canShoot then
			table.insert(bullets, {x = startX, y = startY, dx = bulletDx, dy = bulletDy, height = 8, width = 8})--valores obtidos em controle_virtual.lua
			canShoot=false
	        tick.delay(function() canShoot = true end, bulletCadencia)
        end
		dog.shoot=true
		if math.cos(joy.angle[2]) >= 0 then
		    armaDirecaoY= 0.8
	   else
	        armaDirecaoY= -0.8
	   end
   elseif joy.pressed[1] and joy.pressed[2]==false then
        bulletDx, bulletDy= getRadianosAngle(joy.angle[1])
        arma_angle=joy.angle[1]
        dog.shoot=false
        if math.cos(joy.angle[1]) >= 0 then
		    armaDirecaoY= 0.8
	   else
	        armaDirecaoY= -0.8
	   end
   end
--=======motor movimento
	for i,v in ipairs(bullets) do
		v.x = v.x + (v.dx * bulletSpeed* dt)
		v.y = v.y + (v.dy * bulletSpeed* dt)
	end
	
	for i,v in ipairs(bullets) do
		for k,row in ipairs(tilemap) do
	        for j,tile in ipairs(row) do
	            if tile ~= 0 then
		            if checkSquareCollision(j * tile_tamanho, k * tile_tamanho, tile_tamanho,v.x-8 ,v.y-8 ,16) then
		                table.remove(bullets,i)
		            end
	            end 
	        end
	    end
	end
end





function bullet_draw()
--=====draw bullets==========
	for i,v in ipairs(bullets) do
		love.graphics.draw(tiro, v.x, v.y,0,0.8,0.8,10,10)
	end
--=====draw arma========
    if dog.direcao == "frente" or dog.direcao == "esquerda" then
	    love.graphics.draw(pistola,dog.x+arma_positionX ,dog.y-arma_positionY,arma_angle,0.8,armaDirecaoY,8,16)
	    love.graphics.draw(dog.spr,dog.mao,dog.x+16,dog.y-6,0,0.8,0.8,4,4)
	else
	    love.graphics.draw(pistola,dog.x+arma_positionX-arma_positionX*2 ,dog.y-arma_positionY,arma_angle,0.8,armaDirecaoY,8,16)
	    love.graphics.draw(dog.spr,dog.mao,dog.x-16,dog.y-6,0,0.8,0.8,4,4)
	end
end

Code: Select all

function map_load()                                                                                                                                                                                                                                                
    tilemap = {
    	{9, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 9},
        {4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6},
        {4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6},
        {4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 6},
        {4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6},
        {4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6},
        {4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6},
        {4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6},
        {9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9},
        {9 ,5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 9}
    }
   tile_tamanho=30
   chao_ferro=love.graphics.newImage("chao_ferro.png")
   parede_1=love.graphics.newImage("parede_1.png")
   parede_2=love.graphics.newImage("parede_2.png")
   parede_3=love.graphics.newImage("parede_3.png")
   parede_4=love.graphics.newImage("parede_3_5n.png")
   
 
    
    for i,row in ipairs(tilemap) do
        for j,tile in ipairs(row) do
            if tile ~= 0 then
                parede = world:newRectangleCollider(j * tile_tamanho, i * tile_tamanho, tile_tamanho, tile_tamanho)
                parede:setType("static")
            end 
        end
    end
end






function checkWallCollision(x,y,t)
	for i,row in ipairs(tilemap) do
        for j,tile in ipairs(row) do
            if tile ~= 0 then
                if checkSquareCollision(j * tile_tamanho, i * tile_tamanho, tile_tamanho,x ,y ,t) then
                     return true
                else
                     return false
                end
            end 
        end
    end
end





function map_update(dt)
end






function map_draw()
--#######posicionamento de tiles no cenario#########
--============chao============
for i,row in ipairs(tilemap) do
        for j,tile in ipairs(row) do
            if tile == 0 then
                love.graphics.draw(chao_ferro, j * tile_tamanho, i * tile_tamanho)
            end 
        end
    end
--============parede==============
for i,row in ipairs(tilemap) do
        for j,tile in ipairs(row) do
            if tile == 1 then
                  love.graphics.draw(parede_1, j * tile_tamanho, i * tile_tamanho)
            end 
        end
    end

for i,row in ipairs(tilemap) do
        for j,tile in ipairs(row) do
            if tile == 2 then
                  love.graphics.draw(parede_2, j * tile_tamanho, i * tile_tamanho)
            end 
        end
    end

for i,row in ipairs(tilemap) do
        for j,tile in ipairs(row) do
            if tile == 3 then
                  love.graphics.draw(parede_3, j * tile_tamanho, i * tile_tamanho)
            end 
        end
    end

for i,row in ipairs(tilemap) do
        for j,tile in ipairs(row) do
            if tile == 4 then
                  love.graphics.draw(parede_3, j * tile_tamanho, i * tile_tamanho,-1.57,1,1,30,0)
            end 
        end
    end

for i,row in ipairs(tilemap) do
        for j,tile in ipairs(row) do
            if tile == 6 then
                  love.graphics.draw(parede_3, j * tile_tamanho, i * tile_tamanho,1.57,1,1,0,30)
            end 
        end
    end
--==================================================
end
function parede_frente()
for i,row in ipairs(tilemap) do
        for j,tile in ipairs(row) do
            if tile == 5 then
                  love.graphics.draw(parede_4, j * tile_tamanho, i * tile_tamanho,3.14,1,1,30,34)
            end 
        end
    end
end
Last edited by TitioLasanha on Sat Mar 13, 2021 4:07 pm, edited 1 time in total.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: bug removing items from a table

Post by pgimeno »

Edit: Please try to post the attachment in the next few posts. I believe that you can't link or attach files unless you have more posts.
Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests