Hello guys,
I am having some issue with collisions. I am using a global collision listener for all my collisions and for the most part everything works great BUT randomly the code below gets confused because “event.object1” (which I try to make it so it always refer to a planet display object) randomly sometimes refer to a “rock” display object.
The problem is that planet.health exist but rock do not have a health member (see code below) because of that I am getting an error when the code tries to access rock.health. Basically event.object1 and event.object2 do not always contains the correct objects.
I am doing all the tests before end to make sure event.object1 always contains planet objects and event.object2 rock objects. That way, i can do things to those objects depending on the type. For instance decreasing the planets health went hit with a rock object.
I am not sure if I am making any sense but i would sure appreciate some pointers and how to use global collision correctly and avoid these type of issues.
Thanks a lot.
Mo
ps: The issue mostly appears (it seems) when something is else is happening in the game (like a pressing a button or moving a joystick…) So maybe there is too much going for the collision to keep track of things…
HERE CODE (CLEANED SO TO MAKE IT EASIER TO READ)
[lua] local function onRockCollision(event)
if ( event.phase == “began” ) then
local obj1 = event.object1
local obj2 = event.object2
– tests to make sure obj1 always hold reference to a “planet” object and obj2 is a “rock” object
if obj1.name == “planet” and obj2.name == “rock” then
obj1 = event.object1
obj2 = event.object2
elseif obj1 == “rock” and obj2 == “planet” then
obj1 = event.object2
obj2 = event.object1
elseif obj1.name == “gun” and obj2.name == “rock” then
obj1 = event.object1
obj2 = event.object2
elseif obj1.name == “rock” and obj2.name == “gun” then
obj1 = event.object2
obj2 = event.object1
end
if (obj1.name == “planet” and obj2.name == “rock” ) or (obj1.name == “rock” and obj2.name == “planet” ) then
– reduce planet health by some %
obj1.health = obj1.health - gameLevel*3 — THIS IS WHERE THE GAME REPORT AN ERROR (BECAUSE OBJ1 REFER TO A ROCK INSTEAD OF A PLANET DISPLAY OBJECT)
– destroy rock display object
obj2:removeSelf()
obj2=nil
return true
– check if hitting the gun cross
elseif ((obj1.name == “rock” and obj2.name == “gun”) or (obj1.name == “gun” and obj2.name == “rock”)) and gunEnable == true and stickMove == true then
– increase score code here
XXXXXXX
– destroy rock display object
obj2:removeSelf()
obj2=nil
end
return true
– end of the function
end[/lua]
[import]uid: 49236 topic_id: 24197 reply_id: 324197[/import]
[import]uid: 52491 topic_id: 24197 reply_id: 97658[/import]