Dear All,
I would like to have your feedback / support in the following case. Hope that i could get your feedback that would help me in solving this issue.
I have 5 objects collide together in different conditions as follow:-
1- SpaceShip (1): this object fires a “bullet” object(2)
2- Nemesis Object (3): also this object fires a “NemesisBullet”(4) to shoot the spaceShip object
3- “Bounce” object (5) this object will be generated randomly to hit the spaceShip object
i have done the following code for onCollision and would like you to take a look and give me your feedback in order to optimize the code
[blockcode]
function onCollision( self, event )
local otherObj = event.other
local isSpaceShipDestroyed = false
if ( self.objectName ~= nil ) and (event.other.objectName ~= nil) then
–print(" Self : "…self.objectName … " & Other: "…event.other.objectName)
– Self : spaceShip & Other: nemesisBullet
– Self : flyingObject & Other: bullet
---------------------------------- Bullet Collision ------------------------------------
if (otherObj.objectName == “bullet”) then
cpointerX = self.x;cpointerY = self.y;local showScore = true
if (self.objectName == “nemesisBullet”) then
levelCounter = levelCounter + bulletScore
elseif (self.objectName == “bounce”) then
local showObjScore = DisplayCounter(self);
local clearBounce = clearBounces(self)
local showExplosion = playNemesisAnimation(cpointerX,cpointerY,“bo”)
totalKilledObjects = totalKilledObjects + 1
levelCounter = levelCounter + bounceScore
elseif (self.objectName == “flyingObject” ) then
totalKilled = totalKilled + 1
totalKilledObjects = totalKilledObjects + 1
local showExplosion = playNemesisAnimation(cpointerX,cpointerY,“ne”)
local showObjScore = DisplayCounter(self)
levelCounter = levelCounter + nemesisScore
end
-------------------------remove both objects collide--------------------------
otherObj.parent:remove( otherObj ) – remove object from hierarchy
localGroup.otherObj = nil – remove object as a property of LocalGroup
self.parent:remove( self )
localGroup.self = nil
end
--------------------- Spaceship Collision ----------------------------------------
if ( self.objectName == “spaceShip” ) then
ipointerX = self.x;ipointerY = self.y;local showScore = true
if ( otherObj.objectName == “flyingObject” ) then
totalKilled = totalKilled + 1
totalKilledObjects = totalKilledObjects + 1
levelCounter = levelCounter + nemesisScore
elseif ( otherObj.objectName == “bounce”) then
local removeObject = clearThisObject(bounces, otherObj)
totalKilledObjects = totalKilledObjects + 1
levelCounter = levelCounter + bounceScore
elseif ( otherObj.objectName == “nemesisBullet”) then
levelCounter = levelCounter + bulletScore
end
otherObj.parent:remove( otherObj ) – remove object from hierarchy
localGroup.otherObj = nil – remove object as a property of a Group
local showObjectScore = DisplayCounter(otherObj)
spaceShip:removeEventListener(“tap”, LaunchSpaceShipBullet)
self.parent:remove( self ) – remove object from hierarchy
localGroup.self = nil – remove object as a property of a Group
pasueTimers = true – Pause Active Timers
local destroySpaceShip = destroySpaceShip()-- Reset Scens
end
if (totalKilled == gameTotalObjects ) then – Completed Level
local clearBulets = clearActiveBullets()
local levelCompleted = completeGameLevel() – Move to Next Level
end
------------------ Update Game Score and Killed Nemesis Counter ------------------
scoreObject.text = "Score: "…levelCounter
nemesisObject.text = “Killed Nemesis: " … totalKilled …” / " …gameTotalObjects
end
end
[/blockcode]
[import]uid: 11038 topic_id: 4572 reply_id: 304572[/import]