how to have local with function

hi,

when i declare this object “background” with local the transition don’t work but without the local the function don’t work…Is it a way to have local with a function and that works?

Thanks.

local function drawBackground()     local background= display.newImageRect(myGroupBackground, "backgroundnb.png",480,320)     background.x=display.contentCenterX     background.y=display.contentCenterY     background.alpha=1     return background end drawBackground() transition.to(background,{time=1000,alpha=0}) --don't works local function drawBackground()     background= display.newImageRect(myGroupBackground, "backgroundnb.png",480,320)     background.x=display.contentCenterX     background.y=display.contentCenterY     background.alpha=1     return background end drawBackground() transition.to(background,{time=1000,alpha=0}) --works

Hi @bexphones,

Try doing a local up-value (forward reference) like this:

[lua]

local background  – The up-value (forward reference)

local function drawBackground()

    – Now, “background” refers to the up-value

    background= display.newImageRect(myGroupBackground, “backgroundnb.png”,480,320)

    background.x=display.contentCenterX

    background.y=display.contentCenterY

    background.alpha=1

    return background

end

drawBackground()

– And also here (the up-value is being referenced)

transition.to(background,{time=1000,alpha=0})

[/lua]

Brent

Alternatively, just have your “drawBackground()” function create the object locally (to itself) and then return it to another local variable, upon which you can do your transition.

[lua]

local function drawBackground()

    – This “background” is local to the function and only exists in its scope

    local background= display.newRect( 200,200,200,200 )

    background.x=display.contentCenterX

    background.y=display.contentCenterY

    background.alpha=1

    return background

end

– This “background” is NOT the same reference as the one scoped inside the function above.

local background = drawBackground()

transition.to(background,{time=1000,alpha=0})

[/lua]

hi Brent,

Thanks for the explanation.

Hi @bexphones,

Try doing a local up-value (forward reference) like this:

[lua]

local background  – The up-value (forward reference)

local function drawBackground()

    – Now, “background” refers to the up-value

    background= display.newImageRect(myGroupBackground, “backgroundnb.png”,480,320)

    background.x=display.contentCenterX

    background.y=display.contentCenterY

    background.alpha=1

    return background

end

drawBackground()

– And also here (the up-value is being referenced)

transition.to(background,{time=1000,alpha=0})

[/lua]

Brent

Alternatively, just have your “drawBackground()” function create the object locally (to itself) and then return it to another local variable, upon which you can do your transition.

[lua]

local function drawBackground()

    – This “background” is local to the function and only exists in its scope

    local background= display.newRect( 200,200,200,200 )

    background.x=display.contentCenterX

    background.y=display.contentCenterY

    background.alpha=1

    return background

end

– This “background” is NOT the same reference as the one scoped inside the function above.

local background = drawBackground()

transition.to(background,{time=1000,alpha=0})

[/lua]

hi Brent,

Thanks for the explanation.