Hi.
The following example shows (I hope) the method I’m using for making a module that can create/return viewGroup objects.
Inside the new() method I create local methods and vars (speed). I add an enterFrame listener that calls a local method. I also create a new viewGroup and return it.
The part I cannot get my head around is why and how my returned viewGroup object has knowledge of the local variable speed and the other local methods. I never add any references to them in viewGroup - so how?
If I create more viewGroups by calling new() each one will output a different speed in their onEnterFrame. This tells me each viewGroup must have speed set as a local private variable.
Is this true? - or what is going on here?
Any help as very welcome - thanks
In file “factory.lua” I have the following:
[lua]local factory = {}
function factory:new()
local addListeners – forward method declaration
local onEnterFrame – forward method declaration
local speed = math.random(1,10)
local viewGroup = display.newGroup()
addListeners = function()
Runtime:addEventListener(“enterFrame”, onEnterFrame)
end
onEnterFrame = function()
print(“enterframe called - speed”, speed)
viewGroup.x = viewGroup.x + speed – move viewGroup
end
addListeners()
return viewGroup
end
return factory [/lua]
[import]uid: 87555 topic_id: 18668 reply_id: 318668[/import]