Using functions with the Storyboard

Hi all,

I’m currently wondering how you use functions while using the storyboard.

I’m trying to use a function which incorporates the accelerometer, however it throughs error wherever I put it. My code is

local function moveKeeper:accelerometer(e) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- Accelerometer Movement &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keeper.x = display.contentCenterX + (display.contentCenterX \* (e.xGravity\*3)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((keeper.x - keeper.width \* 0.5) \< 0) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keeper.x = keeper.width \* 0.5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif((keeper.x + keeper.width \* 0.5) \> display.contentWidth) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keeper.x = display.contentWidth - keeper.width \* 0.5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;

I get the error

error loading module ‘fileName’ from file… ‘<’ expected.

I noticed that writing functions like: 

local example = function() &nbsp;

works however how do I incorporate the

:accelerometer(e) &nbsp;

If you need more information please let me know!

Welcome to Corona forums!

I’m afraid that I’m not familiar with defining functions with colon, are you trying to attach this method to moveKeeper variable/object ?

Does this work:

local function myFunction() --blah blah end moveKeeper.myFunction = myFunction

Re your topic question, I’m calling functions normally while utilizing storyboard, they just need to be defined before where you call them, like normal Lua functions.

If these did not help, please post some more code, maybe I then be able to help.

Regards.

This may be one of my main frustrations with Lua.  There are too many ways to declare functions.

When you see:

someObject:someMethod()

you are adding a function someMethod to the object someObject.  In Lua, objects are just overgrown tables.  Therefore someObject has to exist prior to doing this:

local someObject = {} – make it an empty table for now, perhaps you spawned a table and returned it.

function someObject:someMethod()

     – …

end

now works.  Notice I did not make the function “local”.  The object is local and I’m adding a member to it so you can’t use local. 

Doing:

local someVarible = function()

is pretty much the same as:

local function someVariable()

with the most practical difference being that you can pre-declare the variable for forward referencing purposes:

local someFunction   – just declaring the name here:

 

local function fred()

someFunction() – the compiler can code this since it knows the variable someFunction

end

 

someFunction = function()

– do the work here

end

Hopefully that helps.

Thank you very much, your replies were very helpful!

Welcome to Corona forums!

I’m afraid that I’m not familiar with defining functions with colon, are you trying to attach this method to moveKeeper variable/object ?

Does this work:

local function myFunction() --blah blah end moveKeeper.myFunction = myFunction

Re your topic question, I’m calling functions normally while utilizing storyboard, they just need to be defined before where you call them, like normal Lua functions.

If these did not help, please post some more code, maybe I then be able to help.

Regards.

This may be one of my main frustrations with Lua.  There are too many ways to declare functions.

When you see:

someObject:someMethod()

you are adding a function someMethod to the object someObject.  In Lua, objects are just overgrown tables.  Therefore someObject has to exist prior to doing this:

local someObject = {} – make it an empty table for now, perhaps you spawned a table and returned it.

function someObject:someMethod()

     – …

end

now works.  Notice I did not make the function “local”.  The object is local and I’m adding a member to it so you can’t use local. 

Doing:

local someVarible = function()

is pretty much the same as:

local function someVariable()

with the most practical difference being that you can pre-declare the variable for forward referencing purposes:

local someFunction   – just declaring the name here:

 

local function fred()

someFunction() – the compiler can code this since it knows the variable someFunction

end

 

someFunction = function()

– do the work here

end

Hopefully that helps.

Thank you very much, your replies were very helpful!