Corona SDK onRowTouch gotoScene - lua

I cannot change the scene with composer or storyboard. I am trying to change the scene when you touch a row in tableview. I am able to change the scene from the main file to the file with the tableview. The tableview touch is not working though.
Some of my code is below:
local widget = require( "widget" )
local storyboard = require( "storyboard" )
local composer = require( "composer" )
local scene = composer.newScene()
function RowTouch( event )
composer.gotoScene( "thehike" )
end
myTable = widget.newTableView
{
width = display.contentWidth,
height = display.contentHeight,
backgroundColor = { .47, .66, .53 },
topPadding = 0,
hideBackground = false,
onRowRender = onRowRender,
onRowTouch = RowTouch,
noLines = true,
}
for i=1, #hike do
myTable:insertRow{
rowHeight = 220,
isCategory = false,
lineColor = { .47, .66, .53 }
}
end
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
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
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
-- 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.
end
---------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-----------------------------------------------------------------------------------------
return scene

Your code does not work for a couple of reasons. You have a dangling end in line 34 and you did not define hike. You probably need a smaller rowHeight in order to show the rows in your view:
local myTable = widget.newTableView
{
left = 0,
top = 0,
height = 330,
width = 300
}
myTable:insertRow(
{
isCategory = false,
rowHeight = 40,
rowColor = rowColor,
lineColor = {.47, .66, .53}
}
)
Also, the documentation is pretty good on this[1].
[1] http://docs.coronalabs.com/api/library/widget/newTableView.html

Related

composer.removeScene() not working, other display objects not disappearing

