Hi guys,
I was making good progress in finishing my game but I hit a road block and I hope you can help me destroy that road block to pieces:)
Anyway, I was able to destroy the “boss” if the collision last more than 300 ms but I have noticed that if the “boss” get hit (collision with a movable cross-hair AND hit a planet at the same time)then I get very strange behavior. For instance if the boss get hit, it will explode (after 300 ms) and start a transition that will move the points won in animation where the points text simply goes up and goes away over a one second (alpha =0) A pretty typical way of showing points in many games.
My issue is that if the boss collide with the cross hair (called GUN in my code below) AND the boss hit the planet then the starting location of the animation/transition seems to be 0,0 on the screen. When it works ok (boss only get to collide with the cross-hair…away from a planet) the animation of the points (text) going up start from where the boss location on the screen is at the time of collision.
I am using a timer (bossTimer) to start the countdown when the collision happens between the cross-hair and the boss and then cancel the timer if the collision do not last at least 300 ms (so no explosion/removal of the boss happens it that case)
I am pretty sure it is a timing issue when the boss hit a planet AND at the “same” time collide with the cross-hair. The fact that the animation of the points text start from the 0,0 of the screen tells me that boss is removed (and so nil) before the transition is over I “think”. I tried many things like detecting if the boss object is not nil before accepting a collision with the planet but no luck.
I have been working on this for more than a week and frankly I am out ideas! Maybe one of you with a fresh eyes may detect something in code below that I missed. I am also thinking of using something else than timers to deal with delayed destruction of the boss. The regular rocks do not give any issue even if the regular rock hit a planet AND collide with the cross hair at the time. Probably because the object (rock) get removed right away (with a timer but only 1 ms delay)
Thank you so much for any suggestions. So sorry for this lengthy post.
Mo
-----------------MY CODE (Removed anything not important)-----------------
[lua]local function startExplosion(obj)
if obj ~= nil then
screenMessage = display.newText( “+”…gameLevel*10, 160, 240, myFont, 24 )
screenMessage:setTextColor( 255, 0, 0, 255 )
screenMessage.alpha = 1.0
screenMessage.x = obj.x
screenMessage.y = obj.y
local destroyMessage = function()
if messageTween then transition.cancel(messageTween) end
if screenMessage ~= nil then
screenMessage:removeSelf()
screenMessage = nil
end
end
if obj ~= nil then
messageTween = transition.to( screenMessage, { time=1000, alpha=0, y= gunCross.y -100, onComplete=destroyMessage } )
end
end
end
local function planetHit(obj1, obj2)
shakeCounter = 20
if fxOn then audio.play ( planetSound ) end
---- reduce planet health
if obj2.type == “normal” then
obj1.health = obj1.health - gameLevel*3
else
obj1.health = obj1.health - gameLevel*6
end
if obj1.health <= 0 then obj1.health = 0 end
---- “destroy” the planet/bar if health<=0
if obj1.health <= 0 then
obj1.health = 0
if fxOn then audio.play ( planetSound ) end
---- “remove” planet
if obj1 ~= nil then
display.remove(obj1)
obj1 = nil
end
else
– remove rock (if not nil)
if obj2 ~= nil then
display.remove(obj2)
obj2 = nil
end
end
end
local function destroyRock (obj)
if fxOn then audio.play ( explosionSound ) end
startExplosion(obj)
if obj.type = “normal” then
score = score + 10*gameLevel
else
— the rock is a “boss”
score = score + 10*gameLevel
end
– remove rock (if not nil)
if obj ~= nil then
display.remove(obj)
obj = nil
end
return true
end
local function wallHit(obj)
– remove rock (if not nil)
if obj ~= nil then
display.remove(obj)
obj = nil
end
return true
end
– Global Collision function
local function onRockCollision(event)
local phase = event.phase
if “began” == phase then
local obj1 = event.object1
local obj2 = event.object2
– check if a rock hit a planet
if obj1.name == “planet” and obj2.name == “rock” then
timer.performWithDelay( 1, function() planetHit(obj1,obj2) end, 1 )
elseif obj1.name == “rock” and obj2.name == “planet” and bossTimer == nil then
timer.performWithDelay( 1, function() planetHit(obj2,obj1) end, 1 )
end
– check if the gun cross hit a rock
if obj1.name == “gun” and obj2.name == “rock” then
if obj2.type == “boss” and weaponMode == 1 then
bossTimer = timer.performWithDelay(300, function() destroyRock (obj2) end, 1 )
else
– destroy rock (not a boss) right away.
timer.performWithDelay(1, function() destroyRock(obj2) end, 1 )
end
elseif obj1.name == “rock” and obj2.name == “gun” and gunEnable == true then
if obj1.type == “boss” and weaponMode == 1 then
bossTimer = timer.performWithDelay(300, function() destroyRock (obj1) end, 1 )
else
– destroy rock (not a boss) right away.
timer.performWithDelay(1, function() destroyRock(obj1) end, 1 )
end
end
– check if the a rock hit a wall
if obj1.name == “wall” and obj2.name == “rock” then
timer.performWithDelay(1, function() wallHit(obj2) end, 1 )
elseif obj1.name == “rock” and obj2.name == “wall” then
timer.performWithDelay(1, function() wallHit(obj1) end, 1 )
end
elseif phase == “ended” then
– cancel timer if the collision ended (no destruction of boss)
if bossTimer then
timer.cancel(bossTimer)
return true
end
end
end[/lua]
[import]uid: 100814 topic_id: 25361 reply_id: 106723[/import]