Hi All,
I have to admit I am very frustrated with LUA at the moment. I come from AS3.0, PHP, JavaScript and other languages including Objective-C.
From the moment I use Corona I am trying to grasp LUA’s table structure but I keep failing when it comes to giving my projects some structure trying to implement OOP patterns. I wish it was somewhat similar to the AS Object model, but it’s completely different from AS3.0 despite the claims that are made on the corona website.
Allright, got that out of my system. I hope someone is willing to shed some light on the following:
I have some marbles in my game. I want the marbles to be classes so I can make instances of them and control their status by calling functions and set properties. Nothing fancy.
[lua]module(…, package.seeall)
function newMarble()
local marbleGroup = display.newGroup()
local m = display.newCircle(0, 0, 10)
m:setReferencePoint(display.CenterReferencePoint)
m.strokeWidth = 3
marbleGroup:insert(m)
function marbleGroup:setMarbleToActive()
print(‘setting ’ … marbleGroup.creationID … ’ to active’)
marbleGroup.active = true
m:setStrokeColor(255, 0, 0)
end
function marbleGroup:setMarbleToIdle()
marbleGroup.active = false
m:setStrokeColor(128, 128, 128)
end
marbleGroup:setMarbleToIdle();
marbleGroup:setReferencePoint(display.CenterReferencePoint)
physics.addBody(marbleGroup,
{density=1.0, friction=1.0, bounce=0.4, radius=10.0})
marbleGroup.angularDamping = 1
marbleGroup.linearDamping = 1
marbleGroup.name = “marble”
– there is no default event for whenever a physics object goes to sleep
– I have created a method to fire a event when the marble goes sleeping
local function checkForSleep(event)
if(marbleGroup.isAwake == false)then
Runtime:removeEventListener(“enterFrame”, checkForSleep)
marbleGroup:dispatchEvent{name=“wenttosleep”, target=marbleGroup}
end
end
– It’s nescessary to start the checking.
function marbleGroup:startCheckForSleep()
Runtime:addEventListener(“enterFrame”, checkForSleep)
end
return marbleGroup
end [/lua]
This works, but I find it very messy to create classes like this. That’s why I feel it’s not supposed to be done like this…? I am looking around the web to find ways to do it properly, but I keep getting confused a lot. I am trying this now:
[lua]Marble = {}
Marble_mt = {__index = Marble}
Marble.version = 0.1
function Marble:new(marbleType)
local self = {}
setmetatable(self, Marble_mt)
if marbleType == ‘default’ or marbleType == ‘’ then
radius = 10
strokeWidth = 3
density = 1.0
friction = 1.0
bounce = 0.4
linearDamping = 1
angularDamping = 1
end
– Create visual marble
self.name = ‘marble’
self.view = display.newGroup()
self.marbleGraphics = display.newCircle(0, 0, radius)
self.marbleGraphics:setReferencePoint(display.CenterReferencePoint)
self.marbleGraphics.strokeWidth = strokeWidth
self.view:insert(self.marbleGraphics)
self.view:setReferencePoint(display.CenterReferencePoint)
– Let’s get physical
physics.addBody(
self.view,
“dynamic”,
{
density = density,
friction = friction,
bounce = bounce,
radius=radius
})
self.view.angularDamping = angularDamping
self.view.linearDamping = linearDamping
– Default to idle state
self:setToIdle()
– Return instance
return self
end
function Marble:setToActive()
self.active = true
self.marbleGraphics:setStrokeColor(255, 0, 0)
end
function Marble:setToIdle()
self.active = false
self.marbleGraphics:setStrokeColor(128, 128, 128)
end
function Marble:startCheckForSleep()
Runtime:addEventListener(“enterFrame”, checkForSleep)
end
local function checkForSleep(event)
if Marble.isAwake == false then
Runtime:removeEventListener(“enterFrame”, checkForSleep)
self.dispatchEvent{name=“wenttosleep”, target=self}
end
end[/lua]
I can get a Marble in my game like this, and the class itself feels better organised. But to use this is just not very friendly at all. To add event listeners I have to use instance.view:addEventListener. That has some serious drawbacks if I want to call functions in the listener functions that are not assigned to view for example. I just want to add the event listener to instance:addEventListener like I can with my messy solution.
To come to a conclusion. How do you guys make classes for objects like this?
Thanks! [import]uid: 127668 topic_id: 28726 reply_id: 328726[/import]