Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
problem in scenemanager.lua — Gideros Forum

problem in scenemanager.lua

loves_oiloves_oi Member
edited September 2012 in General questions
This is main.lua
sceneManager = SceneManager.new({
 
	["optionScreen"] = optionScreen,
	["results"] = results, 
})
local scenes = {"optionScreen", "results",}
 
sceneManager:addEventListener("transitionBegin", function() print("manager - transition begin") end)
sceneManager:addEventListener("transitionEnd", function() print("manager - transition end") end)
 
--add manager to stage
stage:addChild(sceneManager)
 
 
 transitions = {
	SceneManager.moveFromLeft,
	SceneManager.moveFromRight,
	SceneManager.moveFromBottom,
	SceneManager.moveFromTop,
	---and others ....
}
 
 
--start start scene
sceneManager:changeScene("optionScreen", 1, SceneManager.flipWithFade, easing.outBack)
This is the optionScreen.lua (different from original.I changed the original optionScreen.lua )
optionScreen = Core.class(Sprite)
 
local imgBase = getImgBase()
local font = getFont()
 
local checkX = 60
local sliderX = 240
local gap = 70
 
local col = {r=243/255,g=206/255,b=0,a=1}
 
local soundSlider
local musicSlider
local redSlider
function optionScreen:init()
	local screen = Bitmap.new(Texture.new(imgBase.."opt_back.png"))
	self:addChild(screen)
	screen:setPosition(0,0)	
	local txtY = 100
 
	local loader = UrlLoader.new("<a href="http://localhost/write.php&quot" rel="nofollow">http://localhost/write.php&quot</a><img class="emoji" src="http://forum.giderosmobile.com/resources/emoji/wink.png" title=";)" alt=";)" height="20" />
 
	local function onComplete(event)
		local myString=event.data      -- We got the string here.
 
		local length = string.len(myString)
		local i = 1
		local echoString = ""
		while (i < length+1) do
 
			echoString = echoString .. string.sub(myString,i,i)
 
			i = i+1;
		end
		print (echoString)
		---definition of functions how_many() and getH()	
 
 
		h = how_many(echoString,'\n')
 
		i=1
		k=0
		local tit = ""
		local taken = {}
		local theText = {}
		while (k < h)
		do
			theText[k] = TextField.new(nil, "placeholder")
			theText[k]:setPosition(150,125+k*50)
			theText[k]:setTextColor(0x007B25)
			theText[k]:setScaleX(5)  
			theText[k]:setScaleY(5)
			stage:addChild(theText[k])
 
                         ----here, we found taken[1],taken[2],taken[3]
 
			print (taken[1],taken[2],taken[3])
			theText[k]:setText(taken[2])	
			k = k + 1
		end	--end of while loop
		--]]
 
	index = h
	cbox = {}
	k = 0
	print("h = " .. h)
	print("index = " .. index)
	while(index>k)
	do
		print (k)
		k = k + 1
	end
 
	gonder = {}
 
	--Checkboxes are visible thanks to the below codes
	cbox[1] = checkBox.new(checkX, txtY, "Music", col,
		function(this)
			local state = this:getCheck()
			if state then 
				print("1 checked ")
				gonder[1] = true
			else 
				print("1 not checked ")
				gonder[1] = false
			end
		end
	)
	screen:addChild(cbox[1])
	cbox[1]:setCheck(settings.getMusicState())
 
	txtY = txtY + gap
	-- Sound On / Off
	cbox[2] = checkBox.new(checkX, txtY, "Sound FX", col, 
		function(this)
			local state = this:getCheck()
			if state then 
				print("2 checked")
				gonder[2] = true
			else 
				print("2 not checked")
				gonder[2] = false
			end
		end	
	)
	screen:addChild(cbox[2])
	cbox[2]:setCheck(settings.getSoundState())
 
	txtY = txtY + gap
	-- Autosave On / Off
	cbox[3] = checkBox.new(checkX, txtY, "Autosave", col, 
		function(this)
			local state = this:getCheck()
			if state then 
				print("3 checked") 
				gonder[3] = true
			else 
				print("3 not checked")
				gonder[3] = false
			end
		end
	)
	screen:addChild(cbox[3])
	cbox[3]:setCheck(settings.getAutosaveState())
 
	txtY = txtY + gap
 
	local submitt = Button.new(Bitmap.new(Texture.new(imgBase .. "button_up.png")), Bitmap.new(Texture.new(imgBase .. "button_down.png")))
	submitt:setPosition(20, 170)
	stage:addChild(submitt)
 
 
 
	submitt:addEventListener("click", 
	function()	
		local transition = transitions
		sceneManager:changeScene("results", 1, transition, easing.outBack, { eventFilter={Event.MOUSE_DOWN} } ) 
	end)
 
 
	self:removeEventListener("exitEnd", self.onExitEnd)	
 
	end --end of function onComplete
 
 
 
	--------------------------
	local function onError()
    print("error")
	end
 
	local function onProgress(event)
		print("progress: " .. event.bytesLoaded .. " of " .. event.bytesTotal)
	end
	loader:addEventListener(Event.COMPLETE, onComplete)
	loader:addEventListener(Event.ERROR, onError)
	loader:addEventListener(Event.PROGRESS, onProgress)
 
 
 