I'm working on an app in lua using Corona SDK.
I'm trying to go from my buildscene2 to my mainbuildscene, and I am successfully calling the new scene, but the objects from buildscene2 are not being removed. I have added the code to remove the previous scene, in the mainbuildscene.lua file. I've tried adding it in scene:create() and in scene:show(). Neither worked. This exact method has worked for me when transitioning from other scenes in the same application. What I've done here is created a function that uses composer.gotoScene("mainbuildscene") and then called the function when a button is pressed. I have a bunch of random prints in there just to see if the code is being read. It is.
I've looked for other people having this problem. Plenty of people have experienced this, but none of their solutions have worked for me. I've tried moving the event listener for when the next button is pressed to inside the snapTo function and then back out. If the listener is outside, it gives me an error.
Also, I know that the problem is not when I'm calling the new scene, because I put in a random picture to test if the objects from that scene would show up, and they did, except they were overtop of the objects from buildscene2. I even tried adding a function into the second scene that, when called, destroys the first scene. Didn't work.
Unless I did something wrong, all my display objects in buildscene2 are added to the group.
Here's my buildscene2 file. This is the file I'm transitioning away from and trying to destroy.
local composer = require( "composer" )
local scene = composer.newScene()
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called.
-- -----------------------------------------------------------------------------------------------------------------
-- local forward references should go here
-- ------------------------------------------------------------------------------
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
local group = display.newGroup()
composer.removeScene("thirdscene")
----------EMPTY BOXES
local itemFrame1=display.newImageRect("images/selection-box.png", 200,200)
itemFrame1.x=140
itemFrame1.y=130
group:insert(itemFrame1)
local itemFrame2=display.newImageRect("images/selection-box.png", 200,200)
itemFrame2.x=387
itemFrame2.y=130
group:insert(itemFrame2)
local itemFrame3=display.newImageRect("images/selection-box.png", 200,200)
itemFrame3.x=635
itemFrame3.y=130
group:insert(itemFrame3)
---------------HEADS
local robotHead=display.newImageRect("images/robot-head.png", 195,180)
robotHead.x=137
robotHead.y=130
robotHead.rotation = 180
robotHead.headtype=1
local rabbitHead=display.newImageRect("images/rabbit-head.png", 200, 180)
rabbitHead.x=387
rabbitHead.y=130
rabbitHead.rotation = 180
rabbitHead.headtype=2
local dinoHead=display.newImageRect("images/dino-head.png", 210, 190)
dinoHead.x=665
dinoHead.y=140
dinoHead.rotation = 180
dinoHead.headtype=3
-------------------OTHER
local nextButton=display.newImageRect("images/donebutton.png", 130, 130)
nextButton.x=140
nextButton.y=710
group:insert(nextButton)
nextButton.isVisible = false
local nextText=display.newText("NEXT", 100,100, native.systemFontBold, 40)
nextText.x=140
nextText.y=710
nextText:setTextColor (0,0,0)
group:insert(nextText)
function snapTo (event)
function makeVisible (event)
nextButton.isVisible = true
end
if event.phase == "began" then
event.target.markX = event.target.x -- store x location of object
event.target.markY = event.target.y -- store y location of object
torsoPlaced=event.target.torsoName
end
function addHeadsToGroup (event)
group:insert(robotHead)
group:insert(rabbitHead)
group:insert(dinoHead)
end
if event.phase == "moved" then
local x = (event.x - event.xStart) + event.target.markX
local y = (event.y - event.yStart) + event.target.markY
event.target.x, event.target.y = x, y -- move object based on calculations above
function demolish1 (event)
robotHead.x = 1000
robotHead.y = 900
robotHead.width = 10
robotHead.length = 10
robotHead.isVisible = false
group:insert(robotHead)
local staticRobot = display.newImageRect("images/robot-head.png", 195, 180)
staticRobot.x=137
staticRobot.y=130
staticRobot.rotation = 180
group:insert(staticRobot)
end
function demolish2 (event)
rabbitHead.x = 1000
rabbitHead.y = 900
rabbitHead.width = 10
rabbitHead.length = 10
rabbitHead.isVisible = false
group:insert(rabbitHead)
local staticRabbit = display.newImageRect("images/rabbit-head.png", 200, 180)
staticRabbit.x=387
staticRabbit.y=130
staticRabbit.rotation = 180
group:insert(staticRabbit)
end
function demolish3 (event)
dinoHead.x = 1000
dinoHead.y = 900
dinoHead.width = 10
dinoHead.length = 10
dinoHead.isVisible = false
group:insert(dinoHead)
local staticDino = display.newImageRect("images/dino-head.png", 210, 190)
staticDino.x=665
staticDino.y=140
staticDino.rotation = 180
group:insert(staticDino)
end
if (event.target.headtype == 1) then
event.target.width = 220
event.target.height = 200
if event.target.y > 400 then
event.target.isVisible = false
local newHead1 = display.newImageRect("images/robot-head.png",220, 200)
newHead1.x=150
newHead1.y=500
newHead1.rotation = 270
end
addHeadsToGroup()
makeVisible()
demolish2()
demolish3()
end
if (event.target.headtype == 2) then
event.target.width = 340
event.target.height = 240
if event.target.y > 400 then
event.target.isVisible = false
local newHead2 = display.newImageRect("images/rabbit-head.png",340, 240)
newHead2.x=140
newHead2.y=500
newHead2.rotation = 270
end
addHeadsToGroup()
makeVisible()
demolish1()
demolish3()
end
if (event.target.headtype == 3) then
event.target.width = 280
event.target.height = 250
if event.target.y > 400 then
event.target.isVisible = false
local newHead3 = display.newImageRect("images/dino-head.png",280, 250)
newHead3.x=140
newHead3.y=540
newHead3.rotation = 270
end
addHeadsToGroup()
makeVisible()
demolish1()
demolish2()
end
local function nextButtonClicked2 (event)
if event.phase=="ended" then
function sceneChange (event)
composer.gotoScene("mainbuildscene")
print("jererefs")
end
print("egsijegij")
sceneChange()
end
end
nextButton:addEventListener("touch", nextButtonClicked2)
end
end--end of snapTo
robotHead:addEventListener( "touch", snapTo)
rabbitHead:addEventListener( "touch", snapTo)
dinoHead:addEventListener("touch", snapTo)
end -- end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is still off screen (but is about to come on screen).
elseif ( phase == "did" ) then
-- Called when the scene is now on screen.
-- Insert code here to make the scene come alive.
-- Example: start timers, begin animation, play audio, etc.
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is on screen (but is about to go off screen).
-- Insert code here to "pause" the scene.
-- Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == "did" ) then
-- Called immediately after scene goes off screen.
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's view ("sceneGroup").
-- Insert code here to clean up the scene.
-- Example: remove display objects, save state, etc.
end
---------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- ------------------------------------------------------------------ -------------
return scene
Here's my mainbuildscene file, the file I'm trying to load. It's loading in but the previous scene isn't going away.
local composer = require( "composer" )
local scene = composer.newScene()
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called.
-- -----------------------------------------------------------------------------------------------------------------
-- local forward references should go here
-- -------------------------------------------------------------------------------
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
function removeTheScene (event)
print("this is not working, unless it is...?")
composer.removeScene("buildscene2")
end
removeTheScene()
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
local group=display.newGroup()
--
--
local picture=display.newImageRect("images/bee-torso.png", 100, 100)
picture.x=display.contentCenterX
picture.y=display.contentCenterY
group:insert(picture)
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is still off screen (but is about to come on screen).
elseif ( phase == "did" ) then
-- Called when the scene is now on screen.
-- Insert code here to make the scene come alive.
-- Example: start timers, begin animation, play audio, etc.
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is on screen (but is about to go off screen).
-- Insert code here to "pause" the scene.
-- Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == "did" ) then
-- Called immediately after scene goes off screen.
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's view ("sceneGroup").
-- Insert code here to clean up the scene.
-- Example: remove display objects, save state, etc.
end
-- -------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -------------------------------------------------------------------------------
return scene
Please let me know what I did wrong or give me any advice you can. Thanks for reading.
You must add them to "sceneGroup". You are adding all the object into your own group but they should be added to the "sceneGroup".
In buildscene2.lua remove these lines:
local group = display.newGroup()
composer.removeScene("thirdscene")
And make sure you add all your display object into the sceneGroup, and not the group:
Change this group:insert(itemFrame1) to this sceneGroup:insert(itemFrame1) but for all your display objects and on all your scenes.
If you're planning to reuse the scene (i.e. being able to go back and forth) you don't need to use the removeScene because Corona handles all the display objects belonging to the scenes i.e. when you go from Scene A to Scene B Corona will automatically move away the display objects belonging to Scene A from the screen and move the display objects belonging to Scene B to the screen.

