Groups - hide one and show others

I’m creating 2 groups when a scene is loaded. I only want one to show on screen, and then based on certain actions the second group may be shown later on.

The two groups are loading and form. So I show ‘loading’ (a little animation) while I check for something remote, and then show ‘form’ if I can’t find the remote resource.

To do this I would have assumed that I just add ‘loading’ to my group. However, it seems they both show on screen regardless.

I can get the desired effect by setting form.isVisible = false; but this seems a little bit wrong to me. I don’t want to set it as invisible as such, I just don’t want it to load in the first place. Is what I’m doing right here, or should I be seeking an alternative?

Full code here. Thanks:
[lua]local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local widget = require( “widget” )



– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view

– create a white background to fill screen
local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
bg:setFillColor( 80, 130, 200 ) – white

– Welcome
local title = txt.newEmbossed(“Welcome”, 10, 60, “Helvetica”, 32)
title:setTextColor( 255 ) – white
title:setReferencePoint( display.CenterReferencePoint )
title.x = display.contentWidth * 0.5
title.y = 75

– Welcome
local login = txt.newEmbossed(“Please login to begin.”, 10, 60, “HelveticaNeue”, 22)
login:setTextColor( 255 ) – white
login:setReferencePoint( display.CenterReferencePoint )
login.x = display.contentWidth * 0.5
login.y = 120

–[[FORM]]–
– Create new group for form
local form = display.newGroup();

– Labels and fields
local lblEmail = txt.new(“Email”, 40, 230, native.systemFont, 18)
local txtEmail = native.newTextField( 40, 255, 240, 36 )
txtEmail.font = native.newFont( native.systemFont, 22 )
local lblPassword = txt.new(“Password”, 40, 300, native.systemFont, 18)
local txtPassword = native.newTextField( 40, 325, 240, 36 )
txtPassword.font = native.newFont( native.systemFont, 22 )

local submit = widget.newButton{ label=“Login” }
submit.x = display.contentWidth * 0.5; submit.y = 400

– button

form:insert( lblEmail )
form:insert( txtEmail )
form:insert( lblPassword )
form:insert( txtPassword )
form:insert( submit )
–[[EO FORM]]–

–[[LOADING ANIMATION]]–
local loading = display.newGroup();

– Actual loading animation
local imgLoading = display.newImage( “images/loading.png” )
imgLoading.x = display.contentWidth * 0.5; imgLoading.y = 300

loading:insert(imgLoading)
–[[EO LOADING ANIMATION]]–

– all objects must be added to group (e.g. self.view)
group:insert( bg )
group:insert( title )
group:insert( login )
group:insert( loading )
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view

– Do nothing
end

– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view

– INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view

– INSERT code here (e.g. remove listeners, remove widgets, save state variables, etc.)

end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene[/lua] [import]uid: 61422 topic_id: 25464 reply_id: 325464[/import]

I think you could try this (just one way to do it) :

  1. just below local widget = require( “widget” ) declare each of the variable/members of the form as well as the form itself:

– declare you main display group here also, and initiate it in onCreateScene ‘group = self.view’
lcoal group
local lblEmail
local txtEmail
local lblPassword
local txtPassword
local form

  1. in the ‘onEnterScene function’ create an event listener

local function checkForSomethingRemote(event)
– run your code to check whatever, if it returns false then create the form here
– NOTE: i took out the local here as you have declared each of these outside the function

– place all this code inside some loop where you are checking whatever remote value you are looking for
if remote = false then

form = display.newGroup();

– Labels and fields
lblEmail = txt.new(“Email”, 40, 230, native.systemFont, 18)
txtEmail = native.newTextField( 40, 255, 240, 36 )
txtEmail.font = native.newFont( native.systemFont, 22 )
lblPassword = txt.new(“Password”, 40, 300, native.systemFont, 18)
txtPassword = native.newTextField( 40, 325, 240, 36 )
txtPassword.font = native.newFont( native.systemFont, 22 )

submit = widget.newButton{ label=“Login” }
submit.x = display.contentWidth * 0.5; submit.y = 400

– button

form:insert( lblEmail )
form:insert( txtEmail )
form:insert( lblPassword )
form:insert( txtPassword )
form:insert( submit )

– insert form into main group
group:insert(form)

– now remove eventlistener
Runtime:removeEventListener(“enterFrame”, checkForSomethingRemote)
elseif remote = false then
Runtime:removeEventListener(“enterFrame”, checkForSomethingRemote)
end

end – end of checkForSomethingRemote

Runtime:addEventListener(“enterFrame”, checkForSomethingRemote)

  1. in the ‘onExitScene’ function be sure to remove event listener IF IT HAS NOT ALREADY BEEN REMOVED by the checkForSomethingRemote function
    need to remove form from maingroup and ‘nil’ all the lbl and txt variables etc that were memebers of the form.

Also, not sure you why you create a ‘local group = self.view’ in each function. Usually just in the onCreateScene function, or sometimes I see it declared outside the functions, but never in each function as such.

Of course this is just my quick thought on it. Not taking into effect if this is a scene you are frequently returning to or not, as well as when and where you are getting what ever remote data that seems critical to if/when you want the form to show. But this should get you on the right path. I hope it helps!

[import]uid: 35148 topic_id: 25464 reply_id: 102902[/import]