I had a problem creating a loading scene in between scenes, the solution I got was to use an overlay, I've been trying this for days but for some reason I can't get it to work. I have two scenes, one that shows a list of items, and another that shows a page of text based on which row was clicked. The problem now is the first scene, that shows the list, downloads images to display and of course this takes a while so whenever i transition into that screen, the app appears to be frozen while everything loads up.
I've found out that it does actually go into the overlay scene, since the print statements i put in it print out, but it just doesn't display anything and when i removed the code that hides the overlay after, it still appears to hang, it goes into the overlay scene, doesn't display anything and then renders the table before finally displaying the overlay so instead of showing an overlay, having everything load up beneath, and then hiding the overlay, it appears to be frozen in the current scene, loads up everything beneath, shows overlay and then hides overlay right after.
My code for the three scenes are below, the one I'm posting now is the only one that actually worked once and then never again, it showed the overlay while everything loaded like I wanted but for some reason, it never worked again after that one time. I'm getting really frustrated with it and no forum has given me a solution, I'd really really appreciate some help. Thanks!
ItemListPage.lua
local composer = require ( "composer" )
local widget = require( "widget" )
local json = require( "json" )
-- Load the relevant LuaSocket modules
local http = require( "socket.http" )
local ltn12 = require( "ltn12" )
local scene = composer.newScene()
--NavigationBar elements initiated
--Removed for readability
--image handler
local function networkListener( event )
if ( event.isError ) then
print ( "Network error - download failed" )
end
print ( "event.response.fullPath: ", event.response.fullPath )
print ( "event.response.filename: ", event.response.filename )
print ( "event.response.baseDirectory: ", event.response.baseDirectory )
end
local function onRowRender( event )
-- Get reference to the row group
local row = event.row
local params=event.row.params
local itemRow=3;
-- Cache the row "contentWidth" and "contentHeight" because the row bounds can change as children objects are added
local rowHeight = row.contentHeight
local rowWidth = row.contentWidth
row.rowTitle = display.newText( row, params.topic, 0, 0, nil, 14 )
row.rowTitle:setFillColor( 0 )
row.rowTitle.anchorX = 0
row.rowTitle.x = 0
row.rowTitle.y = (rowHeight/2) * 0.5
--Other elements removed for readabilty (it's all just text objects)
--Download Image
--params referring to items[i]
local imagelink =params.imagelink
-- Create local file for saving data
local path = system.pathForFile( params.imagename, system.TemporaryDirectory )
myFile = io.open( path, "w+b" )
-- Request remote file and save data to local file
http.request{
url = imagelink,
sink = ltn12.sink.file( myFile )
}
row.Image = display.newImageRect(row, params.imagename, system.TemporaryDirectory, 25, 25)
row.Image.x = 20
row.Image.y = (rowHeight/2) * 1.5
row:insert( row.rowTitle )
row:insert( row.Image )
end
local function onRowTouch( event )
local row = event.target
local params=event.target.params
composer.removeScene(composer.getSceneName("current"))
composer.gotoScene( "itempage" , {params=params})
end
function scene:create( event )
local sceneGroup = self.view
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == "will" then
-- Called when the scene is still off screen and is about to move on screen
--overlay
composer.showOverlay( "loading", { isModal = true })
elseif phase == "did" then
--Table stuff
local scrollBarOptions = {
sheet = scrollBarSheet, -- Reference to the image sheet
topFrame = 1, -- Number of the "top" frame
middleFrame = 2, -- Number of the "middle" frame
bottomFrame = 3 -- Number of the "bottom" frame
}
-- Table
local tableView = widget.newTableView(
{
left = 0,
top = navBar.height,
height = display.contentHeight-navBar.height,
width = display.contentWidth,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
listener = scrollListener
}
)
--json work
local filename = system.pathForFile( "items.json", system.ResourceDirectory )
local decoded, pos, msg = json.decodeFile( filename )
if not decoded then
print( "Decode failed at "..tostring(pos)..": "..tostring(msg) )
else
print( "File successfully decoded!" )
end
local items=decoded.items
-- create a white background to fill screen
local background = display.newRect( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight )
background:setFillColor( 1 ) -- white
sceneGroup:insert( background )
-- Insert rows
for i = 1, #items do
-- Insert a row into the tableView
print( "Adding a row!" )
tableView:insertRow{
rowHeight = 100,
rowColor = { default={ 0.8, 0.8, 0.8, 0.8 } },
lineColor = { 1, 0, 0 },
params=items[i]
}
end
sceneGroup:insert( tableView )
composer.hideOverlay( "fade", 100 )
end
end
-- other functions and elements unused and removed for readability
loading.lua
local composer = require ( "composer" )
local widget = require( "widget" )
local json = require( "json" )
local scene = composer.newScene()
-- Create the widget
function scene:create( event )
local sceneGroup = self.view
local background = display.newRect( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight )
background:setFillColor( 1 ) -- white
local text = display.newText( "Loading scene", 0, 0, nil, 14 )
text:setFillColor( 0 )
text.anchorX = display.contentCenterX
text.x = display.contentCenterX
text.y = display.contentCenterY
sceneGroup:insert( background )
sceneGroup:insert( text )
print ( "In loading create")
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == "will" then
elseif phase == "did" then
print ( "In loading show")
end
end
-- other functions and elements unused and removed for readability
ItemDisplayPage.lua
local composer = require ( "composer" )
local widget = require( "widget" )
local json = require( "json" )
local scene = composer.newScene()
--NavigationBar elements initiated
--This creates the "back button", when clicked it returns to the previous scene, in this case "itemListPage"
--it takes, no parameters
local function handleLeftButton( event )
if ( event.phase == "ended" ) then
composer.removeScene(composer.getSceneName("current"))
composer.gotoScene(composer.getSceneName("previous"))
end
return true
end
--Remaining navbar elements removed for readability
function scene:create( event )
local sceneGroup = self.view
local params=event.params
-- create a white background to fill screen
local background = display.newRect( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight )
background:setFillColor( 1 ) -- white
--creating header bar
local bar = display.newRect( navBar.height + (headerBarHeight*0.5), display.contentCenterY, display.contentWidth, headerBarHeight )
bar:setFillColor( 1 )
-- create stuff
local title = display.newText(params.topic, 0, 0, nil, 14 )
title:setFillColor( 0 )
title.anchorX = 0
title.x = margin
title.y = ((2*headerBarHeight/2) * 0.5)+navBar.height
local Image = display.newImageRect(params.imagename, system.TemporaryDirectory, 25, 25)
Image.x = 50
Image.y = display.contentCenterY
-- all objects must be added to group (e.g. self.view)
sceneGroup:insert( background )
sceneGroup:insert( title )
sceneGroup:insert( Image)
end
-- other functions and elements unused and removed for readability
I suggest you do not use scene.show event for loading.
Use timer.performWithDelay to load all data:
--in scene:show
elseif phase == "did" then
timer.performWithDelay(0, function()
local scrollBarOptions = {
--put your code here
composer.hideOverlay( "fade", 100 )
end)
Your current code didn't show overlay because engine waits for scene:show event before rendering anything. So rendering of overlay and images occured after all images are loaded.
In my code timer.performWithDelay doesn't block scene:show execution, so you will see overlay rendered before loading images
Related
I am trying to call my second scene and have the first disappear completely. The objects on the second scene are appearing, but they are overlapping those on the first scene. I've tried calling the scene.hide() and scene.destroy() functions manually and I've also tried adding objects to a group. Oddly enough, some of the objects weren't visible when added to a group, but others were. Please help! Also, sorry only some of the code is in the separate boxes, but I don't know how to fix it!
function scene:create( event )
local sceneGroup = self.view
display.setStatusBar( display.HiddenStatusBar )
local group = display.newGroup()
-------Set up green buttons
local greenButton3=display.newImageRect("images/green-unclicked.png", 520,60)
greenButton3.x=display.contentCenterX
greenButton3.y=(970)
greenButton3.button = 1
local greenButton2=display.newImageRect("images/green-unclicked.png", 520,60)
greenButton2.x=display.contentCenterX
greenButton2.y=(870)
greenButton2.button = 2
local greenButton1=display.newImageRect("images/green-unclicked.png", 520,60)
greenButton1.x=display.contentCenterX
greenButton1.y=(770)
greenButton1.button = 3
--------SET UP RED BUTTONS
local redButton1=display.newImageRect("images/red-dim.png",520,60)
redButton1.x=display.contentCenterX
redButton1.y=(40)
local redButton2=display.newImageRect("images/red-dim.png",520,60)
redButton2.x=display.contentCenterX
redButton2.y=(140)
local redButton3=display.newImageRect("images/red-dim.png",520,60)
redButton3.x=display.contentCenterX
redButton3.y=(240)
---------------TEXT
---Questions
local question1green=display.newText("How are you feeling?", 0,0,native.systemFontBold, 40)
question1green.x=display.contentCenterX
question1green.y=(705)
local question1red=display.newText("The other person is feeling...", 0,0,native.systemFontBold, 40)
question1red.x=display.contentCenterX
question1red.y=(300)
question1red.rotation = 180
---Answers
--Green
local answer1green=display.newText("Sad",0,0,native.systemFontBold, 40)
answer1green.x=display.contentCenterX
answer1green.y=(773)
local answer2green=display.newText("Angry",0,0,native.systemFontBold, 40)
answer2green.x=display.contentCenterX
answer2green.y=(870)
local answer3green=display.newText("I don't know",0,0,native.systemFontBold, 40)
answer3green.x=display.contentCenterX
answer3green.y=(973)
--Red
local answer1red=display.newText("Sad",0,0,native.systemFontBold, 40)
answer1red.x=display.contentCenterX
answer1red.y=(238)
answer1red.rotation = 180
local answer2red=display.newText("Angry",0,0,native.systemFontBold, 40)
answer2red.x=display.contentCenterX
answer2red.y=(141)
answer2red.rotation = 180
local answer3red=display.newText("Confused",0,0,native.systemFontBold, 40)
answer3red.x=display.contentCenterX
answer3red.y=(38)
answer3red.rotation = 180
--------CREATE EMPTY BODY OUTLINE
local bodyOutline=display.newImageRect("images/bodyoutline.png", 380,560)
bodyOutline.x=display.contentCenterX
bodyOutline.y=display.contentCenterY-10
bodyOutline.rotation = 270
--------move text to front after new buttons are created
function moveTextToFront (event)
answer1green:toFront()
answer2green:toFront()
answer3green:toFront()
answer1red:toFront()
answer2red:toFront()
answer3red:toFront()
end
------------------------------------------------------------
--When the Buttons are Clicked
------------------------------------------------------------
local function buttonClicked (event)
print("here")
--[[function destroyAll (event)
greenButton1:removeSelf()
greenButton1 = nil
greenButton2:removeSelf()
greenButton2 = nil
greenButton3:removeSelf()
greenButton3 = nil
redButton1:removeSelf()
redButton1 = nil
redButton2:removeSelf()
redButton2 = nil
redButton3:removeSelf()
redButton3 = nil
print("destroyed")
question1green:removeSelf()
question1green = nil
question1red:removeSelf()
question1red = nil
answer1green:removeSelf()
answer1green = nil
answer2green:removeSelf()
answer2green = nil
answer3green:removeSelf()
answer3green = nil
answer1red:removeSelf()
answer1red = nil
answer2red:removeSelf()
answer2red = nil
answer3red:removeSelf()
answer3red = nil
--]]
--end
--destroyAll()
------------------------------------------------------------
--Start of Results Creation (SETUP)
------------------------------------------------------------
function createGray1 (event)
local gray1=display.newImageRect("images/grayRSU.png",520,60)
gray1.x=display.contentCenterX
gray1.y=(970)
end
function createGray2 (event)
local gray2=display.newImageRect("images/grayRSU.png",520,60)
gray2.x=display.contentCenterX
gray2.y=(870)
end
function createGray3 (event)
local gray3=display.newImageRect("images/grayRSU.png",520,60)
gray3.x=display.contentCenterX
gray3.y=(770)
end
function createGray4 (event)
local gray4=display.newImageRect("images/grayUD.png",520,60)
gray4.x=display.contentCenterX
gray4.y=(240)
end
function createGray5 (event)
local gray5=display.newImageRect("images/grayUD.png",520,60)
gray5.x=display.contentCenterX
gray5.y=(140)
end
function createGray6 (event)
local gray6=display.newImageRect("images/grayUD.png",520,60)
gray6.x=display.contentCenterX
gray6.y=(40)
end
function firstClicked (event)
local greenClicked1=display.newImageRect("images/green-clicked.png",520,60)
greenClicked1.x=display.contentCenterX
greenClicked1.y=(970)
local redClicked1=display.newImageRect("images/red-clicked.png",520,60)
redClicked1.x=display.contentCenterX
redClicked1.y=(240)
end
function secondClicked (event)
local greenClicked2=display.newImageRect("images/green-clicked.png",520,60)
greenClicked2.x=display.contentCenterX
greenClicked2.y=(870)
local redClicked2=display.newImageRect("images/red-clicked.png",520,60)
redClicked2.x=display.contentCenterX
redClicked2.y=(140)
end
function thirdClicked (event)
local greenClicked3=display.newImageRect("images/green-clicked.png",520,60)
greenClicked3.x=display.contentCenterX
greenClicked3.y=(770)
local redClicked3=display.newImageRect("images/red-clicked.png",520,60)
redClicked3.x=display.contentCenterX
redClicked3.y=(40)
end
------------------------------------------------------------
--End of Results Creation (SETUP)
------------------------------------------------------------
--local function sceneDone (event)
--end
-------------------------
--transition to next scene IN THIS FUNCTION
if (event.target.button == 1) then
greenButton1:removeSelf()
greenButton1 = nil
greenButton2:removeSelf()
greenButton2 = nil
greenButton3:removeSelf()
greenButton3 = nil
redButton1:removeSelf()
redButton1 = nil
redButton2:removeSelf()
redButton2 = nil
redButton3:removeSelf()
redButton3 = nil
firstClicked()
createGray2()
createGray3()
createGray5()
createGray6()
--composer.removeScene("firstscene")
end
if (event.target.button == 2) then
greenButton1:removeSelf()
greenButton1 = nil
greenButton2:removeSelf()
greenButton2 = nil
greenButton3:removeSelf()
greenButton3 = nil
redButton1:removeSelf()
redButton1 = nil
redButton2:removeSelf()
redButton2 = nil
redButton3:removeSelf()
redButton3 = nil
secondClicked()
createGray1()
createGray3()
createGray4()
createGray6()
--composer.removeScene("firstscene")
end
if (event.target.button == 3) then
greenButton1:removeSelf()
greenButton1 = nil
greenButton2:removeSelf()
greenButton2 = nil
greenButton3:removeSelf()
greenButton3 = nil
redButton1:removeSelf()
redButton1 = nil
redButton2:removeSelf()
redButton2 = nil
redButton3:removeSelf()
redButton3 = nil
thirdClicked()
createGray1()
createGray2()
createGray4()
createGray5()
--composer.removeScene("firstscene")
end
moveTextToFront()
composer.gotoScene("secondscene")
end
greenButton1:addEventListener("touch", buttonClicked)
greenButton2:addEventListener("touch", buttonClicked)
greenButton3:addEventListener("touch", buttonClicked)
end --end of images and stuff
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == "will" then
-- Called when the scene is still off screen and is about to move on screen
elseif phase == "did" then
-- Called when the scene is now on screen
--
-- INSERT code here to make the scene come alive
-- e.g. start timers, begin animation, play audio, etc.
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
print("hidden2")
if event.phase == "will" then
-- Called when the scene is on screen and is about to move off screen
--
-- INSERT code here to pause the scene
-- e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == "did" then
print("hidden")
-- Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's "view" (sceneGroup)
--
-- INSERT code here to cleanup the scene
-- e.g. remove display objects, remove touch listeners, save state, etc.
if playBtn then
playBtn:removeSelf() -- widgets must be manually removed
playBtn = nil
end
end
---------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-----------------------------------------------------------------------------------------
return scene
I can't see in your code that you are adding your object into the sceneGroup (self.view). Make sure you insert them into that group for example scneeGroup:insert( greenButton3 ).
When changing scene there is no need to hide the objects in the sceneGroup as they will be hidden automatically by Corona.
You have to add all the display objects in to the sceneGroup.
function scene:create( event )
local sceneGroup = self.view
display.setStatusBar( display.HiddenStatusBar )
local group = display.newGroup()
-------Set up green buttons
local greenButton3=display.newImageRect("images/green-unclicked.png", 520,60)
greenButton3.x=display.contentCenterX
greenButton3.y=(970)
greenButton3.button = 1
sceneGroup :insert(greenButton3)
end
try adding all the text objects everything in to the sceneGroup.
I am doing a game in corona SDK, but I have this little problem.
I have a menu with a button. If I press it, it sends me to the first level of my game.
When I pass the final level, the game return me to the menu. Bur, if I start playing the first again, my images doesn´t appear.
The images are balls, and to pass the level, you have to eliminate all the balls. To do this, I use:
ball:removeSlef()
ball = nil
But, I don´t think that this is the problem, because I eliminate this lines, and it doesn´t work.
The images are create in scene:createScene function, and insert in the Group.
I short the code of the first level to be understood.
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local physics = require "physics"
physics.start(); physics.pause()
physics.setGravity( 0, 0 )
local cont = 0
local bur = {}
function eliminar1( event )
if (cont == 0) and (event.phase == "began") then
event.target:removeSelf()
bur[1] = nil
cont = cont + 1
end
end
function eliminar2( event )
if (cont == 1) and (event.phase == "began") then
bur[2]:removeSelf()
bur[2] = nil
cont = cont + 1
end
end
function eliminar3( event )
if (cont == 2) and (event.phase == "began") then
bur[3]:removeSelf()
bur[3] = nil
storyboard.gotoScene( "levels.1.level2" )
end
end
function scene:createScene ( event )
local screenGroup = self.view
for i = 1,3 do
bur[i] = display.newImage("irudiak/"..i..".png")
bur[i]:translate(math.random(0,280), math.random(0,400) )
physics.addBody( bur[i], {bounce = 0.3 } )
bur[i]:setLinearVelocity(math.random(-50,50), math.random(-50,50) )
screenGroup:insert(bur[i])
end
bur[1]:addEventListener("touch", eliminar1)
bur[2]:addEventListener("touch", eliminar2)
bur[3]:addEventListener("touch", eliminar3)
end
function scene:enterScene( event )
local screenGroup = self.view
physics.start()
end
function scene:exitScene( event )
local screenGroup = self.view
physics.stop()
end
function scene:destroyScene( event )
local screenGroup = self.view
package.loaded[physics] = nil
physics = nil
end
return scene
createScene is ran only first time when you gotoScene. Every next time only willEnterScene and enterScene are played. To play createScene again you have to remove it (storyboard.removeScene() I guess). Or you can move some stuff you need to willEnterScene. For more detailed info you can watch this: http://www.coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/
I'm trying to call a text field listener from multiple field like this page
http://docs.coronalabs.com/api/library/native/newTextField.html#listener-optional
When user start to write something in input field, handler function is called normally but closure that is located in handler is not called.
login.lua file like below:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
-- Forward declerations
local userNameField
-- TextField Listener
local function fieldHandler( getObj )
print( "This message is showing up :) " )
-- Use Lua closure in order to access the TextField object
return function( event )
print( "This message is not showing up :( There is something wrong here!!!" )
if ( "began" == event.phase ) then
-- This is the "keyboard has appeared" event
getObj().text = ""
getObj():setTextColor( 0, 0, 0, 255 )
elseif ( "ended" == event.phase ) then
-- This event is called when the user stops editing a field:
-- for example, when they touch a different field or keyboard focus goes away
print( "Text entered = " .. tostring( getObj().text ) ) -- display the text entered
elseif ( "submitted" == event.phase ) then
-- This event occurs when the user presses the "return" key
-- (if available) on the onscreen keyboard
-- Hide keyboard
native.setKeyboardFocus( nil )
end
end -- "return function()"
end
local function userNameFieldHandler( event )
local myfunc = fieldHandler( function() return userNameField end ) -- passes the text field object
end
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
-- Create our Text Field
userNameField = native.newTextField( display.contentWidth * 0.1, display.contentHeight * 0.5, display.contentWidth * 0.8, display.contentHeight * 0.08)
userNameField:addEventListener( "userInput", userNameFieldHandler )
userNameField.font = native.newFont( native.systemFontBold, 22 )
userNameField.text = "User Name"
userNameField:setTextColor( 0, 0, 0, 12 )
end
Help please...
I don't know Corona, but your code is somewhat strange.
userNameFieldHandler doesn't do much, it just creates a handler calling fieldHandler and stores it in a local that is never used (myfunc). Are you sure you didn't mean this:
local function userNameFieldHandler( event )
local myfunc = fieldHandler(
function() return userNameField end ) -- passes the text field object
return myfunc --<<<<--- added return
end
and maybe when you add the event listener you meant this (note the added ()):
userNameField:addEventListener( "userInput", userNameFieldHandler() )
I'm using director , to move from one scene to another. I have a problem that the buttons and text fields from intor still in the top of screen when moving to any scene.
How to remove items (text fields, btns from intro.lua screen) before move to next scene?
enter code here
-- into.lua
module(..., package.seeall)
function new()
--
-- Project: NativeKeyboard2
--
local widget = require( "widget" )
require("hijacks")
local tHeight -- forward reference
-------------------------------------------
-- General event handler for fields
-------------------------------------------
-- You could also assign different handlers for each textfield
local function fieldHandler( textField )
return function( event )
if ( "began" == event.phase ) then
-- This is the "keyboard has appeared" event
-- In some cases you may want to adjust the interface when the keyboard appears.
elseif ( "ended" == event.phase ) then
-- This event is called when the user stops editing a field: for example, when they touch a different field
elseif ( "editing" == event.phase ) then
elseif ( "submitted" == event.phase ) then
-- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard
print( textField().text )
-- Hide keyboard
native.setKeyboardFocus( nil )
end
end
end
-- Predefine local objects for use later
local nameField, phoneField
local fields = display.newGroup()
-------------------------------------------
-- *** Create native input textfields ***
-------------------------------------------
-- Note: currently this feature works in device builds or Xcode simulator builds only (also works on Corona Mac Simulator)
local isAndroid = "Android" == system.getInfo("platformName")
local inputFontSize = 18
local inputFontHeight = 30
tHeight = 30
if isAndroid then
-- Android text fields have more chrome. It's either make them bigger, or make the font smaller.
-- We'll do both
inputFontSize = 14
inputFontHeight = 42
tHeight = 40
end
nameField = native.newTextField( 40, 120, 200, tHeight )
nameField.font = native.newFont( native.systemFontBold, inputFontSize )
nameField:addEventListener( "userInput", fieldHandler( function() return nameField end ) )
phoneField = native.newTextField( 40, 160, 200, tHeight )
phoneField.font = native.newFont( native.systemFontBold, inputFontSize )
phoneField.inputType = "phone"
phoneField:addEventListener( "userInput", fieldHandler( function() return phoneField end ) )
-- Add fields to our new group
fields:insert(nameField)
fields:insert(phoneField)
-------------------------------------------
-- *** Add field labels ***
-------------------------------------------
local defaultLabel = display.newText( "الاسم", 250, 120, native.systemFont, 18 )
defaultLabel:setTextColor( 255, 0, 0 )
local defaultLabel = display.newText( "رقم الجوال", 250, 160, native.systemFont, 18 )
defaultLabel:setTextColor( 255, 0, 0 )
-- -------------------------------------------
-- -- Create a Background touch event
-- -------------------------------------------
local listener = function( event )
-- Hide keyboard
print("tap pressed")
native.setKeyboardFocus( nil )
return true
end
-- Determine if running on Corona Simulator
--
local isSimulator = "simulator" == system.getInfo("environment")
if system.getInfo( "platformName" ) == "Mac OS X" then isSimulator = false; end
-- Native Text Fields not supported on Simulator
--
if isSimulator then
msg = display.newText( "الرجاء ادخال اسمك ورقم جوالك", 0, 280, native.systemFontBold, 12 )
msg.x = display.contentWidth/2 -- center title
msg:setTextColor( 255,0,0 )
end
-- -- Add listener to background for user "tap"
-- bkgd:addEventListener( "tap", listener )
-- display.remove( obj )
-- obj = nil
local introGroup = display.newGroup();
local background = display.newImage("graphics/intro_background.png")
local begin = display.newImage("graphics/begin_button.png")
begin.x = 160;
begin.y = 400;
begin.scene = "menu";
introGroup:insert(background);
introGroup:insert(begin);
begin:addEventListener("touch", changeScene)
return introGroup;
end
Corona provides a very good feature that is "Storyboard".I am giving you a brief explanation, Try this -
Storyboard - It is a scene (e.g. "screens" or "views") management library that provides developers an easy way to create and transition between scene modules in a Corona SDK app.
Syntax -
local storyboard = require "storyboard"
Example -
local scene1 = storyboard.newScene( "name of the Scene" )
Here are the different events used in the Storyboard -
1- Create Scene -
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
end
2- Enter Scene -
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
end
3- Exit Scene -
-- Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
end
4 - Destroy Scene -
-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
local group = self.view
end
It will help you.
I have been modifying code from the memory match game to use scenes, sounds, and a 2d table. Adding the scenes has been the hard part. I have it set up to randomly select items from my 2d table. Load those into a temporary table, shuffle them shuffle(), then boardSet(); to load their sounds and place them on the screen. Basically after the game is over I want the scene to reload or go back to the menu to start again. Selecting random data{} elements over and over.
I have tried returning to the menu, or going to a duplicate scene however I can't seem to correctly unload the objects created by my 2d array, as I can't properly add each item into a display group. I have tried everything I can think of. Right now my game loop is setup to add the play again btn that I want to restart the game after one match is found.
This link was a start in the right direction I believe with information about dealing with tables and groups. I still don't know how to cycle thru my 2d table to include everything in a group and then properly unload it.
http://developer.coronalabs.com/content/application-programming-guide-graphics-and-drawing#Variable_References
---------------------------------------------------------------------------------
--
-- level1.lua
--
---------------------------------------------------------------------------------
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local widget = require "widget"
---------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------
local image, text1, text2, text3, memTimer
-- forward declarations and other locals
local againBtn
-- 'onRelease' event listener for playBtn
local function onPlayBtnRelease()
-- go to level1.lua scene
storyboard.gotoScene( "menu", "fade", 500 )
return true -- indicates successful touch
end
-- Preload the sound file (needed for Android)
--
local playBeep = function( testSound )
media.playEventSound( testSound )
end
---Preload sounds
ki = audio.loadSound("ki-nawaneyoo.mp3")
u = audio.loadSound("u.mp3")
mayuhoo = audio.loadSound("mayuhoo.mp3")
--End Sound Test
--Set Global width and height variables
_W = display.contentWidth;
_H = display.contentHeight;
---Count number of correct matches
local matchCount = 0;
--Hide status bar
display.setStatusBar(display.HiddenStatusBar);
--Declare a totalButtons variable to track number of buttons on screen
local totalButtons = 0
--Declare variable to track button select
local secondSelect = 0
local checkForMatch = false
--Declare Data Table
local data = {}
--Load objects into data
data[1] = {}
data[1].title = "Naka"
data[1].subtitle = "a"
data[1].image = "a.png"
data[1].image1 = "ear.png"
data[1].sound = "naka-ear.caf"
data[1].sound1 = "naka-ear.mp3"
data[2] = {}
data[2].title = "Aapuhu"
data[2].subtitle = "aa"
data[2].image = "aa.png"
data[2].image1 = "eyebrow.png"
data[2].sound = "aapuhu-eyebrow.caf"
data[2].sound1 = "aapuhu-eyebrow.mp3"
data[3] = {}
data[3].title = "Ego"
data[3].subtitle = "Ego"
data[3].image = "e.png"
data[3].image1 = "tongue.png"
data[3].sound = "ego-tongue.caf"
data[3].sound1 = "ego-tongue.mp3"
data[4] = {}
data[4].title = "Kamoo"
data[4].subtitle = "kamoo"
data[4].image = "ee.png"
data[4].image1 = "chin.png"
data[4].sound = "kamoo-chin.caf"
data[4].sound1 = "kamoo-chin.mp3"
data[5] = {}
data[5].title = "Kowpa"
data[5].subtitle = "ego"
data[5].image = "leg.png"
data[5].image1 = "leg.png"
data[5].sound = "kowpa-leg.caf"
data[5].sound1 = "kowpa-leg.mp3"
data[6] = {}
data[6].title = "Mae"
data[6].subtitle = "kwa"
data[6].image = "hand.png"
data[6].image1 = "hand.png"
data[6].sound = "mae-hand.caf"
data[6].sound1 = "mae-hand.mp3"
data[7] = {}
data[7].title = "Nodo"
data[7].subtitle = "kwe"
data[7].image = "kwe.png"
data[7].image1 = "throat.png"
data[7].sound = "nodo-throat.caf"
data[7].sound1 = "nodo-throat.mp3"
data[8] = {}
data[8].title = "Matogo"
data[8].subtitle = "kwe"
data[8].image = "kwe.png"
data[8].image1 = "thumb.png"
data[8].sound = "matogo-thumb.caf"
data[8].sound1 = "matogo-thumb.mp3"
data[9] = {}
data[9].title = "Matzehe"
data[9].subtitle = "kwe"
data[9].image = "kwe.png"
data[9].image1 = "elbow.png"
data[9].sound = "matzehe-eblow.caf"
data[9].sound1 = "matzehe-elbow.mp3"
data[10] = {}
data[10].title = "Kuku"
data[10].subtitle = "kwe"
data[10].image = "kwe.png"
data[10].image1 = "foot.png"
data[10].sound = "kuku-foot.caf"
data[10].sound1 = "kuku-foot.mp3"
--Declare button, buttonCover, and buttonImages table
local tableCopy = {}
local buttonImages = {}
--Shuffle data table
--local shuffleSet = function()
--Shuffle data table
math.randomseed (os.time())
local function shuffle(a)
local n = #a
local t
local k
while(n > 0) do
t = a[n]
k = math.random(n)
a[n] = a[k]
a[k] = t
n = n - 1
end
return a
end
local tableCopy = shuffle(data);
local button = {}
local buttonCover = {}
--Choose six random objects from data to be used in game.
local firstSix = {}
for i = 1,6 do
firstSix[i] = tableCopy[i];
end
local buttonImages = {firstSix[1],firstSix[1],firstSix[2],firstSix[2],firstSix[3],firstSix[3],firstSix[4],firstSix[4],firstSix[5],firstSix[5],firstSix[6],firstSix[6]}
--end
--Declare and prime a last button selected variable
local lastButton;-- = display.newImage("1.png");
--lastButton.myName = 1;
--
--Set up simple off-white background
--Notify player if match is found or not
local matchText = display.newText(" ", 0, 0, native.systemFont, 65)
matchText:setReferencePoint(display.CenterReferencePoint)
matchText:setTextColor(255, 255, 255)
matchText.x = _W/2
--
--Set starting point for button grid
local x = -20
local matchesFound = 0;
--Set up game function
local function game(object, event)
if(event.phase == "began") then
if(checkForMatch == false and secondSelect == 0) then
--Flip over first button
buttonCover[object.number].isVisible = false;
audio.play(object.sound);
lastButton = object
checkForMatch = true
elseif(checkForMatch == true and object.number ~= lastButton.number) then
if(secondSelect == 0) then
--Flip over second button
buttonCover[object.number].isVisible = false;
secondSelect = 1;
--If buttons do not match, flip buttons over
if(lastButton.myName ~= object.myName) then
audio.play(object.sound);
timer.performWithDelay(1000,function()
matchText.text = "Ki Nawa'neyoo";
audio.play(ki); end,1)
timer.performWithDelay(2500, function()
matchText.text = " ";
checkForMatch = false;
secondSelect = 0;
buttonCover[lastButton.number].isVisible = true;
buttonCover[object.number].isVisible = true;
end, 1)
--If buttons DO match, remove buttons
elseif(lastButton.myName == object.myName) then
matchText.text = "U " .. object.myName .. " Mayuhoo";
audio.play(u)
timer.performWithDelay(750,function()
audio.play(object.sound); end, 1)
timer.performWithDelay(1500,function()
audio.play(mayuhoo); end, 1)
timer.performWithDelay(2400, function()
matchText.text = " ";
checkForMatch = false;
secondSelect = 0;
lastButton:removeSelf();
object:removeSelf();
buttonCover[lastButton.number]:removeSelf();
buttonCover[object.number]:removeSelf();
matchesFound = matchesFound + 1;
timer.performWithDelay(250, function()
if (matchesFound == 1) then
matchText.text = " Play Again? ";
againBtn.isVisible = true;
-- all display objects must be inserted into group
--group:insert( playBtn )
end
end, 1)
end, 1)
end
end
end
end
end
--]]
-- Touch event listener for background image
--[[
local function onSceneTouch( self, event )
if event.phase == "began" then
storyboard.gotoScene( "scene2", "slideLeft", 800 )
return true
end
end
--]]
-- Called when the scene's view does not exist:
function scene:createScene( event )
local screenGroup = self.view
local image = display.newImageRect( "bg.png", display.contentWidth, display.contentHeight )
image:setReferencePoint( display.TopLeftReferencePoint )
image.x, image.y = 0, 0
screenGroup:insert( image )
local function boardSet()
for count = 1,3 do
x = x + 100 * 2
y = 20 *2
for insideCount = 1,4 do
y = y + 90 * 2
--Assign each image a random location on grid
local temp = math.random(1,#buttonImages)
button[count] = display.newImageRect(buttonImages[temp].image1, 150, 150);
--Position the button
button[count].x = x;
button[count].y = y;
--Give each a button a name
button[count].myName = buttonImages[temp].title
-- Preload the sound file (needed for Android)
--Give each button a sounds
soundID = audio.loadSound(buttonImages[temp].sound1)
button[count].sound = soundID
button[count].sound1 = soundID
button[count].number = totalButtons
--Remove button from buttonImages table
table.remove(buttonImages, temp)
--Set a cover to hide the button image
buttonCover[totalButtons] = display.newImageRect("button.png", 150, 150);
buttonCover[totalButtons].x = x; buttonCover[totalButtons].y = y;
--screenGroup:insert(button[count].)
totalButtons = totalButtons + 1
--Attach listener event to each button
button[count].touch = game
button[count]:addEventListener( "touch", button[count] )
end
end
end
boardSet();
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
againBtn = widget.newButton{
label="Play Again?",
fontSize = 25,
labelColor = { default={255}, over={128} },
default="button1.png",
over="button-over.png",
width=250, height=100,
onRelease = onPlayBtnRelease -- event listener function
}
againBtn:setReferencePoint( display.CenterReferencePoint )
againBtn.x = display.contentWidth * 0.5
againBtn.y = display.contentHeight - 125
againBtn.isVisible = false
print( "1: enterScene event" )
-- remove previous scene's view
end
-- Called when scene is about to move offscreen:
function scene:exitScene( event )
print( "1: exitScene event" )
if againBtn then
matchText.text = " "
againBtn:removeSelf() -- widgets must be manually removed
button = nil
buttonCover = nil
firstSix = nil
data = nil
tableCopy = nil
buttonImages = nil
againBtn = nil
end
end
-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
local screenGroup = self.view
storyboard.removeScene();
--storyboard.purgeScene( "level1" )
print( "((destroying scene 1's view))" )
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 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
Yeah, this is simple.
here is an example(assuming that you have implemented storyboard)
function scene:createScene(event)
screenGroup = self.view
local image = display.newImage("image.png")
screenGroup:insert(image)
end
Now everything will go fine :)
Add all the display objects to a group.local myGroup = display.newGroup();
local img = display.newImage("yourimage.png");
myGroup:insert(img);
For example
If you follow this method, all display objects will not be flushed out of memory when you change the screen.
When using corona storyboard you need to add display objects to the group. For example you would need to add all of your display objects into screenGroup in order for them to be removed when you change scenes.