I don't get it, how is this not working??

I’m getting an error on “localGroup:insert(sun)”, “drawBackground”, and “gameInit” …

Why isn’t this working? If I take them out of functions, it works fine, but when I add them as functions ( saves memory?), it doesn’t work. Quite frustrating.

[code]
module(…, package.seeall)

function new()
local localGroup = display.newGroup()

system.activate( “accelerometer” )
system.activate( “multitouch” )
system.setIdleTimer(false)
display.setStatusBar( display.HiddenStatusBar )

– External Libraries
local movieclip = require “movieclip”
local ui = require “ui”
local physics = require “physics”
local widget = require “widget”
physics.start()
physics.setGravity(0,0)
local background
local leftwall
local sun
local fog
local fog2
local baseline = 280

local drawBackground = function()

background = display.newImage(“backgroundgame.png”)
background:setReferencePoint(display.TopLeftReferencePoint)
background.x = -45; background.y = 0
localGroup:insert(background)

sun = display.newImage(“sun”, 22, 19)
localGroup:insert(sun)

fog = display.newImage(“fog.png”)
fog:setReferencePoint( display.CenterLeftReferencePoint )
fog.x = 0; fog.y = baseline + 20

fog2 = display.newImage(“fog2.png”)
fog2:setReferencePoint( display.CenterLeftReferencePoint )
fog2.x = 480; fog2.y = baseline + 20

localGroup:insert(fog)
localGroup:insert(fog2)

tPrevious = system.getTimer()

local function move(event)

local tDelta = event.time - tPrevious
tPrevious = event.time

local xOffset = ( 0.6 * tDelta )

sun.x = sun.x + xOffset * 0.03

fog.x = fog.x - xOffset
fog2.x = fog2.x - xOffset

if fog.x + fog.contentWidth < 0 then
fog:translate( 480 * 2, 0 )
end
if fog2.x + fog2.contentWidth < 0 then
fog2:translate( 480 * 2, 0 )
end
end
move()
end
local gameInit = function()
physics.start(true)
physics.setGravity( 0,0 )

drawBackground()

Runtime:addEventListener(“enterFrame”, move)
end

gameInit()

return localGroup
end [import]uid: 114389 topic_id: 30634 reply_id: 330634[/import]

Well, this wouldn’t explain why it’s working outside of a function but not in, but shouldn’t

[lua] sun = display.newImage(“sun”, 22, 19) [/lua]

be

[lua] sun = display.newImage(“sun.png”, 22, 19) [/lua]

If that’s the case then it probably would give you an error when you tried to insert that object into a group… you would then get more info telling you it was from the drawBackground function which was called from your game init function.
[import]uid: 147305 topic_id: 30634 reply_id: 122742[/import]

I’ve never seen functions declared like that, is that a correct syntax?

I’m not 100% as I am quite new to this, but should it be
[lua]local function drawbackground()
–instead of
local drawbackground = function()[/lua] [import]uid: 62706 topic_id: 30634 reply_id: 122747[/import]

Thanks for noticing that .png error, but sadly that didn’t help. I get the “ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ':” error which is really annoying and useless.

And I tried

local function drawBackground()  

also but that didn’t work. I don’t think that matters. I’ve studied a lot of code and see both. [import]uid: 114389 topic_id: 30634 reply_id: 122750[/import]

A couple of other things that are seem odd to me (that don’t necessarily fix your specific problem).

On Line 75 you call move(), which is probably a mistake, because you are using the enterFrame event to do the movement. Plus you are not passing an event parameter to it, so that should produce some errors within the move function.

On Line 86 you call the Runtime:addEventListener(“enterFrame”,move). The move function looks like it is out of scope. It is declared local within the drawBackground function. [import]uid: 94868 topic_id: 30634 reply_id: 122756[/import]

The “ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ':” error " means your localGroup is null when you are calling the insert statement. A simple print(localGroup) should tell you if it’s nil or it will return table x053523(something like that).

For a quick test try 1 of the following:

  1. remove local from local localGroup = display.newGroup() … meaning it just says localGroup = display.newGroup().

  2. move the local localGroup = display.newGroup() into your drawBackground function.
    [import]uid: 147305 topic_id: 30634 reply_id: 122757[/import]

