How do you trigger a function externally from outside that function?

I've got a moveCamera function that I want to trigger when my player reaches the top of the screen. Currently, for testing, I've got the moveCamera function triggered by a 3 second timer like so:

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
local function main()
        --forward declaration of the table, so it is visible.
        local spawnedGroup = display.newGroup()
        local transitions = {}
        local timers = {}
        
        local function squareSpawn (event)
                local square = display.newRect(50, 50, 25,25)
                square.x = -100
                square.y = math.random(0,1024)
                transitions[#transitions + 1] = transition.to(square,  {time = 1500, delay = 0, x = square.x + 968, onComplete=function() square :removeSelf() end})
                spawnedGroup:insert(square)
        end
        
        local cleanUp = function()
                --cancel and nil all the transitions
                for i=1,#transitions do
          if transitions[i] then
                 transition.cancel(transitions[i])
                 transitions[i] = nil
          end    
        end
        --cancel and nil all the timers
        for i=1,#timers do
          if timers[i] then
                 timer.cancel(timers[i])
                 timers[i] = nil
          end    
        end
        display.remove(spawnedGroup)
        end
        
        local moveCamera = function()
                transitions[#transition + 1] = transition.to(spawnedGroup, {time = 1000, y = spawnedGroup.y + 1200, onComplete = cleanUp})      
        end     
        
        timers[#timers + 1] = timer.performWithDelay(math.random(20,35),squareSpawn,0)
        timers[#timers + 1] = timer.performWithDelay(3000, moveCamera, 0)
end
 
main()

I myself am pretty noobish to coding, this probly isn't what you are looking for but i thought i would just give my thought.

If both of these functions are in the same .lua file, at the very beginning of your code, first create your function variables like this:

1
2
local main
local wrap

Maybe you might be interested to have a read on this article/tutorial here to understand a bit more about scopes of functions and ways to call them.

cheers,

?:)

I'll be sure to give it a read, jayan. Thanks for the tip!

Cheers,
Steven

views:1533 update:2011/10/15 21:01:16
corona forums © 2003-2011