I need to be able to trigger a function that quietly waits inside an object build function.
A hypothetical example:
[code]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
In this example script, I am building 4 boxes and hiding 3. I then have a listener on the first box to trigger the wake up function. I want that function to then space the boxes and wake them up via their own internal linked function.
I’ve commented the 2 lines of code syntax that I am unsure of how to implement. I’ve looked for examples but I’m not finding any. Any help is appreciated.
If you are unsure of what I am trying to do…please ask…I will try to clarify further. Thanks for helping (hopefully)! [import]uid: 9492 topic_id: 8113 reply_id: 308113[/import]
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
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
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 [import]uid: 12108 topic_id: 8113 reply_id: 28921[/import]
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 [lua]myInstance.sprite[/lua]. that way my object isn’t intrinsically linked to a loaded image and therefore it is easier to change it’s graphic when required [import]uid: 6645 topic_id: 8113 reply_id: 28928[/import]
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 [lua]myInstance.sprite[/lua]. that way my object isn’t intrinsically linked to a loaded image and therefore it is easier to change it’s graphic when required [import]uid: 6645 topic_id: 8113 reply_id: 28929[/import]
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.
[import]uid: 9492 topic_id: 8113 reply_id: 29037[/import]