Button gotoScene not working - lua

the storyboard.gotoScene("facebook", "fade", 400) is not working if i tap the button, and i dont get any error messages in the terminal. What am i doing wrong ?
-- requires
display.setStatusBar( display.HiddenStatusBar )
_W = display.contentWidth; --Returns Screen Width
_H = display.contentHeight; --Returns Screen Height
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
-- background
function scene:createScene(event)
local screenGroup = self.view
background = display.newImage("restart.png")
screenGroup:insert(background)
button = display.newImage("share2.png")
button.x = display.contentWidth / 2
button.y = display.contentHeight -400
end
function listener(event)
if event.phase == "began" then
print(event.name.." occurred")
storyboard.gotoScene("facebook", "fade", 400)
end
end
function scene:enterScene(event)
storyboard.purgeScene("game")
button:addEventListener( "tap", listener )
end
function scene:exitScene(event)
button:removeEventListener( "tap", listener )
end
function scene:destroyScene(event)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene

The "tap" event and "touch" events are different and they get different "phases" passed to the event handler. The way you have your event handler programmed, you're expecting "touch" events (began, ended, moved). The tap event doesn't really generate any phases, either you were tapped or not.
Either change these two lines:
button:addEventListener( "tap", listener )
button:removeEventListener( "tap", listener )
to:
button:addEventListener( "touch", listener )
and
button:removeEventListener( "touch", listener )
or you can change your listner to:
function listener(event)
print(event.name.." occurred")
storyboard.gotoScene("facebook", "fade", 400)
end

Try this:
storyboard.gotoScene("facebook", {effect = "fade", time=400})
Or:
local options =
{
effect = "fade",
time = 400,
}
storyboard.gotoScene("facebook", options)
http://docs.coronalabs.com/api/library/storyboard/gotoScene.html

It looks like corona does not like it when you call a Scene 'facebook' or renamed facebook to 'postmyscore' and it works

I had the same problem since Coronas last update. I fixed the problem by removing the if check for the event phase. Just comment out the event.phase check in your listener function:
function listener(event)
--if event.phase == "began" then
print(event.name.." occurred")
storyboard.gotoScene("facebook", "fade", 400)
end

facebook.lua is already integrated in the inbuild API , so it may generate the problem , more over you are using tap event , so in listener no need to check the phase or you may use touch event.

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

HowTo create a fuel bar

I would like to build a fuel bar (from full tank to empty). When the user touch the screen, the fuel bar dicrease.
Here is my code :
lifeBar = display.newImage("fuel_bar1.png")
lifeBar.anchorX=0
lifeBar.anchorY=0.6
lifeBar.x = fuel_title.x +114+13.5
lifeBar.y = 37
screenGroup:insert(lifeBar)
Then, i used a function called FuelConsumption() to reduce the fuel tank each time the variable "pressed" is equal to "true". When Pressed==true, it means that the user is touching the screen :
function FuelConsumption()
if lives > 0 and pressed==true then
lives = lives - 1
lifeBar.width=lifeBar.width-1
livesValue.text = string.format("%d", lives)
end
end
This function is active on the enterFrame event as follow :
Runtime:addEventListener( "enterFrame", FuelConsumption )
It works very well, but my question is : How is possible to reduce the lifebar width each 5 secondes (once the variable pressed==true) ?
I tried to add this to my touch event function :
function flyUp(event)
if event.phase == "began" then
pressed=true
if gameStarted == false then
gameStarted = true
pauseBtn.isVisible=true
opt_btn.isVisible=true
Runtime:addEventListener("enterFrame", enterFrameListener)
Runtime:addEventListener("enterFrame",scrollGrasses)
timer.performWithDelay( 5000, function() Runtime:addEventListener( "enterFrame", FuelConsumption ) end,-1 )
end
elseif event.phase == "ended" then
pressed = false
timer.performWithDelay( 1000, function() Runtime:removeEventListener( "enterFrame", FuelConsumption ) end,-1 )
end
end
The problem is that code is consumming a lot of memory and the game is going so slow ! So, is there any other way to do ?
Thank you :)
Instead of using a single image use a sprite sheet.That might boost up the game.

Corona SDK PurgeScene destroys physics

