グローバル変数を持つ外部クラス/モジュールを使うことを理解するために基本的なことをやっていますが、今はディレクターを追加しようとしています。しかし、ディレクタークラスを追加すると、applyThrustなどのグローバル変数が外部クラスで表示されなくなりました。なぜDirectorクラスは、グローバル変数をコロナの外部モジュールから見えないようにしますか?
ディレクターのリファクタリング前に正常に機能しました。ディレクターを使う前に、私は2つのファイルrocket.luaとmainScreen.luaファイルを持っていました。単にmain.luaでした。私は同じグローバル、applyThrustとthrustForceYを使用し、それは働いた。
ところで、画面を切り替えるディレクタの機能は、グローバル変数を表示しないというこの奇妙な副作用以外は正常に動作しているようです。
私はコロナディレクターのフォーラムでこれを尋ねたので、私は信頼できるSTOを試してみると思いました。
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
mainScreen.lua
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
:
ここで私はディレクターのもので使用していたファイルです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
グローバル変数が '_G.myVarになりました。私はそれをベストプラクティスとして読んだと思ったので、私は_Gの使用を避けようとしていました。それにもかかわらず、Directorのパラム通過についてのあなたの応答に感謝します。私はそれを試みますが、それはまさに問題ではありません。問題は、mainScreen.luaとrocket.luaの間のグローバル変数 – user26270