Need your help Guys!!

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]

You’re doing too much in one routine…

If that is a general collision function, break it out into object specific collision functions - ie: don’t use one collision function for every collision in the entire program, put collision handlers into the objects which need to handle them.

It also looks like you’re trying to do display update (scores) within the collision handler. Move that code somewhere else - you definately don’t want to try to update screen content every time a collision happens as they could be firing a lot. The most often you want to update specific display objects is on enterFrame events, any more and your wasting processor time.

IMHO, try to take a more Object Oriented approach. I say that because, from the code in your post, it looks like you’re probably using lots of objects which don’t deal with their own state; you’re using global functions and values to manage collections of objects. Try to build your objects as if they were in classes, eg: Java, C#, etc. But that’s only IMHO - others will probably have many different ideas there.

matt. [import]uid: 8271 topic_id: 4572 reply_id: 14646[/import]

One more thing…

I started (and still do) with proof of concept code. This often does not lead to well structured app code - take that code, break it into sections and have each one deal with its own state. What that should imply is that, if I have 3 types of object in the app, I take those, create .lua files for each and in there have an appropriate function to create instances (an OO term) of that object (‘class’, in OO terms.)

Not wishing to teach a grandmother’s egg blowing class here, but that how’s I do it. And yes, I got bitten by the same issues in my earlier lua code, too. So don’t worry. I’ve now structured my lua code the same way as my C# - the only difference is that the ‘class’ keyword and constructor is replaced by the ‘function’ keyword and its arguments. A fairly big mental jump if you’re not used to it, but not hard to get used to.

Hope this helps. Like I said; not wishing to teach anyone to suck eggs…

matt [import]uid: 8271 topic_id: 4572 reply_id: 14648[/import]

Thanks mate for you wonderful and helpful reply. I’m really interested to fix my code Coz I was sure it misses the OOP cocept and that’s why I have posted this here and would like you to help me in this regard.

I really keen to learn but I don’t know where ti start and how. I’ve just used my basic OOP programming concept in this.

I wish if you could support me in this regard and help me to restructure my code again.

Do u mind working in parallel wiggle in this regard. Fir example just if you could show me and example of displaying a spaceShipobject and nemesisObjects. SpaceShip will through bullets to shot nemesises shown on the screen.

Basically, this what I’m trying to do in my game and hope that i could find an expert person like you who could help. Thereafter, I’ll learn and update my code according to your directions.

Thanks again mate [import]uid: 11038 topic_id: 4572 reply_id: 14654[/import]

One more thing if possible just show me how to build the classes you have mentioned for this two objects and how to separate the collisions as well.

I know you may not be able to support that much Coz I’m sure you would be busy. In all cases I appreciate your support

Thanks [import]uid: 11038 topic_id: 4572 reply_id: 14655[/import]

Well, create a lua file for one thing, say your spaceship, and create a function in it. Then create a local table or display object and start adding variables to it for things like state, name, id or whatever.

Provide some manipulation functions on the table to modify it or get values back. Then attach events to it, to deal with different situations.

The following code is an example of what, in my opinion, each file should contain in order to be self-contained, self-managing code in an Object Oriented design. Again, others may disagree, but it works for me:

[lua]spaceship.lua:

function newSpaceship( parentGrp, imgPath, x, y )

– create ship
local ship = display.newImage( parentGrp, imgPath )

– place ship on screen
ship.x, ship.y = x, y

– have ship detect collisions
function ship:collision( event )
– do something here
return true
end

– function for the game to use to make the ship do something
function ship:spinShip( spinDegrees, time )
– make the ship spin (for no useful reason)
transition.to( ship, { time=time, rotation=spinDegrees } )
end

– attach collision listener (and any other listeners)
ship:addEventListener( “collision”, ship )

– return the ship object for use in the game
return ship

end[/lua] [import]uid: 8271 topic_id: 4572 reply_id: 14667[/import]

Thsnks my mate for the pice of art you’ve sent.
So basically, i use the same concept to build enemiesShips.lua, and add inside that file an prooerty to shot bullets as well. However, im still cofused how to manage the collions between the ship’s bullet and other enimiesShips incase the ship’s bullet hits one of the enemies’ ship. Also youve mentioned somthing about performance which i didnt get it. You’ve said thst i should do the update score and other stuff at enterFrame listener instead of the collision listener.

Thanks [import]uid: 11038 topic_id: 4572 reply_id: 14676[/import]

I’ve not done shooting games yet, but I would probably go with having a collision listener on the objects which can be shot, not the bullets themselves. Keep the bullet code very simple.

Have a ‘.name’ value on the bullet with the value “bullet” and in the spaceship (or enemy) collision listeners check the name of the object which has collided with it. If the value of the name is “bullet” then you know it’s been shot.

No, don’t update the score in the enterFrame handler. Update the score variable whenever the score changes. If that is in a collision handler then fine. Update the display of the score (I assume you’ll be using images to show the score’s numbers, etc) in the enterFrame handler, or less.

One thing you could do is update the score on a timer, but you don’t want to do /any heavy work/ in physics listeners or fast timers because they could slow the code down, which would cause the frame rate to drop.

So, update the score variable whenever you like and maybe set another value (eg: scoreChanged) to true. In the enterFrame handler, if ‘scoreChanged == true’ then update the displayed score.

matt [import]uid: 8271 topic_id: 4572 reply_id: 14679[/import]

Thanks my friend. I really appreciate your support.
I’ll fix the code and will send it to you to have your opinion on it.

By the way, I’m facing a problem with Corona Simulator related to the default view when I launch the simulator. Everytime I open the simulator it opens in minimized iPhone screen and I have to click on ‘Command’ + ‘=’ to maximize the iPhone simulator. However, I haven’t been faxing this since I downloaded corona, this problem has occurred 2 weeks back and I’m unable to solve it.

Thanks again. [import]uid: 11038 topic_id: 4572 reply_id: 14681[/import]