Hi Brent,
I’m very grateful to Omid for sharing his OOP work with the community free of charge (and even taking the time to revise his work based on constructive criticism from readers). Very cool of him. Regarding his implementation of OOP though, I do work in a different way and find some of the code structured in weird ways with him (or better said: in weird places - I understand why the code is there, but I would put some of the code that is in his object-code into the main game logic instead.
I’ll share my own code for simple OOP here, code that is basically very useful for a) modularizing your game code, B) spawning easily and most of all c) just creating an object and relying on that object itself to run all of it’s relevant code - no further maintenance or management required. This last point is the biggest advantage of OOP for me, actually.
-------------------
-- bulletClass.lua --
-------------------
local bulletClass = {}
local bulletClass\_metatable = { \_\_index = bulletClass} -- metatable
bulletClass.frameLoop = function(self, event)
-- draw self
self.image.x = self.image.x + self.xSpeed
self.image.y = self.image.y + self.ySpeed
-- see if bullet needs to fade and die
if event.time - self.startTime \> 1000 then
if self.fading == false then
self.fading = true
transition.to(self.image, {time = 800, xScale = .1, yScale = .1, transition = easing.inQuad})
timer.performWithDelay(850, function() self:stop() end)
end
end
end -- frameLoop
bulletClass.start = function(self)
-- this is our unique anonymous function
-- we need to give \*this one\* back to the event listener
--
local callback = function(event)
self.frameLoop(self, event)
end
self.f = callback -- let's save the function for later
Runtime:addEventListener( "enterFrame", callback )
end -- bulletClass.start
bulletClass.stop = function(self)
local callback = self.f
Runtime:removeEventListener("enterFrame", callback)
self.image:removeSelf()
self.image = nil
self = nil
end -- bulletClass.stop
-- CONSTRUCTOR --
-----------------
bulletClass.new = function(x,y,xSpeed,ySpeed)
local bulletInstance = {}
bulletInstance.image = display.newImage("ButterflyBullet.png", 200,200)
bulletInstance.image.xReference = 36
bulletInstance.image.x = x
bulletInstance.image.y = y
bulletInstance.xSpeed = xSpeed
bulletInstance.ySpeed = ySpeed
bulletInstance.image.rotation = math.deg(math.atan2(ySpeed, xSpeed))
bulletInstance.fading = false
bulletInstance.startTime = system.getTimer()
setmetatable (bulletInstance, bulletClass\_metatable)
bulletInstance:start()
return bulletInstance
end
--
--
return bulletClass
That’s it. This object takes care of it’s own removal so you don’t even need to give it a handle. Just require “bulletClass” from your main.lua file and then say bulletClass.new(x position to start, y position to start, x speed per frame, y speed per frame)
Don’t have the time to go into further detail but suffice to say that this just a simple practical example of basic OOP without inheritance, sub- or superclasses etc… But I do believe that, together with an even simpler static class this cover 99 percent of the OOP needs for Corona programmers.
Also I must say THANK YOU to dmCuskey, how was essential in my understanding of this. Thank dmCuskey!