OOP help, is it ok to redeclare function within same function

Hi kinda asked this yesterday but got no answer, so i’ll try a second time. Trying to get my head around OOP. Essentially I want to overide a objects current move function with a new one or change its class???

Maybe my logic is all wrong hence why im asking for help, want to get it working the best way. Heres what I have done but I have a feeling its a bad method, could someone please let me know if its ok to redeclare the move function like I have???

[code]
local Angledetection = {}

function Angledetection.new(param)

local obstacle = require(“Obstacle”).new(param)

obstacle.superMove = obstacle.move

local travel = 0
local angle
local atanIt = math.atan2
local RADIANS_TO_DEGREES = 57.2957795 --180 / pi = 57.2957795

function obstacle:move(yOffset,tDelta,targetY,targetX)

self:superMove(yOffset)

angle = atanIt(targetY - self.y, targetX - self.x) * RADIANS_TO_DEGREES – work out angle between target and missile

if angle < 0 then
travel = travel + 1
if travel < 300 then
self.rotation = self.rotation + 3
else
function obstacle:move(yOffset,tDelta)
self:superMove(yOffset)
local speed = param.speed
local cosIt = math.cos
local sinIt = math.sin
local angle = 330 * (math.pi/180)
local angle = atanIt(aimY - self.y, aimX - self.x) – work out angle between target and missile
self.rotation = self.rotation - 8

local newSpeed = speed * tDelta
local originX = self.x + (cosIt(angle) * newSpeed) – update x pos in relation to angle
local originY = self.y + (sinIt(angle) * newSpeed) – update y pos in relation to angle

self.x, self.y = originX, originY

if originX > 420 or originY < - 40 or originX < - 40 then
obstacle:removeEventListener(“collision”, obstacle)
obstacle:removeSelf()
obstacle = nil
return
end

end
end
end
end

return obstacle

end

return Angledetection
[/code] [import]uid: 118379 topic_id: 23149 reply_id: 323149[/import]

I am not an expert in this department and someone else may have more insight (please feel free to correct me if so!) but I believe what you are doing should be fine :slight_smile: [import]uid: 52491 topic_id: 23149 reply_id: 92712[/import]

Thanks, peach it does seem to work ok but i cant help feeling like its bad techinique, im trying to achieve better code by creating a new function that removes conditionals that were previously needed which will improve performance.

Do you know what method is best for performance when it comes to OOP e.g. metatables vs tables/closures. Im currently using the tables method I think.

Ive been reading for a long time and frankly im very confused to what is best for performance?

any how thanks for vote of confidence. [import]uid: 118379 topic_id: 23149 reply_id: 92723[/import]

I’m afraid I haven’t gotten into OOP, however someone you might consider asking is @dunkelg on Twitter. If I recall correctly he was looking into OOP earlier in the year. (He’s the creator of ChocoRun.)

There would also be some resources online of course although they wouldn’t likely be Corona specific.

(A search for OOP in Corona returns several results though, which may be enough.)

Hope that helps,
Peach :slight_smile: [import]uid: 52491 topic_id: 23149 reply_id: 92932[/import]

Thanks peach, your a star. Managed to sort out a kinda of function stack.

Essentially add all my functions into a table, then have one line in my function that requests a function at a particular index, when a condition is met in the function it ups the index variable and on next frame event the next function gets called. Its actually a really good way to do things.

[code]
local Angledetection = {}

function Angledetection.new(param)

local obstacle = require(“Firehoming”).new(param)
obstacle.supersuperMove = obstacle.move
obstacle.superAngleBetween = obstacle.angleBetween
local angle
local detect = param.detect

local RADIANS_TO_DEGREES = 57.2957795 --180 / pi = 57.2957795

local stage = 1

local function distanceBetween(self)

local xfactor = self.targetX-self.x ; local yfactor = self.targetY-self.y
local distanceBetween = math.sqrt((xfactor*xfactor) + (yfactor*yfactor))
return distanceBetween
end

local spinBack = function(self,tDelta) self.rotation = self.rotation + 9 return self:supersuperMove(obstacle.yOffset, tDelta) end
local spinUp = function(self) obstacle.travel = obstacle.travel + 1 if obstacle.travel < detect then self.rotation = self.rotation + 3 else stage = stage + 1 end return self:superMove(obstacle.yOffset) end
local targetAtAngle = function (self) self.distance = distanceBetween(obstacle) if self.distance < 200 then stage = stage + 1 end return self:superMove(obstacle.yOffset) end

local functions = {targetAtAngle,spinUp,spinBack}

function obstacle:move(yOffset,tDelta,targetY,targetX)

self.targetY,self.targetX = targetY,targetX
self.yOffset = yOffset
functionsstage

end

return obstacle

end

return Angledetection
[/code] [import]uid: 118379 topic_id: 23149 reply_id: 92975[/import]