OK, I have made some progress but now I run into a problem that seems totally weird to me. This is my code, the key point here is the event listener for “enterFrame”. Notice that it is commented out in both locations, I will explain below.
require "scriptengine"
local sceneGroup
function startScene(sceneName)
local function btn\_play\_t ( event )
print (event.phase)
if event.phase == "ended" then
sceneGroup.play\_button.isVisible = false
--Runtime:addEventListener( "enterFrame", scriptengine.scriptAction );
end
end
director:changeScene(sceneName,"overFromRight");
scriptengine.setRestartFlag();
scriptengine.setSceneName(sceneName);
sceneGroup = ((mainGroup[1])[2])[1]
local btn\_play = display.newImageRect("images/play\_button.png",100,100)
btn\_play:addEventListener("touch",btn\_play\_t)
btn\_play.x = 240
btn\_play.y = 160
btn\_play.alpha = 0.5
sceneGroup:insert(btn\_play)
sceneGroup["play\_button"] = btn\_play
--Runtime:addEventListener( "enterFrame", scriptengine.scriptAction );
end
And then I have something like this in the scriptengine.scripAction function (simplified, as it’s a big module):
local sceneGroup
local restartFlag
function scriptAction(event)
if restartFlag == true then
restartFlag = false
tStart = event.time
sceneGroup = ((mainGroup[1])[2])[1]
print ("No of children: " .. sceneGroup.numChildren)
end
end
The idea here is to add a “start” button to my already prepared scene. Clicking the button will enable the event listener and the action will start. Problem is that if I enable the event listener when the button is clicked (first commented-out statement), I get this error:
scriptengine.lua:124: attempt to index upvalue ‘sceneGroup’ (a nil value)
But if I enable the event listener when I start the scene (second commented-out statement), everything works just fine. If I change the assignment of sceneGroup in the scriptAction function to this, it works fine again:
sceneGroup = ((mainGroup[1])[1])[1]
So the whole display group hiearchy have changed just because I enabled an event listener in a different place in my code. Is there a logical explanation to this?
/Leif
[import]uid: 10329 topic_id: 3867 reply_id: 12274[/import]