My buttons wont appear on the screen for some reason in Corona SDK

My buttons wont appear on the screen for some reason in Corona SDK here is my code what am I missing?
local composer = require( "composer" )
local scene = composer.newScene()
-- include Corona's "widget" library
local widget = require "widget"
-- Function to handle button events
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
print( "Button was pressed and released" )
end
end
-- forward declarations and other locals
local playBtn
-- 'onRelease' event listener for playBtn
local function onPlayBtnRelease()
-- go to levelSelect.lua scene
composer.gotoScene( "levelSelect", "fade", 500 )
return true -- indicates successful touch
end
-- Background
local sky = display.newImage ("startScreen/sky.png")
sky.x = display.contentWidth/2; sky.y = display.contentHeight/2;
-- Picture
local preston = display.newImage ("startScreen/PrestonArt.png")
preston:scale( 0.4, 0.4 )
preston.x = display.contentWidth/2; preston.y = display.contentHeight/2;
-- Labels
local learningLabel = display.newImage ("startScreen/Learning.png")
learningLabel:scale( 0.3, 0.3 )
learningLabel.x = 506; learningLabel.y = 170;
local centerLabel = display.newImage ("startScreen/Center.png")
centerLabel:scale( 0.3, 0.3 )
centerLabel.x = 506; centerLabel.y = 600;
function scene:create( event )
local sceneGroup = self.view
-- Called when the scene's view does not exist.
--
-- INSERT code here to initialize the scene
-- e.g. add display objects to 'sceneGroup', add touch listeners, etc.
-- create a widget button (which will loads levelSelect.lua on release)
playBtn = widget.newButton{
defaultFile = "startScreen/Play.png", --the "default" image file
overFile = "startScreen/Play-Over.png", --the "over" image file
width=240, height=120,
onRelease = onPlayBtnRelease -- event listener function
}
playBtn.x = 300; playBtn.y = 695;
-- all display objects must be inserted into group
sceneGroup:insert( playBtn )
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
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
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
-- 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
My guesses are
Comment says go to level1.lua but it tries to go to levelSelect
Don't use upper case in file names (in general)
try removing "fade", 500 to see it it works
in levelselect.lua you did not return scene or did not handle scene:create
More code and info please

Corona SDK Using nativ text field in storyboard

