Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Redirection to another lua page after downloading? — Gideros Forum

Redirection to another lua page after downloading?

loves_oiloves_oi Member
edited August 2012 in General questions
I built a login page using Mathz's code. If the user enters correct username and password , then user's name is written on the simulator, if not it says "Wrong username or password" . Everything is Okay until here. But i want to redirect the user to another page if he enters correct username and password. How can i do it?
This is main.lua
	local keyboard = KeyBoard.new("en_GB")
	keyboard:Create()
 
	local label1 = TextField.new(nil,"Username : ")
	label1:setPosition(40, 45)
	label1:setTextColor(0xFF1300)
	stage:addChild(label1)
 
	local label2 = TextField.new(nil,"Password")
	label2:setPosition(40, 100)
	label2:setTextColor(0xFF1300)
	stage:addChild(label2)
 
	local inputbox = InputBox.new(150,20,150,40)
	inputbox:setText("")
	inputbox:SetKeyBoard(keyboard)
	inputbox:setBoxColors(0xefefef,0xff2222,0,1)
	inputbox:setActiveBoxColors(0xff5555,0xff2222,0,1)
	stage:addChild(inputbox)
 
	local inputbox2 = InputBox.new(150,80,150,40)
	inputbox2:setText("")
	inputbox2:SetKeyBoard(keyboard)
	inputbox2:setBoxColors(0xefefef,0xff2222,0,1)
	inputbox2:setActiveBoxColors(0xff5555,0xff2222,0,1)
	stage:addChild(inputbox2)
 
 
	stage:addChild(keyboard)
 
local a = TextField.new(nil,"Loading")
 
local upp = Bitmap.new(Texture.new("button.png"))
local down = Bitmap.new(Texture.new("application lifecycle.png"))
 
-- create the button
local button = Button.new(upp, down)
button:setPosition(200, 120)
stage:addChild(button)
nameTaken = ""
local click = 0
button:addEventListener("click", 
	function() 
		if inputbox:getText() == "test" then
			inputbox:setText("done")
		else
			nameTaken = inputbox:getText()
			passwordTaken = inputbox2:getText()
			print (nameTaken)
			print (passwordTaken)
 
			print (nameTaken .. "---")
 
			local loader = UrlLoader.new("<a href="http://localhost/checklogin.php?myusername=&quot" rel="nofollow">http://localhost/checklogin.php?myusername=&quot</a>; .. nameTaken .. "&mypassword=" .. passwordTaken)
 
			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
 
				a:setText(echoString)
				a:setPosition(10, 185)
				a:setTextColor(0x007B25)
				stage:addChild(a)
 
				print (echoString)
 
				loader:removeEventListener(Event.COMPLETE, onComplete)
			end
 
			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	
 
	end) --end of function which exist in button:addEventListener


checklogin.php
 
<?php
session_start();
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name
 
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
 
// username and password sent from form 
$myusername=$_GET['myusername']; 
$mypassword=$_GET['mypassword']; 
 
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
 
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
 
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
 
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
echo "Welcome Mr. " . $myusername ;
//session_register("myusername");
//session_register("mypassword"); 
//header("location:login_success.php");
 
}
else {
echo "Wrong Username or Password";
}
?>

Thanks in advance.

