Hello James/Kent
WOW! First time seeing this thread. I am must have been asleep:)
Anyway, I would really appreciated if you would not mind looking to the code below. It “works” but not sure if is the best way to do what i need. Here the “quick” story.
Here my original post on this issue:
http://developer.anscamobile.com/forum/2012/04/21/how-destroy-enemy-boss-overtime-during-single-collision
I am basically using a global collision listener to destroy on collision some rocks. I just move a gun cross on top of some rock and they get destroyed. That seems to work perfectly (both the gun cross and the rocks use physics. Now I want to add a different type of rock (rock.type = “boss”) which are more difficult to destroy. To keep the code sample, I thought I could use a timer that would time the gun cross being over the rock (colliding with the rock) and if the rock is of Boss type then after say 1 second, the rock will be destroyed. If for some reason the player cannot keep the gun cross over the rock long enough (less than 1 second) then the timer (called here bossTimer) will be canceled in the “ended” phase of the runtime collision. Please note the collision also check for other collision like the ones between rocks and the walls outside the screen.
Like i said it seems to work but I am affraid about doing so much stuff during a collision. I was wondering if you may have a better idea, maybe less complicated idea on to accomplish my primary goal: Delay the destruction of a rock if it’s type is “boss” The rocks are spawned using a timer and the type is randomly set when they spawned toward the screen (they are spwaned from outside the screen and given a velocity toward other objects on the screen.
I am sure i gave way too much details and probably confused the issue!
Please see my current code below (i removed some code to help on the reading of it)
THANKS for doing this by the way (helping people in general I mean)
Mo
(LairdGames)
[lua]
local bossTimer
–
local function destroyRock ( obj)
– GUN CROSS HIT NORMAL ROCK!
if fxOn then audio.play ( explosionSound ) end
score = score + 10*gameLevel
– explosion code here
display.remove(obj)
obj = nil
return true
end
local function destroyBoss ( obj)
– GUN CROSS HIT THE BOSS ROCK!
if fxOn then audio.play ( explosionSound ) end
– explosion code here
score = score + 20*gameLevel
– remove rock display object
display.remove(obj)
obj = nil
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 the gun cross hit a rock (normal or boss) and that the gun is enable
if obj1.name == “gun” and obj2.name == “rock” and gunEnable == true then
– check if the rock hit is of type = boss
if obj2.type == “boss” then
– start BOSS timer
bossTimer = timer.performWithDelay(300,
function()
timer.cancel(bossTimer)
destroyBoss(obj2)
end, 1 )
else
– if just normal rock then simply destroy the rock display object
destroyRock(obj1,obj2)
end
– check if the gun cross hit a rock (normal or boss) and that the gun is enable (in case obj1 is a “rock”)
if obj1.name == “rock” and obj2.name == “gun” and gunEnable == true then
if obj1.type == “boss” then
– start BOSS timer
bossTimer = timer.performWithDelay(300,
function()
timer.cancel(bossTimer)
destroyBoss(obj1)
end, 1 )
else
destroyRock(obj2,obj1)
end
end
elseif phase == “ended” then
if bossTimer then
– cancel the bossTimer if the player moved the gun cross away from the boss rock
timer.cancel(bossTimer)
end
end
end[/lua] [import]uid: 100814 topic_id: 20965 reply_id: 104313[/import]