Objects not maintaining their state
Posted: Sun Apr 03, 2011 12:23 pm
I am trying to track the health of each enemy ship. When an enemy ship (health = 2) collides with a player weapon (damage = 1) id like 1 to be subtracted from health and for the second shot to destroy the enemy. Unfortunately, one shot destroys an enemy. Here's how I understand the code. Each Raider object that is created in Waves has it's own health that it inherits from Raider and each Laser object has it's own damage that it inherits from Laser. So, in checkCollision when I check v1.health and v2.damage it should be value specific to each Raider and Laser object.
Here is the relevant code:
Manager
Raider
Laser
FirstLevel
Main
I use this code from PIL for my inheritance:
Here is the full game:
http://www.trevorhrenko.ca/jelaga/Jelaga.love
I'll be happy to provide more information or answer questions if needed.
Here is the relevant code:
Manager
Code: Select all
function Manager:checkCollisions(ship, wm)
for i1,v1 in ipairs(ship) do
for i2,v2 in ipairs(wm) do
if Collision:check(v1.x, v1.y, v1.anim:getWidth(), v1.anim:getHeight(),
v2.x, v2.y, v2.image:getWidth(), v2.image:getHeight()) == true then
if v1.health - v2.damage <= 0 then -- Doesn't work?
v1.alive = false
table.insert(ExplosionManager, v1)
table.remove(ship, i1)
table.remove(wm, i2)
else
v1.health = v1.health - v2.damage
end
end
end
end
end
Code: Select all
Raider.name = "Raider"
Raider.speed = 90
Raider.health = 2
Code: Select all
Laser.name = "Laser"
Laser.speed = 200
Laser.rateOfFire = 1
Laser.damage = 1
Code: Select all
function FirstLevel.load()
FirstLevel.Waves = {
{
Raider:new{x = 0, y = -32, Explode = { anim =
newAnimation(ActionSprite.Explode.image, 32, 32, 0.2, 0)} },
Raider:new{x = 32, y = -32, Explode = { anim =
newAnimation(ActionSprite.Explode.image, 32, 32, 0.2, 0)} }
},
{
Raider:new{x = 0, y = -64, Explode = { anim =
newAnimation(ActionSprite.Explode.image, 32, 32, 0.2, 0)} },
Raider:new{x = 64, y = -64, Explode = { anim =
newAnimation(ActionSprite.Explode.image, 32, 32, 0.2, 0)} }
}
}
end
Code: Select all
-- For each enemy in each wave check shooting conditions and check
-- for collisions with Player weapon fire.
for i=1, #CurrentLevel.Waves do
for j, v in ipairs (CurrentLevel.Waves[i]) do
-- If any x coord of player equals any x coord of an enemy
-- and the enemy is on the screen the enemy shoots and the
-- player is still alive.
if p.alive then
if ( p.x < v.x + v.anim:getWidth() and p.x > v.x ) or
( p.x + p.anim:getWidth() > v.x and p.x + p.anim:getWidth()
< v.x + v.anim:getWidth() ) and v.y > 0 then
v:shoot(dt, timeElapsed)
end
end
end
-- Check if enemy hit by player fire
Manager:checkCollisions(CurrentLevel.Waves[i], PlayerWeaponManager)
end
Code: Select all
function Raider:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
http://www.trevorhrenko.ca/jelaga/Jelaga.love
I'll be happy to provide more information or answer questions if needed.