Comments

  • I would just send back a string containing the name of the screen you want to go to and then in your response function simply do a string compare against known values and then jump to the appropriate screen.

    A good scene manager class is useful here - have a look at the game template example to see what I mean.

    Simples :)
    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
  • I didn't understand what you mean . I've uploaded the screenshots.
    After logging successfully , for example , i want user.lua page to open on the simulator.

    for what reason should i use "Scenemanager class " . What can i do with it? I looked at "Scenemanager class" but i can't find what it can do for me
    resim.png
    1360 x 880 - 158K
  • I think you are looking at the problem too much like a web solution. In this case, you don't need a new page, you need a new "scene". All that matters is what is drawn on the screen currently. The Scene Manager class makes it easy to switch between scenes, so you could have a "Login" scene and a "Home" screen for when they are logged in.

    Likes: loves_oi

    +1 -1 (+1 / -0 )Share on Facebook
  • techdojotechdojo Guru
    Accepted Answer
    Exactly - think of each scene as a different "page", you have some high level control logic that displays the correct "page" (a scene / page is usually derived from a Gideros Sprite) (ie the Scene manager class - if the scene manager is a sprite, then each page is usually associated with a sprite and you set the corresponding sprite to be visible (and all others invisible) to show the current page).

    When your app starts the login "page" is active, then when you get a response from the server you can decide to change to the home "page" by sending a message to the scene / page manager.

    Likes: loves_oi

    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
    +1 -1 (+1 / -0 )Share on Facebook
  • That's the module I was referring to initially
    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
  • i'm studying the Game Template example and scenemanager.lua but it's so complex and difficult to understand for me.There are many classes and files in Game Template. And in scenemanager.lua there exist functions such as moveFromRight,moveFromLeft,overFromRight,overFromLeft,moveFromRightWithFade,moveFromLeftWithFade,overFromRightWithFade,overFromLeftWithFade,moveFromBottom,moveFromTop,overFromBottom,overFromTop,moveFromBottomWithFade,moveFromTopWithFade,overFromBottomWithFade ....
    I have no idea about all these files . It so complex for beginners :( I'm trying to simplify it and deleting some classes or some functions but it breaks.
    This circumstance discourages me so much.I can't do anything. Please help :(
  • I'd start by looking at http://www.giderosmobile.com/blog/2011/11/17/gideros-scene-manager/ for more description of SceneManager and its transitions. You can download the example and play with that to find out what it does and then try altering some of the transitions to see what happens. Nothing beats having a good play when trying to get the hang of things! :)

    Likes: techdojo, loves_oi

    +1 -1 (+2 / -0 )Share on Facebook
  • loves_oiloves_oi Member
    edited August 2012
    OK . I carefully looked at these codes which you suggest .Now i have an idea. When the user enters his username and password , if it is valid , i will post True to Lua page .I will put if statement and if it is true , it will direct to another page. These are only ideas now. I hope it works.

    For today, i only want to redirect it to a scene page when it is admin .But it doesn't work.

    this is main.lua:
     
    		local keyboard = KeyBoard.new("en_GB")
    	keyboard:Create()
     
    	local label1 = TextField.new(nil,"Username : ")
    	label1:setPosition(40, 45)
    	label1:setTextColor(0xFF1300)
    	stage:addChild(label1)
     
    	local label2 = TextField.new(nil,"Password")
    	label2:setPosition(40, 100)
    	label2:setTextColor(0xFF1300)
    	stage:addChild(label2)
     
    	local inputbox = InputBox.new(150,20,150,40)
    	inputbox:setText("")
    	inputbox:SetKeyBoard(keyboard)
    	inputbox:setBoxColors(0xefefef,0xff2222,0,1)
    	inputbox:setActiveBoxColors(0xff5555,0xff2222,0,1)
    	stage:addChild(inputbox)
     
    	local inputbox2 = InputBox.new(150,80,150,40)
    	inputbox2:setText("")
    	inputbox2:SetKeyBoard(keyboard)
    	inputbox2:setBoxColors(0xefefef,0xff2222,0,1)
    	inputbox2:setActiveBoxColors(0xff5555,0xff2222,0,1)
    	stage:addChild(inputbox2)
     
    	--local scene = "scene1"
     
    	stage:addChild(keyboard)
     
    	local a = TextField.new(nil,"Loading")
     
    	local upp = Bitmap.new(Texture.new("button.png"))
    	local down = Bitmap.new(Texture.new("application lifecycle.png"))
     
    	-- create the button
    	local button = Button.new(upp, down)
    	button:setPosition(200, 120)
    	stage:addChild(button)
     
    	nameTaken = ""
    	local click = 0
     
    	local sceneManager = SceneManager.new({
    					["scene1"] = Scene1,	
    				})
     
    	local scenes = {"scene1"}
     
    button:addEventListener("click", 
    	function() 
    		if inputbox:getText() == "test" then
    			inputbox:setText("done")
    		else
    			nameTaken = inputbox:getText()
    			passwordTaken = inputbox2:getText()
    			print (nameTaken)
    			print (passwordTaken)
     
    			print (nameTaken .. "---")
     
    			local loader = UrlLoader.new("<a href="http://localhost/checklogin.php?myusername=&quot" rel="nofollow">http://localhost/checklogin.php?myusername=&quot</a>; .. nameTaken .. "&mypassword=" .. passwordTaken)
     
    			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
     
    				a:setText(echoString)
    				a:setPosition(10, 185)
    				a:setTextColor(0x007B25)
    				stage:addChild(a)
     
    				print (echoString)
     
    				loader:removeEventListener(Event.COMPLETE, onComplete)
     
     
     
    				if(nameTaken == 'admin') then
    					local transition = transitions
    					sceneManager:changeScene(scene1, 1, transition, easing.outQuadratic, { eventFilter={Event.MOUSE_DOWN} } ) 
    					stage:addChild(sceneManager)
    				end	
    			end
     
    			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	
     
    	end) --end of function which exist in button:addEventListener
    ERROR IS :
    scenemanager.lua:264: attempt to index field '?' (a nil value)
    stack traceback:
    scenemanager.lua:264: in function 'changeScene'
    main.lua:92: in function

    Thanks in advance
  • You should pass scene name as string:
    sceneManager:changeScene("scene1", 1, transition, easing.outQuadratic, { eventFilter={Event.MOUSE_DOWN} } )
  • loves_oiloves_oi Member
    edited September 2012
    ok . i edited it. But now , i'm taking the error when i submit the username and password :
    scenemanager.lua:277: attempt to index field '?' (a nil value)
    stack traceback:
    scenemanager.lua:277: in function 'changeScene'
    main.lua:94: in function



    The lines 277-279 are:
    277   self.scene2 = self.scenes[scene].new(options and options.userData)
    278	self.scene2:setVisible(false)
    279	self:addChild(self.scene2)
  • @loves_oi
    well this error looks quite similar. Which transition do you use, maybe this transition requires to have 2 scenes (from and to)?
  • loves_oiloves_oi Member
    edited September 2012
    @ar2rsawseen ; i don't know actualy. I got it from "Gideros Scene Manager Project". i have added the project.You can try it by submitting username 'a' .
    zip
    zip
    version 1.4.zip
    16K
  • loves_oiloves_oi Member
    edited September 2012
    i have updated the file. Now i'm taking error:
    scenemanager.lua:325: attempt to perform arithmetic on field 'duration' (a nil value)
    stack traceback:
    scenemanager.lua:325: in function
    the line is:
    325 local t = (self.duration == 0) and 1 or (self.time / self.duration)
    reason is (self.time / self.duration)
    because t is null.

    t is null because duration is null.In function changeScene , duration is 1 at first call , but then it becomes Null.Why??

    This is changeScene function:
     
    function SceneManager:changeScene(scene, duration, transition, ease, options)
     
    print ("b")
    print (duration)
     
    	self.eventFilter = options and options.eventFilter
     
    	if self.tweening then
    		print ("100")
    		return
    	end
    		print ("200")
    	if self.scene1 == nil then
    		print ("300")
    		self.scene1 = self.scenes[scene].new(options and options.userData)
    		self:addChild(self.scene1)
    		dispatchEvent(self, "transitionBegin")
    		dispatchEvent(self.scene1, "enterBegin")
    		dispatchEvent(self, "transitionEnd")
    		dispatchEvent(self.scene1, "enterEnd")
    		return
    	end
    		print ("400")
     
    	print ("a")
    	print (duration)
     
    	self.duration = duration
    	self.transition = transition
    	self.ease = ease or defaultEase
     
    	self.scene2 = self.scenes[scene].new(options and options.userData)
    	self.scene2:setVisible(false)
    	self:addChild(self.scene2)
     
    	self.time = 0
    	self.currentTimer = os.timer()
    	self.tweening = true
    end
    This is result:
    Welcome Mr. a
    b
    1
    200
    300
    scene1 - enter begin
    scene1 - enter end
    b
    nil
    200
    400
    a
    nil
    scene1 - exit begin
    scene1 - enter begin
    scenemanager.lua:335: attempt to perform arithmetic on field 'duration' (a nil value)
    stack traceback:
    	scenemanager.lua:335: in function <scenemanager.lua:318>
  • @loves_oi: in your main.lua file @ line 89 you have this code
    	if(nameTaken == 'a') then
    		local transition = transitions
    		sceneManager:changeScene("scene1", 1, transition, easing.outQuadratic, { eventFilter={Event.MOUSE_DOWN} } ) 
    		stage:addChild(sceneManager)
    		sceneManager:changeScene("scene1") -- This line is your problem. It isn't needed and is also missing parameters
    	end

    Likes: loves_oi

    +1 -1 (+1 / -0 )Share on Facebook
  • loves_oiloves_oi Member
    edited September 2012
    Ok . In examples , texture is added to the self. But isn't it possible to add TextField to the page?When i tried it , It gives error..
Sign In or Register to comment.