So, I have a program that is using the corona storyboard.
There is scene 1 where the user can type something in a textbox at and there is also another scene, scene 2 where I have some text. The problem is that when the user goes away from the scene with the textbox, the textbox still appears on the screen. I've tried using removeSelf() which does delete it. But the problem is that the textbox is gone forever if i use removeSelf(), even when the user goes back to scene 1, it's still gone!
How could I fix this? Here is the code for scene 1:
--
-- view1.lua
--
-----------------------------------------------------------------------------------------
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
function scene:createScene( event )
local group = self.view
local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
bg:setFillColor(0,255,0 ) -- green
local title = display.newText( "Second View", 0, 0, native.systemFont, 32 )
title:setTextColor( 0 ) -- black
title:setReferencePoint( display.CenterReferencePoint )
title.x = display.contentWidth * 0.5
title.y = 125
local summary = display.newText( "Loaded by the first tab 'onPress' listener\n— specified in the 'tabButtons' table.", 0, 0, 300, 300, native.systemFont, 14 )
summary:setTextColor( 0 ) -- black
summary:setReferencePoint( display.CenterReferencePoint )
summary.x = display.contentWidth * 0.5 + 10
summary.y = title.y + 215
local function textListener( event )
if ( event.phase == "began" ) then
-- user begins editing text field
print( event.text )
elseif ( event.phase == "ended" ) then
-- text field loses focus
elseif ( event.phase == "ended" or event.phase == "submitted" ) then
-- do something with defaulField's text
elseif ( event.phase == "editing" ) then
print( event.newCharacters )
print( event.oldText )
print( event.startPosition )
print( event.text )
end
end
-- Create our Text Field
defaultField = native.newTextField( 150, 150, 180, 30 )
defaultField:addEventListener( "userInput", textListener )
-- all objects must be added to group (e.g. self.view)
group:insert( bg )
group:insert( title )
group:insert( summary )
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
So maybe you could use alpha to hide all elements in group?
group.alpha=0
You need to add the text box on the scene:enterScene method and remove it on the scene:exitScene.
Corona calls createScene only once, when creating the scene, but it calls enterScene and exitScene when the scene moves in or out of the stack. These are the places to create/remove elements.
For more information about screen management in corona you can read this. It's main focus is about cleaning up resources used in a scene and the examples are about sounds, but it applies to what you're trying to do and explains a little the Scene Management in Corona.

A table does not get deleted on scene change, CoronaSDK Storyboard

