I need to be able to trigger a function that quietly waits inside an object build function.
A hypothetical example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | local xSpacing = 0 function buildBoxes() local box = {} local i for i = 1,4 do box[i] = display.newRect(20,20,20,20) box[i].isVisible = false local function wakeupThisBox() -- PROBLEM LINKING FUNCTION TO OBJECT box[i].x = xSpacing box[i].isVisible = true end end end buildBoxes() box[1].isVisible=true function wakeUpBoxes() local n for n = 2,4 do xSpacing = xSpacing + 25 box[n]:wakeupThisBox() -- PROBLEM TRIGGERING THE OBJECT'S INTERNAL FUNCTION end end box[1]:addEventListener("tap",wakeUpBoxes) |
both box and wakeupthisbox are local to the function. they wont be visible outside the function
this is how i'd do it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | local xSpacing = 0 local box={} -- accessible throughout file now local function makeBox() -- let's give each box it's own constructor local someBox = display.newRect(0,0,20,20) someBox.isVisible=false someBox:setReferencePoint(display.TopLeftReferencePoint) function someBox:wakeupThisBox() -- self is inherently passed as a parameter self.x = xSpacing self.isVisible = true end -- we've built the box locally, now send it back return(someBox) end local function buildBoxes() local i for i = 1,4 do -- call the constructor, -- and stick the result in our array box[i] = makeBox() end end local function wakeUpBoxes() local n for n = 2,4 do xSpacing = xSpacing + 25 box[n]:wakeupThisBox() end end buildBoxes() box[1].isVisible=true box[1]:addEventListener("tap",wakeUpBoxes) |
sorry for the tangent, but just want to respond to your last comment
well actually i'd have a box module with a metatable (since currently you're creating duplicates of a function for every box, which is a waste of memory), but let's save that for later
"Waste" is a relative term, because it seems to be a tradeoff between speed and memory:
http://developer.anscamobile.com/forum/2011/01/21/oop-game-classes-objects-inheritance-methods-properties
But as I said "it seems." I'm not sure, do you know anything about this matter beyond what was addressed in that thread? I probably wouldn't start using metatables anyway because you can't set the metatable on a display object, but it'd be good to understand the specifics:
http://developer.anscamobile.com/content/display-objects#Display_Objects_vs._Tables:_A_Technical_Discussion
well it depends on how you construct your object. I have a "class" that wraps the display object so the actual display object is reference by eg myInstance.sprite
. that way my object isn't intrinsically linked to a loaded image and therefore it is easier to change it's graphic when required
well it depends on how you construct your object. I have a "class" that wraps the display object so the actual display object is reference by eg myInstance.sprite
. that way my object isn't intrinsically linked to a loaded image and therefore it is easier to change it's graphic when required
thanks JMP!!!
I used your sample to help me rebuild my function (not the box function above but my real game function) and it works like a champ!!!
It seems that breaking out the build functions from the table array and then returning that object to the table was the key element I was missing. (I actually had the table out of the function in my real life code and not inside per my example.)
Now I am able to RESET my game assets freely to a default state without destroying them and rebuilding them (laggy solution)!!! (which is what I was actually trying to do but used the box sample above as my real code is more complex and I wanted to isolate the issue).
Thanks again for your response and for the assistance!!! It looks like I am turning the corner from a beginner to an intermediate OO programming. :)
As for modules...I will take that fight on another day. :)