为什么Director类使我的全局变量对于电晕中的外部模块不可见?

时间:2011-12-17 19:30:09

标签: global-variables corona

我正在做基本的事情来理解使用带有全局变量的外部类/模块,现在我正在尝试添加导演。但是当我添加导演类时,我的全局变量(例如applyThrust)在外部类中变得不可见。

在为Director重构之前,它运行良好。在使用导演之前,我只有2个文件,rocket.lua和下面的mainScreen.lua文件只是main.lua。我使用了相同的全局变量,applyThrust和thrustForceY,它起作用了。

顺便说一下,切换屏幕的导演功能似乎工作正常,除了使全局变量不可见的奇怪副作用。

我在电晕导演论坛上问过这个并没有得到回复,所以我想我会尝试可靠的STO。

这是我正在使用导演的文件:

main.lua

display.setStatusBar (display.HiddenStatusBar)

local director = require ("director")
local mainGroup = display.newGroup()

local function main()
    mainGroup:insert(director.directorView)
    director:changeScene("menu")
    return true
end

main()

menu.lua

module(..., package.seeall)

function new()
    local localGroup = display.newGroup()
    ------------------------------------------------------------------------------
    local playText = display.newText("Play",160, 100, "Arial", 32)
    playText:setTextColor(0, 0, 255)
    localGroup:insert(playText)

    local function pressPlay (event)
        if event.phase == "ended" then
            director:changeScene ("mainScreen")
        end
    end

    playText:addEventListener ("touch", pressPlay)

    ------------------------------------------------------------------------------
    return localGroup
end

mainScreen.lua

local Rocket = require( "rocket" )
local physics = require( "physics" )
module(..., package.seeall)

function new()
    local localGroup = display.newGroup()
    ------------------------------------------------------------------
    score = 100

    physics.start()

    local ground = display.newRect( 60, 170, 60, 60 )
    physics.addBody( ground, "static", { density=3.0, friction=0.5, bounce=0.2 } )

    rocket = Rocket.new(80, 110)

    local upText = display.newText("Up",160, 300, "Arial", 32)
    upText:setTextColor(0, 0, 255)

    ------------- my global variables  -----------
    thrustForceY = -100
    applyThrust = false

    local function pressUp (event)
        local t = event.target

        local phase = event.phase
        if( "began" == phase ) then
            display.getCurrentStage():setFocus( t )
            t.isFocus = true

            if( not applyThrust ) then
                rocket:addThrust()
            end
            rocket:applyForce(0, thrustForceY, rocket.x, rocket.y)
            applyThrust = true
        elseif "ended" == phase or "cancelled" == phase then
            rocket:removeThrust()
            display.getCurrentStage():setFocus( nil )
            t.isFocus = false
            applyThrust = false
        end
        return true
    end

    upText:addEventListener ("touch", pressUp)

    ----------------------------------------------
    return localGroup
end

rocket.lua

module(..., package.seeall)
local physics = require( "physics" )

--constructor--------------------
function new(x, y) 
    rocket = display.newGroup()

    local body = display.newRect( x, y, 25, 60 )
    physics.addBody( body, { density=1.5, friction=0.5, bounce=0.2 } )
    body.isFixedRotation = true
    rocket:insert(body)

    local thrust = {}

    function rocket:addThrust()
        thrust = display.newRect( body.x, body.y + (body.height / 2) , 10, 10 )
        thrust.y = body.y + (body.height / 2) + (thrust.height / 2)
        physics.addBody( thrust, { density=1.5, friction=0.5, bounce=0.2 } )
        rocket:insert(thrust)
    end

    function rocket:removeThrust()
        rocket:remove(thrust)
    end

    function rocket:applyForce( xForce, yForce, atPointX, atPointY )
        body:applyForce( xForce, yForce, atPointX, atPointY )
    end

    ----------------------------------------------------------------------
    --      enterFrame listener for rocket
    ----------------------------------------------------------------------

    function rocket:enterFrame (event)

        if( applyThrust ) then
            body:applyForce(0, thrustForceY, body.x, body.y)
            thrust.y = body.y + (body.height / 2) + (thrust.height / 2)
            thrust.x = body.x
        else
            print("applyThrust is nil")
        end

    end

    Runtime:addEventListener("enterFrame", rocket)

    return rocket
end

2 个答案:

答案 0 :(得分:0)

Corona中的全局变量就像使用“_G”一样简单。变量之前。

例如:

lives = 3

会变成:

_G.lives = 3

但是Director已经具有将参数传递给另一个场景的功能....不需要使用全局变量。

<强> scene1.lua

local params = {
       param1= "http://github.com/",
       param2 = "bla-bla"
}
director:changeScene( params, "screen2", "moveFromRight" )

<强> screen2.lua

new = function ( params )

        local url = params.param1
        local bla = params.param2
        ...
end

答案 1 :(得分:0)

好点。

此外,您可以将所有变量放在您创建的文件中,并从任何模块访问它。