enemy removal code (UPDATE: caused a hit detection bug)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
GungnirDev
Party member
Posts: 119
Joined: Thu Jun 28, 2012 10:31 pm

enemy removal code (UPDATE: caused a hit detection bug)

Post by GungnirDev »

Thanks to Wojak who helped me adapt the code in order to remove enemies as they go offscreen, however I now have a hit detection bug which I cannot seem to resolve. Enemies now seemingly need to be shot in a certain order which, while that may be OK for some other interesting space shooter would make this one really hard and not fun. Can anyone assist?

Full Download: http://www.2shared.com/file/kgJDormt/Invictus.html
Last edited by GungnirDev on Tue Jan 08, 2013 8:44 pm, edited 3 times in total.
Bullets are the beauty of the blistering sky
Bullets are the beauty and I don't know why
User avatar
GungnirDev
Party member
Posts: 119
Joined: Thu Jun 28, 2012 10:31 pm

Re: simple enemy removal code

Post by GungnirDev »

Guys, I'm really lost here. Does anyone know of a good way to ensure that all enemies are removed when they leave the screen?
Bullets are the beauty of the blistering sky
Bullets are the beauty and I don't know why
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: simple enemy removal code

Post by Boolsheet »

I'll quote myself from another thread.
Generally, removing stuff from a table while you are iterating over it is bad (mostly because it will introduce weird bugs). Lua allows you to change and remove (set to nil), but not add. table.remove modifies the table in a way that it may add things and you get undefined behaviour. On way to solve it is to use a 'for' loop that iterates backwards.

Code: Select all

for i = #bullet, 1, -1 do
    if bullet[i].deleteMe then
        table.remove(bullet, i)
    end
end
Shallow indentations.
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: simple enemy removal code

Post by Wojak »

If you relay need to completely remove the enemy for some reason I recommend iterating using 'pairs' instead of 'ipairs' and removing enemies like this:

Code: Select all

for i,v in pairs(enemies) do

if v.y > 920 then
	enemies[i] = nil
    table.insert(remEnemy, i)
end
    if v.x > 1400 then
	enemies[i] = nil
    table.insert(remEnemy, i)
end
    if v.x < -300 then
	enemies[i] = nil
   table.insert(remEnemy, i)
end 

end 
so you can restore them:

Code: Select all

