Hi peps, been getting my head round OOP and am now trying to improve my skills further with class inheritance. Im trying to work the best to approach something I think makes sense.
At current I have a class which drops a object using parameters fed into the class, this class has a move function that gets called on enterframe which moves the object.
I have then extended the class using inheritance to add rotation to the object if it is at particular angle in relation to a target parameter. All fine so far.
Now what I want to know is what would be the best way to override the move function with a completely new one or maybe overide the entire object with a new set of classes. Essentially once the object has rotated for a period of time I want the move function to completely change.
I know I can do it with conditionals but want something a bit more intuitive. Heres some code to assist all the above. Hope it makes sense? All help appreciated.
-- superclass
local Obstacle = {}
function Obstacle.new(param)
local obstacle, speed
local obstacle = display.newImageRect(param.image, param.width, param.height)
physics.addBody(obstacle, param.physic, { density=param.density, friction=param.friction, bounce=param.bounce })
obstacle.id = param.id
function obstacle:velocity(value)
speed = value
end
local obstacleY
function obstacle:fire(percent,height)
obstacle.x = percent
obstacleY = height
obstacle.y = obstacleY
end
function obstacle:move(yOffset)
obstacleY = obstacleY + yOffset
if obstacleY \> 520 then
obstacle:removeEventListener("collision", obstacle)
obstacle:removeSelf()
obstacle = nil
return
end
obstacle.y = obstacleY
end
function obstacle:destroy()
obstacle:removeEventListener("collision", obstacle)
obstacle:removeSelf()
obstacle = nil
end
return obstacle
end
return Obstacle
-- this class the inherits Obstacle class and is called on enter frame
local Angledetection = {}
function Angledetection.new(param)
local obstacle = require("Obstacle").new(param)
obstacle.superMove = obstacle.move
function obstacle:move(yOffset,tDelta,targetY,targetX)
local atanIt = math.atan2
local RADIANS\_TO\_DEGREES = (180 /math.pi)
local angle = atanIt(targetY - self.y, targetX - self.x) \* RADIANS\_TO\_DEGREES -- work out angle between target and missile
self:superMove(yOffset)
if angle \< 0 then
self.rotation = self.rotation + 3
-- at some point here I wish to overide this object with a new set of functions/class????
end
end
return obstacle
end
return Angledetection
Hope it makes sense what im trying to achieve. [import]uid: 118379 topic_id: 23100 reply_id: 323100[/import]