end
 
-- Let's get rid of all of the elements shall we. This is strictly not required
-- but I thought I'd put it in for completeness
function optionScreen:onExitEnd()
	self:removeEventListener("exitEnd", self.onExitEnd)
	redSlider:onExitEnd()
	greenSlider:onExitEnd()
	blueSlider:onExitEnd()
	musicSlider:onExitEnd()
	soundSlider:onExitEnd()
 
	soundChk:onExitEnd()
	musicChk:onExitEnd()
	autosaveChk:onExitEnd()
 
	collectgarbage()
end
 
 
function optionScreen:adjustRed(this) 
	col.r = (redSlider:getPos()*2.55)/255 
	redSlider:setCol({r=col.r,g=0,b=0,a=1})
	musicSlider:setCol(col)
	soundSlider:setCol(col)
	autosaveChk:setCol(col)
	soundChk:setCol(col)
	musicChk:setCol(col)
end
 
function optionScreen:adjustGreen(this) 
	col.g = (greenSlider:getPos()*2.55)/255 
	greenSlider:setCol({r=0,g=col.g,b=0,a=1})
	musicSlider:setCol(col)
	soundSlider:setCol(col)
	autosaveChk:setCol(col)
	soundChk:setCol(col)
	musicChk:setCol(col)
end
 
function optionScreen:adjustBlue(this) 
	col.b = (blueSlider:getPos()*2.55)/255 
	blueSlider:setCol({r=0,g=0,b=col.b,a=1})
	musicSlider:setCol(col)
	soundSlider:setCol(col)
	autosaveChk:setCol(col)
	soundChk:setCol(col)
	musicChk:setCol(col)
end
This is results.lua
 
results = gideros.class(Sprite)
 
function results:init(t)
	if t then
		print("results: ", t)
	end	
 
	self:addChild(Bitmap.new(Texture.new("resources/images/results.jpg")))
 
	self:addEventListener("enterBegin", self.onTransitionInBegin, self)
	self:addEventListener("enterEnd", self.onTransitionInEnd, self)
	self:addEventListener("exitBegin", self.onTransitionOutBegin, self)
	self:addEventListener("exitEnd", self.onTransitionOutEnd, self)
end
 
function results:onTransitionInBegin()
	print("results - enter begin")
end
 
function results:onTransitionInEnd()
	print("results - enter end")
end
 
function results:onTransitionOutBegin()
	print("results - exit begin")
end
 
function results:onTransitionOutEnd()
	print("results - exit end")
end
I'm taking the error:

sources/classes/scenemanager.lua:286: attempt to index field '?' (a nil value)
stack traceback:
sources/classes/scenemanager.lua:286: in function 'changeScene'
sources/screens/optionScreen.lua:202: in function

This is changeScene function of scenemager.lua
 
function SceneManager:changeScene(scene, duration, transition, ease, firsttime)
	if self.tweening then
		return
	end
 
	if self.scene1 == nil then
		self.scene1 = self.scenes[scene].new(firsttime)
		self:addChild(self.scene1)
		dispatchEvent(self, "transitionBegin")
		dispatchEvent(self.scene1, "enterBegin")
		dispatchEvent(self, "transitionEnd")
		dispatchEvent(self.scene1, "enterEnd")
		return
	end
 
	self.duration = duration
	self.transition = transition
	self.ease = ease or defaultEase
 