Basically, I have a table that has images that move horizontally, when I change scenes it stays there, even though I didn't call for it.. As in, on there is no code for it in offlinemode.lua.. I'm thinking that for some reason it's a global element..
Here is main.lua:
local storyboard = require "storyboard"
local options =
{
effect = "slideLeft",
time = 800
}
storyboard.gotoScene( ".Lua.MainMenu", options )
This MainMenu.lua
--//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
--/////// Unamed game (so far) main.lua is GUI/MENU
-- //////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////
--implements the storybord then deletes it upon completeion--
local storyboard = require "storyboard"
storyboard.purgeOnSceneChange = true
--You may began now-- (incorrectly speeleed corrctly)
local scene = storyboard.newScene()
--makes a container for the background--
-- createScene event simply creates a background image
function scene:createScene( event )
local bg = display.newImage( self.view, "background.png" )
end
scene:addEventListener( "createScene" )
--end of aking a container for the background--
--disables the status bar--
display.setStatusBar( display.HiddenStatusBar )
--end of disabling of status bar--
--Returns Screen Width and Screen Height--
_W = display.contentWidth;
_H = display.contentHeight;
--end of returning screen width and height--
--////////////////////////////////////////////////end of the background\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\--
--**********************************************************************************************************************--
--////////////////////////////////////////////////Menu Buttons\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\--
--Set up a local container for the buttons--
local widget = require( "widget" )
local PlayBtn
local function onPlayBtnRelease()
-- go to level1.lua scene
storyboard.gotoScene( ".Lua.offlinemode", "slideLeft", 500 )
display.remove(initstar)
return true -- indicates successful touch
end
local PlayBtn = widget.newButton
{
left = 160,
top = 80,
label = "Offline",
labelAlign = "center",
font = "Arial",
fontSize = 18,
labelColor = { default = {0,0,0}, over = {255,255,255} },
onRelease = onPlayBtnRelease
}
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
--********************************************************************************************************************--
--///////////////////////////////////////Setting up background\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\--
--set up the table to store the clouds in--
local starTable = {}
--end of the table to store the clouds in--
--creates three containers with a picture of a cloud--
function initStar()
local star1 = {}
star1.imgpath = "/images/Cloud1.png"; --Set Image Path for Star
star1.movementSpeed = 10000; --Determines the movement speed of star
table.insert(starTable, star1); --Insert Star into starTable
local star2 = {}
star2.imgpath = "/images/cloud2.png";
star2.movementSpeed = 12000;
table.insert(starTable, star2);
local star3 = {}
star3.imgpath = "/images/cloud3.png";
star3.movementSpeed = 14000;
table.insert(starTable, star3);
end
--ends the function--
--Gets random stars from the table, positioning them randomly--
function getRandomStar()
local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable
local randomStar = display.newImage(temp.imgpath) -- Set image path for object
randomStar.myName = "star" -- Set the name of the object to star
randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move
randomStar.y = math.random(10,_H) -- Set starting point of star at a random X position
randomStar.x = -40; -- Start the star off screenm
starMove = transition.to(randomStar, { --Move the Clouds
time=randomStar.movementSpeed, --sets the speed relative to it's lifetime
onComplete = function(self) self.parent:remove(self); self = nil; end, -- removes itself
x=500 --speed of ?
}) -- Move the Clouds
end
--The end of this function--
--Starts the timer before the Clouds "spawn"--
function startGame()
starTimer1 = timer.performWithDelay(1070,getRandomStar, 0)
starTimer2 = timer.performWithDelay(2030,getRandomStar, 0)
starTimer3 = timer.performWithDelay(2070,getRandomStar, 0)
end
--ends the function--
initStar()
startGame()
-- "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().
function scene:didExitScene( event )
storyboard.purgeScene( "scene1" )
end
scene:addEventListener( "didExitScene" )
return scene
--end the Online function--
and finally offlinemode.lua:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local background = display.newImage("background.png")
local image = display.newImage("/images/ButtonStartOffline.png")
function scene:createScene( event )
end
function scene:enterScene( event )
end
function scene:exitScene( event )
end
function scene:destroyScene( event )
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
If it works, tell me why it didn't for me please..
you should put the image to the display group either in createScene or enterScene so when you go to another scene it will remove all the image within that scene here's the implementation of the it see the code
function scene:createScene( event )
local group = self.view
--/////////////////////Setting up background\\\\\\\\\\\\\\\\\\--
--set up the table to store the clouds in--
local starTable = {}
--end of the table to store the clouds in--
--creates three containers with a picture of a cloud--
function initStar()
local star1 = {}
star1.imgpath = "/images/Cloud1.png"; --Set Image Path for Star
star1.movementSpeed = 10000; --Determines the movement speed of star
table.insert(starTable, star1); --Insert Star into starTable
local star2 = {}
star2.imgpath = "/images/cloud2.png";
star2.movementSpeed = 12000;
table.insert(starTable, star2);
local star3 = {}
star3.imgpath = "/images/cloud3.png";
star3.movementSpeed = 14000;
table.insert(starTable, star3);
end
--ends the function--
--Gets random stars from the table, positioning them randomly--
function getRandomStar()
local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable
local randomStar = display.newImage(temp.imgpath) -- Set image path for object
randomStar.myName = "star" -- Set the name of the object to star
randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move
randomStar.y = math.random(10,_H) -- Set starting point of star at a random X position
randomStar.x = -40; -- Start the star off screenm
group:insert(randomStart) **-- inserting random star into Group**
starMove = transition.to(randomStar, { --Move the Clouds
time=randomStar.movementSpeed, --sets the speed relative to it's lifetime
onComplete = function(self) self.parent:remove(self); self = nil; end, -- removes itself
x=500 --speed of ?
}) -- Move the Clouds
end
--The end of this function--
--Starts the timer before the Clouds "spawn"--
function startGame()
starTimer1 = timer.performWithDelay(1070,getRandomStar, 0)
starTimer2 = timer.performWithDelay(2030,getRandomStar, 0)
starTimer3 = timer.performWithDelay(2070,getRandomStar, 0)
end
--ends the function--
initStar()
startGame()
end
Can you try to add " randomStar.y = -100" to your new scene this will not remove them but will take them out of view.
scene:addEventListener( "didExitScene")
Should be :
scene:addEventListener( "didExitScene", scene )
In your last part of the first code snippet