local random =  math.random(1,#remEnemy)
enemies[remEnemy[random]] = CreateEnemyFunction()
table.remove(remEnemy, random) 
But You don't need to destroy them at all, but assign two new properties to them – visible and alive (If there are on screen – visible = true, else false, if a bullet hit them, alive = false)

then you need the checks:

Code: Select all

function love.update()
	for i,v in pairs(enemies) do
		if v.alive then
			--code
		end
	end
end

function love.draw()
	for i,v in pairs(enemies) do
		if v.alive and v.visible then
			--code
		end
	end
end
Not drawing an image representing an enemy may improve performance up to 10 times, not drawing an image and not performing any operations on an enemy is almost as efficient as removing it from the table
User avatar
GungnirDev
Party member
Posts: 119
Joined: Thu Jun 28, 2012 10:31 pm

Re: simple enemy removal code

Post by GungnirDev »

@ Boolsheet: I'm sorry, I'm...not the best at this, so I'm not really sure what that code is even supposed to do.

@ Wojak: Yeah, I need to remove the enemies, because I need to limit the spawn of a certain enemy type so that it won't spawn if there are a certain number of enemies on the screen. Too many of those enemies quickly swarm the player and are almost unkillable and I don't want that in my game.

I tried the code you and it worked but that removed all enemies, I need to remove just the enemy that is off the screen.
Bullets are the beauty of the blistering sky
Bullets are the beauty and I don't know why
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: simple enemy removal code

Post by Wojak »

There is not enough data to help You more...
I don't know what the numbers 920, 1400 and – 300 are, if the screen can move (camera system), if the enemies should be able to leave the screen and com back, how the spawn system work and so on...

Maybe you should include the love file?
User avatar
GungnirDev
Party member
Posts: 119
Joined: Thu Jun 28, 2012 10:31 pm

Re: simple enemy removal code

Post by GungnirDev »

Ah very well.

http://www.2shared.com/file/mg9yQFl6/Invictus.html

It's so big, so I have to go to 2shared to upload it. Here you are.

I don't really need the enemies to come back, because it's a one-way sidescroller. I just need each enemy to be deleted when I shoot it or when it leaves the screen. I can always spawn more.

Assuming I know what I'm talking about. It's got your code in there right now, so this is what it does in my game. The code is foe.lua.
Bullets are the beauty of the blistering sky
Bullets are the beauty and I don't know why
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: simple enemy removal code

Post by Wojak »

In order to implement my code, Your code needed to be adapted...
- all ipairs used on enemies table needed to be changed to pairs
- remEnemy = {} needed to be moved to the beginning of the file, so you can use it
- spawn code needed to be changed:

Code: Select all

if #remEnemy == 0 then  
	table.insert(enemies, enemy)
else
	local random =  math.random(1,#remEnemy)
	enemies[remEnemy[random]] = enemy
	table.remove(remEnemy, random) 
end  
also you can not use #enemies any more, so you need to count the enemies manually...

Code: Select all

foe = {}
	
	
bullets = {}

remEnemy = {}

	

function trigger()	
	
	spawn = math.random(1,5)
	
end
	

function brigger()
	
	spawnboss = math.random(1,6)
end


function myswerve()
	
	swerve = math.random(5)

end
	

dofire = 0
	

function firing()
	
	dofire = 1

end

	
bosshealth = 100


function foe.load()


	cron.every (1, trigger)


	cron.every (3, brigger)
	cron.every (4, myswerve)
	

cron.every (3, firing)

	
	swerve = 3
	
	
	spawn = 0
	
	
	spawnboss = 0
	
	
	respawn = 0
	
	
	reinforce = 0
	
	
	shootn = 0
		
	
	wolv = love.graphics.newImage('shmuppics/wolverine.png')
	
	bdgr = love.graphics.newImage('shmuppics/honeybadger.png')
	
	hover = love.graphics.newImage('shmuppics/hoverguard.png')
	torpido = love.graphics.newImage('shmuppics/bullet_1.png')
	bossgun_1 = love.graphics.newImage('shmuppics/bossgun_1.png')
	bossgun_2 = love.graphics.newImage('shmuppics/bossgun_3.png')
	bossgun_3 = love.graphics.newImage('shmuppics/bossgun_3.png')
	lazor = love.graphics.newImage('shmuppics/lazor.png')
	cluster = love.graphics.newImage('shmuppics/cluster.png')
	volley = love.graphics.newImage('shmuppics/volley.png')
	volley2 = love.graphics.newImage('shmuppics/volley.png')
	blast = love.graphics.newImage('shmuppics/explosion.png')  
	scrap1 = love.graphics.newImage('shmuppics/scrap1.png')
	scrap2 = love.graphics.newImage('shmuppics/scrap2.png')
	scrap3 = love.graphics.newImage('shmuppics/scrap3.png')
	scrap4 = love.graphics.newImage('shmuppics/scrap4.png')
	darkship_up = love.graphics.newImage('shmuppics/darkship_up.png')
	darkship_down = love.graphics.newImage('shmuppics/darkship_down.png')
	darkship_left = love.graphics.newImage('shmuppics/darkship_left.png')
	darkship_right = love.graphics.newImage('shmuppics/darkship_right.png')
	darkship_left_super = love.graphics.newImage('shmuppics/darkship_left_super.png')
	darkship_right_super = love.graphics.newImage('shmuppics/darkship_right_super.png')
	bosshp = love.graphics.newImage('shmuppics/bosshp.png')
	redship = love.graphics.newImage('shmuppics/redship.png')
	blueship = love.graphics.newImage('shmuppics/blueship.png')
	warlord = love.graphics.newImage('shmuppics/warlord.png')
	swarlord = love.graphics.newImage('shmuppics/swarlord.png')
	null = love.graphics.newImage('shmuppics/null.png')
	
	glowlord = newAnimation(warlord, 136, 131, 0.2, 0)
	glowlord:setMode("bounce")
		
	enemies = {}
	
	bosses = {}
	

for i=0,2 do
    enemy = {}
    enemy.img = wolv
    enemy.width = 50
    enemy.height = 20
    enemy.x = i * (enemy.width + 100) + 100
    enemy.y = i * (enemy.height) - 400
    table.insert(enemies, enemy)
	end
	

	
end

function foe.update(dt)

glowlord:update(dt)

if bosshealth < 1 then
bosshealth = 0
end

for _, bullet in ipairs(bullets) do
	if CheckCollision(player.x, player.y, player.width, player.height, bullet.x, bullet.y, bullet.width, bullet.height) then
	playerhealth = playerhealth - 5
	end
end

shootn = shootn + 1
if shootn > 50 then
shootn = 0
end

for i,v in ipairs(bullets) do
if v.img == torpido then
v.y = v.y + 700 * dt
elseif v.img == bossgun_1 then
v.y = v.y + 1000 * dt
elseif v.img == bossgun_2 then
v.x = v.x - 800 * dt
elseif v.img == bossgun_3 then
v.x = v.x + 800 * dt
elseif v.img == lazor then
v.y = v.y + 1000 * dt
elseif v.img == cluster then 
v.y = v.y - 600 * dt
elseif v.img == volley then
v.x = v.x - 700 * dt
elseif v.img == volley2 then
v.x = v.x + 700 * dt
end
end

remBullet = {}

for i,v in pairs(enemies) do
if v.img == hover then
if v.aim == shootn then
	bullet = {}
	bullet.img = torpido
	bullet.x = v.x + 70
	bullet.y = v.y + 50
	bullet.width = 10
	bullet.height = 10
	table.insert(bullets, bullet) -- reset
		end
	end
	
if v.img == blueship then
if v.aim == shootn then
	bullet = {}
	bullet.img = lazor
	bullet.x = v.x + 90
	bullet.y = v.y + 50
	bullet.width = 20
	bullet.height = 60
	table.insert(bullets, bullet) -- reset
		end
	end
	
if v.img == redship then
if v.aim == shootn then

	bullet = {}
	bullet.img = cluster
	bullet.x = v.x + 90
	bullet.y = v.y + 50
	bullet.width = 1
	bullet.height = 1
	table.insert(bullets, bullet) -- reset
	
	bullet = {}
	bullet.img = cluster
	bullet.x = v.x + 25
	bullet.y = v.y + 50
	bullet.width = 1
	bullet.height = 1
	table.insert(bullets, bullet)
	
	bullet = {}
	bullet.img = cluster
	bullet.x = v.x + 50
	bullet.y = v.y + 70
	bullet.width = 1
	bullet.height = 1
	table.insert(bullets, bullet)
	
	bullet = {}
	bullet.img = cluster
	bullet.x = v.x + 75
	bullet.y = v.y + 70
	bullet.width = 1
	bullet.height = 1
	table.insert(bullets, bullet)
	
	bullet = {}
	bullet.img = cluster
	bullet.x = v.x + 100
	bullet.y = v.y + 50
	bullet.width = 1
	bullet.height = 1
	table.insert(bullets, bullet)
	
		end
	end
	
if v.img == swarlord then
if v.aim == shootn then
	
	bullet = {}
	bullet.img = volley
	bullet.x = v.x + 90
	bullet.y = v.y + 30
	bullet.width = 20
	bullet.height = 60
	table.insert(bullets, bullet) -- reset
	
	bullet = {}
	bullet.img = volley
	bullet.x = v.x + 90
	bullet.y = v.y + 70
	bullet.width = 20
	bullet.height = 60
	table.insert(bullets, bullet) -- reset
	
	bullet = {}
	bullet.img = volley2
	bullet.x = v.x + 90
	bullet.y = v.y + 30
	bullet.width = 20
	bullet.height = 60
	table.insert(bullets, bullet) -- reset
	
	bullet = {}
	bullet.img = volley2
	bullet.x = v.x + 90
	bullet.y = v.y + 70
	bullet.width = 20
	bullet.height = 60
	table.insert(bullets, bullet) -- reset
	
		end
	end
	
end

for i,v in ipairs(bosses) do
if v.img == darkship_down then
if v.aim == shootn then
	bullet = {}
	bullet.img = bossgun_1
	bullet.x = v.x + 70
	bullet.y = v.y + 50
	bullet.width = 10
	bullet.height = 10
	table.insert(bullets, bullet) -- reset
	end
end
	
if v.img == darkship_left then
if v.aim == shootn then
	bullet = {}
	bullet.img = bossgun_3
	bullet.x = v.x + 70
	bullet.y = v.y + 70
	bullet.width = 50
	bullet.height = 50
	table.insert(bullets, bullet) -- reset
	end
end
	
if v.img == darkship_right then
if v.aim == shootn then
	bullet = {}
	bullet.img = bossgun_2
	bullet.x = v.x + 70
	bullet.y = v.y + 70
	bullet.width = 50
	bullet.height = 50
	table.insert(bullets, bullet) -- reset
		end
	end
end

if stage == 1 then
	
	if spawn == 1 then
	for i=0,4 do
	enemy = {}
	enemy.img = wolv
	enemy.width = 70
	enemy.height = 20
	enemy.x = i * (enemy.width + 100) + 100
	enemy.y = i * (enemy.height) - 400
	enemy.aim = 100
	enemy.timer = 0
	table.insert(enemies, enemy)
	spawn = 0
	end
end
	
	if spawn == 2 then
	for i=0,5 do
	enemy = {}
	enemy.img = wolv
	enemy.width = 70
	enemy.height = 20
	enemy.x = i * (enemy.width + 200) + 200
	enemy.y = i * (enemy.height) - 400
	enemy.aim = 100
	enemy.timer = 0
	table.insert(enemies, enemy)
	spawn = 0
	end
end

	if spawn == 3 then
	for i=0,3 do
	enemy = {}
	enemy.img = wolv
	enemy.width = 70
	enemy.height = 20
	enemy.x = i * (enemy.width + 150) + 100
	enemy.y = i * (enemy.height) - 400
	enemy.aim = 100
	enemy.timer = 0
	table.insert(enemies, enemy)
	spawn = 0
	end
end	

	if spawn == 4 then
	for i=0,1 do
	enemy = {}
	enemy.img = hover
    enemy.width = 70
    enemy.height = 20
    enemy.x = i * (enemy.width + 100) - 300
    enemy.y = i * (enemy.height - 70) + 100
	enemy.aim = math.random( 1, 49 )
	enemy.timer = 0
    table.insert(enemies, enemy)
    spawn = 0
    
    end
end
    
    if spawn == 5 then
    for i=0,1 do
    enemy = {}
    enemy.img = bdgr
    enemy.width = 70
    enemy.height = 20
    enemy.x = i * (enemy.width) + 800
    enemy.y = i * (enemy.height) - 200
    enemy.aim =  100
    enemy.timer = 0
    table.insert(enemies, enemy)
    spawn = 0
		end    
	end
end

if stage == 2 then

end

if stage == 3 then

end

if stage == 4 then
	
	if spawnboss == 1 then
for i=0,0 do
	boss = {}
	boss.img = darkship_right
	boss.width = 80
    boss.height = 60
    boss.x = i * (enemy.width) + 1200
    boss.y = i * (enemy.height) + 400 + 50
    boss.aim = math.random( 1, 69 )
    table.insert(bosses, boss)
    spawnboss = 0
	end
end

if spawnboss == 2 then
for i=0,0 do
	boss = {}
	boss.img = darkship_left
	boss.width = 80
    boss.height = 60
    boss.x = i * (enemy.width) - 200
    boss.y = i * (enemy.height) + 400 + 50
    boss.aim = math.random( 1, 69 )
    table.insert(bosses, boss)
    spawnboss = 0
	end
end

if spawnboss == 3 then
for i=0,0 do
	boss = {}
	boss.img = darkship_right_super
	boss.width = 100
    boss.height = 220
    boss.x = i * (enemy.width) + 1200
    boss.y = i * (enemy.height) + swerve * 150
    table.insert(bosses, boss)
    spawnboss = 0
	end
end

if spawnboss == 4 then
for i=0,0 do
	boss = {}
	boss.img = darkship_left_super
	boss.width = 100
    boss.height = 220
    boss.x = i * (enemy.width) - 200
    boss.y = i * (enemy.height) + swerve * 150
    table.insert(bosses, boss)
    spawnboss = 0
	end
end

if spawnboss == 5 then
for i=0,0 do
	boss = {}
	boss.img = darkship_up
	boss.width = 80
    boss.height = 60
    boss.x = i * (enemy.width) + 500
    boss.y = i * (enemy.height) + 1000
    table.insert(bosses, boss)
    spawnboss = 0
	end
end

if spawnboss == 6 then
for i=0,0 do
	boss = {}
	boss.img = darkship_down
	boss.width = 80
    boss.height = 60
    boss.x = i * (enemy.width) + 500
    boss.y = i * (enemy.height) - 200
    boss.aim = math.random( 1, 49 )
    table.insert(bosses, boss)
    spawnboss = 0
	end
end
	
	if spawn == 1 then
	for i=0,2 do
	enemy = {}
	enemy.img = scrap1
	enemy.width = 80
	enemy.height = 30
	enemy.x = i * (enemy.width) + 300 * swerve
    enemy.y = i * (enemy.height + 200) - 500
    enemy.aim =  100
    enemy.timer = 0
    table.insert(enemies, enemy)
    spawn = 0
    swerve = math.random(5)
	end
end

	if spawn == 2 then
	for i=0,1 do
	
	enemy = {}
	
	enemy.img = scrap2
	
	enemy.width = 80
	
	enemy.height = 30
	
	enemy.x = i * (enemy.width) + swerve  + 250 * swerve
    
	enemy.y = i * (enemy.height + 300) - 500
    
	enemy.aim =  100
    enemy.timer = 0
 
	if #remEnemy == 0 then  
		table.insert(enemies, enemy)
 
	else
		local random =  math.random(1,#remEnemy)
		enemies[remEnemy[random]] = enemy
		table.remove(remEnemy, random) 
	end   
	spawn = 0
    
	swerve = math.random(4)	
    end
end

	if spawn == 3 then
	for i=0,0 do
	enemy = {}
	enemy.img = scrap3
	enemy.width = 80
	enemy.height = 30
	enemy.x = i * (enemy.width) + 100 * swerve
    enemy.y = i * (enemy.height + 300) - 300
    enemy.aim =  100
    enemy.timer = 0
    
	if #remEnemy == 0 then  
		table.insert(enemies, enemy)
 
	else
		local random =  math.random(1,#remEnemy)
		enemies[remEnemy[random]] = enemy
		table.remove(remEnemy, random) 
	end       
	spawn = 0
    swerve = math.random (3,5)
	end
end

	if spawn == 4 then
	for i=0,0 do
	enemy = {}
	enemy.img = scrap4
	enemy.width = 80
	enemy.height = 30
	enemy.x = i * (enemy.width + 100 * swerve) + 600
    enemy.y = i * (enemy.height + 200) - 300
    enemy.aim =  100
    enemy.timer = 0
    
	if #remEnemy == 0 then  
		table.insert(enemies, enemy)
 
	else
		local random =  math.random(1,#remEnemy)
		enemies[remEnemy[random]] = enemy
		table.remove(remEnemy, random) 
	end   
    spawn = 0
    swerve = math.random (2,4)
    	end
	end
end

if stage == 5 then

if spawn == 1 and # enemies < 3 then
	for i=0,0 do
	enemy = {}
	enemy.img = blueship
	enemy.width = 80
	enemy.height = 50
	enemy.x = i * (enemy.width + 100) - 100
	enemy.y = i
	enemy.aim = math.random(30,49)
	enemy.timer = 0
	if #remEnemy == 0 then  
		table.insert(enemies, enemy)
 
	else
		local random =  math.random(1,#remEnemy)
		enemies[remEnemy[random]] = enemy
		table.remove(remEnemy, random) 
	end   
	spawn = 0
	end
end

if spawn == 2 then
	for i=0,0 do
	enemy = {}
	enemy.img = redship
	enemy.width = 80
	enemy.height = 50
	enemy.x = i * (enemy.width + 100) + 600
	enemy.y = i - 100
	enemy.aim = math.random(30,49)
	enemy.timer = 0
	
	if #remEnemy == 0 then  
		table.insert(enemies, enemy)
 
	else
		local random =  math.random(1,#remEnemy)
		enemies[remEnemy[random]] = enemy
		table.remove(remEnemy, random) 
	end   
	spawn = 0
	end
end


if spawn == 3 then
	
	for i=0,3 do
	
		enemy = {}
	
		enemy.img = wolv
	
		enemy.width = 70
	
		enemy.height = 20
	
		enemy.x = i * (enemy.width + 150) + 100
	enemy.y = i * (enemy.height) - 400
	
		enemy.aim = 100
	enemy.timer = 0
	
		if #remEnemy == 0 then  
		table.insert(enemies, enemy)
 
	else
		local random =  math.random(1,#remEnemy)
		enemies[remEnemy[random]] = enemy
		table.remove(remEnemy, random) 
	end   	
		spawn = 0
	
	end

end	


if spawn == 4 then
	
	for i=0,0 do
	
		enemy = {}
	
		enemy.img = swarlord
	
		enemy.width = 80
	
		enemy.height = 50
	
		enemy.x = i * (enemy.width + 100) + 600
	enemy.y = i - 100
	
		enemy.aim = math.random(30,50)
	enemy.timer = 0
	
		if #remEnemy == 0 then  
		table.insert(enemies, enemy)
 
	else
		local random =  math.random(1,#remEnemy)
		enemies[remEnemy[random]] = enemy
		table.remove(remEnemy, random) 
	end   
		spawn = 0
	
	end

end




end



remBoss = {}


if gamestate == "playing" or "winstage" then


	for i, v in ipairs(bullets) do
	
		if v.y > 920 then
	
			table.insert(remBullet, i)
	
		end

end

	
for i, v in ipairs(remBullet) do
	
	table.remove(bullet, i)

end


for i,v in pairs(enemies) do
	
	if v.img == wolv then
    
		v.y = v.y + dt * 300
    
		v.x = v.x + math.cos(game.clock) * swerve
    
	elseif v.img == hover then
    
		v.x = v.x + dt * 300
    
	elseif v.img == bdgr then
    
		v.x = v.x - dt * 500
    
		v.y = v.y + dt * 500
    
	elseif v.img == torpido then
    
		v.y = v.y + dt * 800
    
	elseif v.img == scrap1 then
    
		v.x = v.x - dt * 15
    
		v.y = v.y + dt * 100
    
	elseif v.img == scrap2 then
    
		v.x = v.x + dt * 5
    
		v.y = v.y + dt * 250
    
	elseif v.img == scrap3 then
    
		v.x = v.x + dt * 30
    
		v.y = v.y + dt * 30
    
	elseif v.img == scrap4 then
    
		v.x = v.x - dt * 50
    
		v.x = v.x + dt * 10
    
	elseif v.img == blueship then
    
		if v.x > player.x then
    
			v.x = v.x - dt * 30 * swerve
    
		elseif v.x < player.x then
    
			v.x = v.x + dt * 30 * swerve
    
		end
    
	elseif v.img == redship then
    
		if v.x > player.x then
    
			v.y = v.y + dt * 500
    
			v.x = v.x + dt * 100
    
	elseif v.x < player.x then
    
			v.y = v.y + dt * 400
    
			v.x = v.x - dt * 100
    
	end
    elseif v.img == swarlord then
    
			v.y = v.y + dt * 500
	
	end

end


for i,v in pairs(enemies) do


	--[[if v.y > 920 then
   
		enemies[i] = nil
    
		table.insert(remEnemy, i)

	end
    
	if v.x > 1400 then
   
		enemies[i] = nil
    
		table.insert(remEnemy, i)

	end
    
	if v.x < -300 then
   
		enemies[i] = nil
   
		table.insert(remEnemy, i)
	
end ]]

	if v.y > love.graphics.getHeight()
 - 200 then
 -- just a test, You may use the code above  
		enemies[i] = nil
    
		table.insert(remEnemy, i)

	end
  
	

end



	
for i,v in ipairs(bosses) do
	if v.img == darkship_right then
	v.y = v.y + math.cos(game.clock) * swerve
    v.x = v.x - dt * 500
    elseif v.img == darkship_left then
	v.y = v.y + math.cos(game.clock) * swerve
    v.x = v.x + dt * 500
	elseif v.img == darkship_left_super then
	v.x = v.x + dt * 600
	elseif v.img == darkship_right_super then
	v.x = v.x - dt * 600
	elseif v.img == darkship_up then
	v.y = v.y - dt * 500
	v.x = v.x + math.cos(game.clock) * swerve
	elseif v.img == darkship_down then
	v.y = v.y + dt * 500
	end
	
	if v.y < 400 then
	table.insert(remBoss, i)
end
	if v.y > 920 then
    table.insert(remBoss, i)
end
    if v.x > 1400 then
    table.insert(remBoss, i)
end
    if v.x < -300 then
	table.insert(remBoss, i)
end

	for i, v in ipairs(remBoss) do
	table.remove(boss, v)
end
			end	
		end
	end

function foe.draw()

love.graphics.setColor(255,100,100,200)
love.graphics.rectangle("fill", 980, 110, 20, bosshealth * 6)	

love.graphics.setColor(255,255,255,255)

love.graphics.draw(bosshp, 973, 102)


if gamestate == 'playing' then
	for i,v in ipairs(bullets) do
	love.graphics.draw (v.img, v.x, v.y)
	end
end
	

	for i,v in pairs(enemies) do
    love.graphics.draw (v.img, v.x, v.y)
	end
	
if gamestate == 'playing' then
	for i,v in ipairs(bosses) do
	love.graphics.draw (v.img, v.x, v.y)
	end
end
	
end
User avatar
GungnirDev
Party member
Posts: 119
Joined: Thu Jun 28, 2012 10:31 pm

Re: simple enemy removal code

Post by GungnirDev »

The code works great for what I needed and I do much appreciate it, but now I have an error in hit detection, usually with the grouped enemy waves. It's intermittent, though.
Bullets are the beauty of the blistering sky
Bullets are the beauty and I don't know why
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: simple enemy removal code

Post by Wojak »

You are using the enemies table in other files (ship.lua), so you will need to adapt it as well, I would also recommend do move bullets to this system

functions like ipairs, table.insert, table.remove #table only work with arrays

an array is a table indexed by integers starting from 1 and with interval of 1 between the indexes, so:

Code: Select all

tab = {[1]='a',[2]='b',[3]='c',[4]='d'} -- this is an array
for i,v in ipairs(tab) do --ipairs can access all indexes
  print(i..'='..v)
end
--results: 
--1=a
--2=b
--3=c
--4=d

tab = {[1]='a',[2]='b',[5]='c',[6]='d'} -- this is NOT an array
for i,v in ipairs(tab) do -- ipairs can not access all indexes
  print(i..'='..v)
end
--results: 
--1=a
--2=b


tab = {[1]='a',[2]='b',[5]='c',[6]='d'} -- this is NOT an array
for i,v in pairs(tab) do -- pairs can access all indexes
  print(i..'='..v)
end
--results (may be in random order, but it is not important): 
--1=a
--2=b
--5=c
--6=d
but IMO ipairs is still useless because if you have an array you can do this:

Code: Select all

tab = {[1]='a',[2]='b',[3]='c',[4]='d'} -- this is an array
for i=1, #tab do --no ipairs here
  print(i..'='..tab[i])
end
--results: 
--1=a
--2=b
--3=c
--4=d

also I noticed a strange lack “local” keyword in your script... You may want to reed the lua manual to check this out...

and when I was admiring the art folder I noticed that some images are identical... (love allows you to rotate and “mirror flip” the images using parameters in love.graphic.draw function ;))


I relay want to say that the game in the current state is pretty awesome, I can wait to see the final version :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 10 guests