286.	self.scene2 = self.scenes[scene].new(firsttime)
287	self.scene2:setVisible(false)
	self:addChild(self.scene2)
 
	self.time = 0
	self.currentTimer = os.timer()
	self.tweening = true
end
This is the lines between 199-203 of optionScreen.lua
submitt:addEventListener("click", 
	function()	
201		local transition = transitions
202		sceneManager:changeScene("results", 1, transition, easing.outBack, { eventFilter={Event.MOUSE_DOWN} } ) 
	end)
Why i'm taking error? How to prevent it ?
Thanks in advance.

Comments

  • Try to comment out all the code in the results:init (t) function and check again.
    If not help, then maybe it's the Code Dependency. Make optionScreen.lua Depends on result.lua

    Likes: loves_oi

    +1 -1 (+1 / -0 )Share on Facebook

  • If not help, then maybe it's the Code Dependency. Make optionScreen.lua Depends on result.lua
    How to make optionScreen.lua depend on result.lua ?
    And what is the possible reason of the error : attempt to index field '?' (a nil value)
    stack traceback:
  • self.scene2 = self.scenes[scene].new() //scenemanager.lua
    The problem is that self.scenes["result"] is not defined - it's nil. So you are trying to do nil.new(). So error in the result class, or code dependencies.

    Code dependency:
    In Gideros Studio Right click on optionScreen.lua -> "Code dependencies...". And then check "result.lua"

    Likes: loves_oi

    +1 -1 (+1 / -0 )Share on Facebook
  • loves_oiloves_oi Member
    edited September 2012

    Code dependency:
    In Gideros Studio Right click on optionScreen.lua -> "Code dependencies...". And then check "result.lua"
    After doing that , it works . But i want to prevent this dependency because i need the reverse of this dependency ,namely optionScreen.lua should work before result.lua. I want this because i make calculations in options.lua and sending the results of this calculations to the result.lua
  • loves_oiloves_oi Member
    edited September 2012
    Should i do something here?

    Inside of optionScreen.lua
    submitt:addEventListener("click", 
    	function()	
    		sceneManager:changeScene("results", 1, SceneManager.flipWithFade, easing.outBack)
     
    	end)
    After clicking ,for example ,i want to send "Hello world" to the result.lua and then i will print "Hello world" in the screen of result.lua . But the text which will be sent is determined in optionScreen.lua
  • ScouserScouser Guru
    edited September 2012
    ^^^oops Ninja'd

    @loves_oi: One of the first things I think you should do is use the up to sate SceneManager.lua that comes with GiderosStudio. I notice from reading the sample above that you are using quite an old version of SceneManager.lua. The reason I know this is that it is one I myself modified a long time ago to add a parameter when I called the scene:init() function.

    The new SceneManager.lua in the very least should be used for compatibility reasons.

    ... Removed as I was wrong ...

    *edit*
    I see from your recent post that results is actually a scene. Maybe then I was quite correct & you should use the latest SceneManager.lua and pass the results see this link for more info

  • ....namely optionScreen.lua should work before result.lua. I want this because i make calculations in options.lua and sending the results of this calculations to the result.lua
    I think, you misunderstood the Code Dependency.

    Use SceneManager v1.0.4

    +

    submitt:addEventListener("click",
    function()
    local transition = transitions
    sceneManager:changeScene("results", 1, transition, easing.outBack, "HELLO, WORLD" )
    end)

    +

    In Gideros Studio Right click on optionScreen.lua -> "Code dependencies...". And then check "result.lua"
  • Sorry, instead of
    "sceneManager:changeScene("results", 1, transition, easing.outBack, "HELLO, WORLD" ) "
    should be

    "options = Core.class()
    options.userData = "Hello, World"
    sceneManager:changeScene("results", 1, transition, easing.outBack, options) "

  • loves_oiloves_oi Member
    edited September 2012
    ^^^oops Ninja'd

    @loves_oi: One of the first things I think you should do is use the up to sate SceneManager.lua that comes with GiderosStudio. I notice from reading the sample above that you are using quite an old version of SceneManager.lua. The reason I know this is that it is one I myself modified a long time ago to add a parameter when I called the scene:init() function.

    The new SceneManager.lua in the very least should be used for compatibility reasons.

    ... Removed as I was wrong ...

    *edit*
    I see from your recent post that results is actually a scene. Maybe then I was quite correct & you should use the latest SceneManager.lua and pass the results see this link for more info
    I don't see any scenemanager which comes with Gideros setup :S
    But it is the sceneManager.lua which i am using :
    --[[
     
    .......
     
    function SceneManager:init(scenes)
    	self.scenes = scenes
    	self.tweening = false
    	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    end
     
    function SceneManager:changeScene(scene, duration, transition, ease, firsttime)
     
    	if self.tweening then
    		return
    	end
     
    	if self.scene1 == nil then
    		self.scene1 = self.scenes[scene].new(firsttime)
    		self:addChild(self.scene1)
    		dispatchEvent(self, "transitionBegin")
    		dispatchEvent(self.scene1, "enterBegin")
    		dispatchEvent(self, "transitionEnd")
    		dispatchEvent(self.scene1, "enterEnd")
    		return
    	end
     
    	self.duration = duration
    	self.transition = transition
    	self.ease = ease or defaultEase
    	self.scene2 = self.scenes[scene].new(firsttime)
    	self.scene2:setVisible(false)
    	self:addChild(self.scene2)
     
    	self.time = 0
    	self.currentTimer = os.timer()
    	self.tweening = true
    end
     
    function SceneManager:onEnterFrame(event)
    	if not self.tweening then
    		return
    	end
     
    	if self.time == 0 then
    		self.scene2:setVisible(true)
    		dispatchEvent(self, "transitionBegin")
    		dispatchEvent(self.scene1, "exitBegin")
    		dispatchEvent(self.scene2, "enterBegin")
    	end
     
    	local timer = os.timer()
    	local deltaTime = timer - self.currentTimer
    	self.currentTimer = timer
     
    	self.time = self.time + deltaTime
     
    	if self.time > self.duration then
    		self.time = self.duration
    	end
     
    	local t = (self.duration == 0) and 1 or (self.time / self.duration)
     
    	self.transition(self.scene1, self.scene2, self.ease(t))
     
    	if self.time == self.duration then
    		dispatchEvent(self, "transitionEnd")
    		dispatchEvent(self.scene1, "exitEnd")
    		dispatchEvent(self.scene2, "enterEnd")
     
    		self:removeChild(self.scene1)
    		self.scene1 = self.scene2
    		self.scene2 = nil
    		self.tweening = false
     
    		collectgarbage()
    	end
    end
    Is this new or old?
  • i call it as this , in options.lua
    sceneManager:changeScene("results", 1, SceneManager.flipWithFade, easing.outBack,{x=sendRows[1],xt=gonder[1],y=sendRows[2],yt=gonder[2],z=sendRows[3],zt=gonder[3]})

    and in results.lua i want to take the size of the array -last parameter of the funct calling above- ({x=sendRows[1],xt=gonder[1],y=sendRows[2],yt=gonder[2],z=sendRows[3],zt=gonder[3]}) it should return 6 but it returns zero.

    I tried calling as this:
    sceneManager:changeScene("results", 1, SceneManager.flipWithFade, easing.outBack,{params = {x=sendRows[1],xt=gonder[1],y=sendRows[2],yt=gonder[2],z=sendRows[3],zt=gonder[3]}})
    but it didn't give the correct answer ,too. What should i do?
  • OK . I solved . When i did :
    sceneManager:changeScene("results", 1, SceneManager.flipWithFade, easing.outBack,params = {sendRows[1],gonder[1],sendRows[2],gonder[2],sendRows[3],gonder[3]})
    it worked.Interesting :)
  • loves_oiloves_oi Member
    edited September 2012
    This code works perfectly , but it is for only 3 rows. But we don't know how many rows there are in the database. I want to do it for all Rows.Am i have to do it with a loop?
    	cbox[1] = checkBox.new(checkX, txtY, sendRows[1], col,
    		function(this)
    			local state = this:getCheck()
    			if state then 
    				print("1 checked")
    				gonder[1] = 1
    			else 
    				print("1 isn't checked")
    				gonder[1] = 0
    			end
    		end
    	)
    	screen:addChild(cbox[1])
    	cbox[1]:setCheck(true)
    	txtY = txtY + gap
    	print ("gonder[1] = " .. gonder[1])
     
     
     
    	cbox[2] = checkBox.new(checkX, txtY, sendRows[2], col, 
    		function(this)
    			local state = this:getCheck()
    			if state then 
    				print("2 checked")
    				gonder[2] = 1
    			else 
    				print("2 isn't checked")
    				gonder[2] = 0
    			end
    		end	
    	)
    	screen:addChild(cbox[2])
    	cbox[k]:setCheck(true)
    	print ("gonder[2] = " .. gonder[2])
    	txtY = txtY + gap
     
     
    	cbox[3] = checkBox.new(checkX, txtY, sendRows[3], col, 
    		function(this)
    			local state = this:getCheck()
    			if state then 
    				print("3 checked") 
    				gonder[3] = 1
    			else 
    				print("3 isn't checked")
    				gonder[3] = 0
    			end
    		end
    	)
    	screen:addChild(cbox[3])
    	cbox[3]:setCheck(true)
    	print ("gonder[3] = " .. gonder[3])
    	txtY = txtY + gap
  • loves_oiloves_oi Member
    edited September 2012
    After exiting while loop , m equals rowNumber. After clicking any row , it doesn't know which row is clicked because m = rowNumber . How to solve this problem ?
    How to do this loop with success ?
     
    local m = 1
    while(m<rowNumber)
     
    	cbox[m] = checkBox.new(checkX, txtY, sendRows[m], col,
    		function(this)
    			local state = this:getCheck()
    			if state then 
    				print(m .. " checked")
    				gonder[m] = 1
    			else 
    				print(m  .. " does not checked")
    				gonder[m] = 0
    			end
    		end
    	)
    	screen:addChild(cbox[m])
    	cbox[m]:setCheck(settings.getMusicState())
    	txtY = txtY + gap
    end
    I think that : When i clicked checkboxes it falls into while loop . And it does not know what it does so it does nonsense things...
    What should i do?
    Thanks in advance
  • I 'm trying to do this . Because all the rows which exist in the table should be visible on the screen . What should i do ? In order to prevent it should i give up using loop? If i give up using loop , what can i do to print all rows??
  • @loves_oi - please don't take this the wrong way, I've seen a lot of posts from you in the last few days and it's obvious from them that your trying to learn lua by throwing yourself in the deep end, and it seems to me (at least) that your really struggling.

    I would *SERIOUSLY* recommend getting a hold of the Beginning Lua Programming book (http://www.amazon.co.uk/Beginning-Lua-Programming-Programmer/dp/0470069171/ref=sr_1_1?ie=UTF8&qid=1347350411&sr=8-1)

    and starting with some of the more simple examples, from what I can see your getting seriously mixed up with your closures, accessing upvalues, nested and anonymous functions and a lot of the more powerful (yet complex to use properly) lua concepts.

    Start at the beginning - and get a SOLID foundation in the basics of the language before you try the advanced concepts - which in a lot of cases can just as easily be implemented using the basics and at all times remember the KISS principal - the moment the code get's too unwieldy and difficult for you too read then it's time to scale back and consider a simpler implementation, there's nothing worse for breaking your enthusiasm than trying to bang in advanced code that you don't understand, which then doesn't work and you have no way to fix.

    You have to remember, most (if not all) of the forum members are NOT professional teachers and will post examples assuming you have a basic understanding or will guide you to places where you can find the knowledge through experimentation and a full understanding of the results that your getting.

    One last tip - if your ever in doubt as to what your code is doing or the path through which your code travels, add in LOT's of print() functions, these will allow you to trace the path of the code by comparing the contents of the output window with your program - this is what really allowed me to get an understanding of how lua deals with closures, callbacks, program flow etc.

    Hope this helps...

    Jon...
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • loves_oiloves_oi Member
    edited September 2012
    Thanks for your advices @techdojo ;) i have already put many many print in the code.There are so much that you can say me that i am a spender :D i have some lacks you are right but i don't have time to read all this book. After completing my project,maybe, i can read it :) I analysed examples which comes with Gideros and you masters' projects. I am trying to understand them and then doing somethings. I think i learned many things and still trying to learn but if you realize a point which i don't understand , please tell it to me ;)
    What about my question :) Did you find it very easy ? or , does it contain any advance point which i have to know to solve it?
    --edit--
    I determined the specific problem . But i can't find the solution. I updated the codes above which shows the specific problem
Sign In or Register to comment.