Clean data form scene

How can i clear all objects out of 'game.lua, when i leave the game scene all the data is still on the screen, how can i remove everything when i leave and reset it back to the start position when i go back to 'game.lua' ?
game.lua:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
_W = display.contentWidth
_H = display.contentHeight
system.setIdleTimer(false); -- Prevent the app from becoming suspended
local physics = require "physics"
physics.start()
clouts = true
score = 0
speeda1 = 100
speedb1 = 150
function scene:createScene( event )
local group = self.view
end
function scene:enterScene( event )
local group = self.view
--start drop zone
if clouts then
local badclout1 = {}
local bad1Group = display.newGroup()
local function spawnBC1()
local badclouts1 = display.newImage("BCloud1.png")
badclouts1.x = math.random(0, _W)
physics.addBody( badclouts1, "dynamic", { density=.1, bounce=.1, friction=.2, radius=45 } )
badclouts1.name = "BCloud1"
badclouts1.bodyType = "kinematic"
badclouts1.isSensor = true
badclouts1.y = math.random(-100, -50)
badclouts1.index = #badclout1 + 1
bad1Group:insert(badclouts1)
badclouts1.rotation = math.random(-10,10) -- Rotate the object
badclouts1:setLinearVelocity(0, math.random(speeda1, speedb1)) -- Drop down
badclout1[badclouts1.index] = badclouts1
tmrSpawn1 = timer.performWithDelay(math.random(spawna, spawnb), spawnBC1)
return badclouts1
end
tmrSpawn1 = timer.performWithDelay(math.random(1000, 10000), spawnBC1)
local function removeBomb()
for i, v in pairs(badclout1) do
if badclout1[i].y >1000 then
badclout1[i]:removeSelf()
badclout1[i] = nil
end
end
end
Runtime:addEventListener("enterFrame", removeBomb)
end
-- end drop zone
local function speatTimer()
speeda1 = speeda1+1
speedb1 = speedb1+1
end
local mainTimer = timer.performWithDelay( 550, speatTimer, 200 )
function gameOver()
storyboard.gotoScene("restart", "fade", 400)
end
end
function scene:exitScene( event )
local group = self.view
Runtime:removeEventListener( "collision", onCollision )
Runtime:removeEventListener("accelerometer", onTilt)
Runtime:removeEventListener("enterFrame", removeBomb)
end
function scene:destroyScene( event )
local group = self.view
end
function scene:overlayEnded( event )
local group = self.view
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
scene:addEventListener( "overlayEnded", scene )
return scene
Regards Kevin,
if you are using storyboard and want to reset all the values you can remove the scene before you go to another scene so when you go again to the same scene all will be created
function scene:exitScene( event )
local group = self.view
Runtime:removeEventListener( "collision", onCollision )
Runtime:removeEventListener("accelerometer", onTilt)
Runtime:removeEventListener("enterFrame", removeBomb)
storyboard.removeScene("SCENE TO REMOVE")
end
or do this after you go to the scene
function scene:enterScene( event )
storyboard.removeScene("SCENE TO REMOVE")
end
here's the link on how to implement it.
http://www.coronalabs.com/blog/2012/07/31/storyboard-scene-purging-vs-removal/
There are two way to reset back your object positions and variable data
The first one is to create a function like this
function resetGame()
--your initial position and data values here
end
And call it whenever you need to reset your game but you will manually code the reset values.
The second one is by creating a dummy scene. A dummy scene is a scene that will redirect you again to the game scene like this
Dummy Scene
function scene:createScene( event )
local group = self.view
storyboard.gotoScene( "scenes.Game" ) --scene/Game.lua
end
to reset the values of your objects and variables, but don't forget to remove all of the listeners and put it on the exitScene() function when you go to the dummy scene.
Since you are using storyboard, why not simply insert all your display objects into group:
group:insert(someobject)
and let the scene manager do the work for you. By not creating the scene in scene:createScene() your ability to transition the screen won't work. By not putting things into the group, it can't remove them for you when the scene goes away.

Resources