Need help calling a function inside an object from another function

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

box[1]:addEventListener(“tap”,wakeUpBoxes)
[/code]

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]

both box and wakeupthisbox are local to the function. they wont be visible outside the function

this is how i’d do it
[lua]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)[/lua]

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

[import]uid: 6645 topic_id: 8113 reply_id: 28916[/import]

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 [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]

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. :slight_smile:

As for modules…I will take that fight on another day. :slight_smile:
[import]uid: 9492 topic_id: 8113 reply_id: 29037[/import]