I have create a simple scrolling game, following closely to Mark Farkland's Game tutorial. I clearly followed his coding pattern but got to the point where I am just lost and don't why mine behaves different that his example. Everything is working fine until after a scene is purged.
I have two lua files: game.lua and gameover.lua. In game.lua exitScene I have removed all the eventlisteners. In gameover.lua enterScene, I have purged game.lua scene. When the user touched the screen it then loads game.lua file. Every objects load and refreshes like new apart from the main object which just shoots upwards.
Game.lua
local storyboard = require("storyboard")
local scene = storyboard.newScene()
local physics = require("physics")
function scene:createScene(event)
physics.start()
....
local jet = display.newImage("images/jet_normal.png")
physics.addBody(jet, "dynamic", {density=.07, bounce=0.1, friction=.2, radius=12})
local activateJets = function(self, event)
self:applyForce(0, -1.5, self.x, self.y)
end
function touchScreen(event)
local phase = event.phase
if phase == "began" then
jet.enterFrame = activateJets
Runtime:addEventListener("enterFrame", jet)
end
if phase == "ended" then
Runtime:removeEventListener("enterFrame", jet)
end
end
Runtime:addEventListener("touch", touchScreen)
end
function scene:exitScene(event)
Runtime:removeEventListener("touch", touchScreen)
Runtime:removeEventListener("enterFrame", enemy1)
Runtime:removeEventListener("collision", onColission)
end
GameOver.lua
function start(event)
if event.phase == "began" then
storyboard.gotoScene("game", "fade", 100)
end
end
function scene:enterScene(event)
storyboard.purgeAll("game")
background:addEventListener("touch", start)
end
function scene:exitScene(event)
background:removeEventListener("touch", start)
end
You need to add the jet to the current group.
function scene:createScene(event)
local group = self.view
--...
local jet = display.newImage("images/jet_normal.png")
--...
group:insert(jet)
Runtime:addEventListener("touch", touchScreen)
end
The proper cleanup will happen to the group when exitScene is triggered.
more info / different implementation:
http://docs.coronalabs.com/guide/graphics/group.html

Corona going to previous scene results in black screen

I've got this problem that is delaying my game by a milestone, in scene1 I click the menu button that takes me to the menu then when the user wants to play again they click the play button and they should go to the previous scene however when it does its goes to a black screen. here is some code, this is the main menu button in scene 1:
function scene:enterScene(event)
local group = self.view
function menubutton:touch( event )
if event.phase == "began" then
storyboard.gotoScene( "menu", "slideRight", 750 )
audio.play(click)
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
elseif event.target.isFocus then
if event.phase == "moved" then
print( "user has moved their finger off the button." )
elseif event.phase == "ended" then
print( "user has switched to the main menu" )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
here is the play button on the main menu:
function scene:enterScene(event)
local group = self.view
local function onSceneTouch( event )
if event.phase == "ended" then
audio.play(click)
local previousScene = storyboard.getPrevious()
if previousScene == nil then
storyboard.gotoScene( "scene1", "slideLeft", 750 ) else
storyboard.gotoScene(previousScene)
return true
end
end
end
any ideas? I am getting NO errors in the simulator output.
Edit: This line of code stopped the blank screen when i placed it on the menu, but only the images show up, background image button images etc but nothing else.
local prior_scene = storyboard.getPrevious()
storyboard.purgeScene( prior_scene )
Try to use a tap listener instead of touch listener. I don't see your entire code but I think there is the problem.
main menu button in scene 1:
function scene:enterScene(event)
local group = self.view
local function onMenuButtonTap( event )
audio.play(click)
storyboard.gotoScene( "menu", "slideRight", 750 )
return true
end
end
play button on the main menu scene:
function scene:enterScene(event)
local group = self.view
local function onPlayTap( event )
audio.play(click)
local previousScene = storyboard.getPrevious()
if previousScene == nil then
storyboard.gotoScene( "scene1", "slideLeft", 750 )
else
storyboard.gotoScene(previousScene)
end
return true
end
end
------------------------------------------------------------
New code:
------------------------------------------------------------
change this in your scene1.lua
function scene:exitScene(event)
local group = self.view
storyboard.destroyScene( "scene1" )
end
with this
function scene:exitScene(event)
local group = self.view
end
In your menu.lua add this:
function scene:createScene( event )
local group = self.view
storyboard.purgeScene( "scene1" )
end
scene:addEventListener( "createScene", scene )

Resources