@CraftyDeano : Just to mention that both of these ways to declare function are legible in Lua.

local function drawbackground() local drawbackground = function() [import]uid: 142361 topic_id: 30634 reply_id: 122872[/import]

I look at Beebes examples and have bought that Corona SDK book and they all have a function that starts the game “gameInit()” or “startGame()”, but every time I try that way, it doesn’t work as in the example above with a simple background, even if I follow along. [import]uid: 114389 topic_id: 30634 reply_id: 122913[/import]

@CraftyDeano & @Roland

FWIW, I believe the difference in the function declaration syntax is that when using

local function drawbackground()

the syntax above can call itself from inside the function. You can’t do this when using the other syntax.

local drawbackground = function() --cant' call itselft

From what I’ve read, I believe there are no performance differences, only this limitation of the bottom syntax. If I’m wrong here, please correct me.

@Silky,

Not sure if this is related to your problem or not. I’ve never done or seen what you are doing by containing your entire module.lua into a single global function that contains all your local declarations.

Nail

[import]uid: 106779 topic_id: 30634 reply_id: 123009[/import]

Well, this wouldn’t explain why it’s working outside of a function but not in, but shouldn’t

[lua] sun = display.newImage(“sun”, 22, 19) [/lua]

be

[lua] sun = display.newImage(“sun.png”, 22, 19) [/lua]

If that’s the case then it probably would give you an error when you tried to insert that object into a group… you would then get more info telling you it was from the drawBackground function which was called from your game init function.
[import]uid: 147305 topic_id: 30634 reply_id: 122742[/import]

I’ve never seen functions declared like that, is that a correct syntax?

I’m not 100% as I am quite new to this, but should it be
[lua]local function drawbackground()
–instead of
local drawbackground = function()[/lua] [import]uid: 62706 topic_id: 30634 reply_id: 122747[/import]

Thanks for noticing that .png error, but sadly that didn’t help. I get the “ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ':” error which is really annoying and useless.

And I tried

local function drawBackground()  

also but that didn’t work. I don’t think that matters. I’ve studied a lot of code and see both. [import]uid: 114389 topic_id: 30634 reply_id: 122750[/import]

A couple of other things that are seem odd to me (that don’t necessarily fix your specific problem).

On Line 75 you call move(), which is probably a mistake, because you are using the enterFrame event to do the movement. Plus you are not passing an event parameter to it, so that should produce some errors within the move function.

On Line 86 you call the Runtime:addEventListener(“enterFrame”,move). The move function looks like it is out of scope. It is declared local within the drawBackground function. [import]uid: 94868 topic_id: 30634 reply_id: 122756[/import]

The “ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ':” error " means your localGroup is null when you are calling the insert statement. A simple print(localGroup) should tell you if it’s nil or it will return table x053523(something like that).

For a quick test try 1 of the following:

  1. remove local from local localGroup = display.newGroup() … meaning it just says localGroup = display.newGroup().

  2. move the local localGroup = display.newGroup() into your drawBackground function.
    [import]uid: 147305 topic_id: 30634 reply_id: 122757[/import]

@CraftyDeano : Just to mention that both of these ways to declare function are legible in Lua.

local function drawbackground() local drawbackground = function() [import]uid: 142361 topic_id: 30634 reply_id: 122872[/import]

I look at Beebes examples and have bought that Corona SDK book and they all have a function that starts the game “gameInit()” or “startGame()”, but every time I try that way, it doesn’t work as in the example above with a simple background, even if I follow along. [import]uid: 114389 topic_id: 30634 reply_id: 122913[/import]

@CraftyDeano & @Roland

FWIW, I believe the difference in the function declaration syntax is that when using

local function drawbackground()

the syntax above can call itself from inside the function. You can’t do this when using the other syntax.

local drawbackground = function() --cant' call itselft

From what I’ve read, I believe there are no performance differences, only this limitation of the bottom syntax. If I’m wrong here, please correct me.

@Silky,

Not sure if this is related to your problem or not. I’ve never done or seen what you are doing by containing your entire module.lua into a single global function that contains all your local declarations.

Nail

[import]uid: 106779 topic_id: 30634 reply_id: 123009[/import]