A Few Quick Questions On Understanding Modules

This one should be simple for people here to answer, and I know that I should already know this...but...

I'm trying to modularise my code and I need some clarification one one or two things before I really start.

I have a levelSetup.lua module file as follows:

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
local M = {}
 
local _W = display.contentWidth
local _H = display.contentHeight
 
local function addBorders()
 
        local ground = display.newRect(0,0,_W,20)
        ground.x = _W * 0.5; ground.y = _H - (ground.height * 0.5)
        ground:setFillColor(0, 0, 0)
        
        local ceiling = display.newRect(0,0,_W,20);
        ceiling.x = _W * 0.5; ceiling.y = 0 - (ceiling.height * 0.5)
        ceiling:setFillColor(0, 0, 0)
        
        local leftWall = display.newRect(0,0,10,_H)
        leftWall.x = 0 - (leftWall.width * 0.5); leftWall.y = _H * 0.5
        leftWall:setFillColor(0, 0, 0)
        
        local rightWall = display.newRect(0,0,10,_H)
        rightWall.x = _W + (rightWall.width * 0.5); rightWall.y = _H * 0.5
        rightWall:setFillColor(0, 0, 0)
 
end
 
M.addBorders = addBorders
 
return M

Hi insert.code,

It sounds like you're on the right track. Here's your answers:

1. Yes, but only if you put them into local variables (see question 2).

2. Yes, otherwise they will only be localized to the levelSetup module and you'll have no control over them (like moving them, deleteing them, etc.). Make sure you "catch" all of them by using a line like...

1
local myGround, myCeiling, myLeftWall, myRightWall = levelSetup.addBorders()

Hi Matt, thanks for the reply.

I've tried adding the code above, but I seem to be having issues. After assigning the objects to local variables, if I try something simple like:

myGround.x = 150

I get a Runtime error: attempt to index local 'myGround' (a nil value)

It also won't let me insert objects to the mainGroup - again because it's showing a nil

Any ideas?

is myGround local variable? If so it`s the issue no?

Yes, I assume so. It was set up using the code suggested above:

local myGround, myCeiling, myLeftWall, myRightWall = levelSetup.addBorders()

...

Sorry.

Let`s wait W2MD as he might know explain.

But for now iNSERT.CODE did you try make them global removing the Local keyword and using W2MF sugested code? If so did it work as you expected?

Cheers,
Rodrigo.

Can anyone else throw any light on this? I'd be very grateful.

Just thought, it may be worth mentioning that I'm using the new Stroyboard API for scene management.

At the end of your addBorders() function, add this line:

    return ground, ceiling, leftWall, rightWall

Hey, Jonathan, that does it for me! I mean, I've been puzzling over how I'm supposed to deal with external module/function. The problem was, I didn't think of returning more than one object/variable from a function. I didn't realize I could return multiple of variables/objects like that. Woohoo! My coding method just expanded.

Cheers,
Naomi

P.S. iNSERT.CODE, I'm sorry about jumping in with something that really doesn't add to your thread. I just couldn't help myself.

@Jonathanbeebe - Works, of course (was it ever in doubt?) - it seems so blindingly obvious when someone else points it out?

@Naomi - No problem. I feel the same when the lights turn on. Besides, I think positivity and enthusiasm can only add to a forum and community anyway.

Thanks for everyone's input on this - I'm (slowly) getting my head around modularising now.

views:1553 update:2011/11/19 17:31:56
corona forums © 2003-2011