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:
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()[/code]To sense when my player has reached the top of the screen, I am using a wrap function like so:local function wrap (event)if player.x < 25 thenplayer.x = 25endif player.x > 743 thenplayer.x = 743endif player.y < 50 thenplayer.y = 50 endif player.y > 1010 thenplayer.y = 1010endendRuntime:addEventListener("enterFrame", wrap)[/code]What I want to do is trigger the moveCamera function when the player.y < 50 condition is met. Unfortunately, the moveCamera function is local to the function entitled "main", and the player.y < 50 conditional is local to the "wrap" function. How would I work around this fact to accomplish what I'm trying to achieve? I'm sure this is a newbie question, so any help would be much appreciated.Thank you,Steven [import]uid: 79394 topic_id: 15328 reply_id: 315328[/import]
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:
[lua]local main
local wrap[/lua]
then on your actual function you would code it like so:
[lua]main = function()
–and the rest of the function of course
–and
wrap = function()
–and the rest of the function of course[/lua]
doing it this way still keeps your functions local, but makes it so you can call them from anywhere in your code. With this method you should be able to call your wrap function from anywhere in this same .lua file by just using
[lua]wrap()[/lua]
hope that was helpful, im guessing its not what you were asking exactly =D [import]uid: 19620 topic_id: 15328 reply_id: 56591[/import]
Maybe you might be interested to have a read on this article/tutorialhere to understand a bit more about scopes of functions and ways to call them.
cheers,
?
[import]uid: 3826 topic_id: 15328 reply_id: 56798[/import]
I’ll be sure to give it a read, jayan. Thanks for the tip!
Cheers,
Steven [import]uid: 79394 topic_id: 15328 reply_id: 56801[/import]