Cannot 'gotoscene' with Corona SDK - lua

I have tried, tried and tried. But cannot get this to work.
All I have it main.lua with a button in it. I then want this to go to about.lua.
My main.lua is:
local function about(event)
storyboard.gotoScene( "about", {"Fade", 500} )
return true
end
local about = widget.newButton
{
top = 280,
width = 320,
height = 66,
defaultFile = "about.png",
overFile = "aboutdown.png",
onRelease = about
}
And my about.lua is:
local storyboard = require ( "storyboard" )
local scene = storyboard.newScene()
local widget = require ( "widget" )
local background = display.newImage( "logo.png" )
background.x = display.contentCenterX
background.y = display.contentCenterY
Please help!

If you look at Storyboard API you will see a template code you need to use to create a scene, copy all the template code to your about.lua and include your actual code in the createScene function, it should work.
First step: you create a new about.lua using the template code.
Second step: add your code in the createScene function like this, background and widget are forward declarations so they are before function calls
local widget = require ( "widget" )
local background
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
-----------------------------------------------------------------------------
-- CREATE display objects and add them to 'group' here.
-- Example use-case: Restore 'group' from previously saved state.
-----------------------------------------------------------------------------
background = display.newImage( "logo.png" )
background.x = display.contentCenterX
background.y = display.contentCenterY
end

Related

Transitioning Storyboard Effect in Corona SDK not Transitioning Properly

In my code, I have am using the storyboard to transition between two .lua files. However, the effect used to transition between them is not present even though the transition itself is taking place. So essentially, the storyboard succesuflly transitions between the two files, but is accompanied with none of the effects specified. Does anyone know why this is happening?
First Scene:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
------------------------------------------Set Background
local backgroundFillColor = {}
--Red RGB
backgroundFillColor[1] = 76/255
--Greeb RGB
backgroundFillColor[2] = 217/255
--Blue RGB
backgroundFillColor[3] = 100/255
display.setDefault("background",backgroundFillColor[1],backgroundFillColor[2],backgroundFillColor[3])
local function changeScene( event )
-- body
if event.phase == "ended" then
storyboard.gotoScene( "scene1", "fade", 500 ) -- go to levels scene
end
end
Runtime:addEventListener( "touch", changeScene )
return scene
Second Scene:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
------------------------------------------Set Background
local backgroundFillColor = {}
--Red RGB
backgroundFillColor[1] = 0/255
--Greeb RGB
backgroundFillColor[2] = 0/255
--Blue RGB
backgroundFillColor[3] = 0/255
display.setDefault("background",backgroundFillColor[1],backgroundFillColor[2],backgroundFillColor[3])
local function changeScene( event )
-- body
if event.phase == "ended" then
storyboard.gotoScene( "scene", "fade", 500 ) -- go to levels scene
end
end
Runtime:addEventListener( "touch", changeScene )
return scene
Main:
display.setStatusBar( display.HiddenStatusBar )
-- include the Corona "storyboard" module
local storyboard = require "storyboard"
storyboard.gotoScene( "scene", "fade", 5000 ) -- go to levels scene
You must insert any display object that you want Storyboard to manage (i.e. transition around) into the scene's "view" group. Normally you create all of these objects in the createScene() function where you setup a reference to the view, i.e.
function scene:createScene( event )
local group = self.view
local background = display.newImageRect("somebackgroundimage.png", width, height)
group:insert( background )
...
end
The group:insert( background ) is critical for the functioning of Storyboard (and Composer)

Issues with Corona Storyboard Scenes

I am trying to make a card game.. I am creating a scene, making the background,and adding an image where when the player touches he/she will be transfered to the next scene.
singlePlayer scene:
local storyboard = require("storyboard")
local singlePlayer = storyboard.newScene()
local card1,card2,card3
function singlePlayer:createScene(event )
local group = self.view
-- body
local bg = display.newImage("bg.png")
bg.x = 100 ; bg.y = 50
group:insert(bg)
end
function singlePlayer:enterScene( event )
local group = self.view
local count = math.random(3)
local storyboard = require("storyboard")
local singlePlayer = storyboard.newScene()
local card1,card2,card3
function singlePlayer:createScene(event )
local group = self.view
-- body
local bg = display.newImage("bg.png")
bg.x = 100 ; bg.y = 50
group:insert(bg)
end
function singlePlayer:enterScene( event )
local group = self.view
local count = math.random(3)
if(count == 1) then
card1 = display.newImage("attack.png")
card1.x = 50 ; card1.y = 150
group:insert(card1)
else
card1 = display.newImage("ability.png")
card1.x = 50 ; card1.y = 150
group:insert(card1)
end
function card1:touch(event )
print("ok")
if(event.phase == "ended") then
storyboard.gotoScene("opponent_scene")
else
end
-- body
end
card1:addEventListener("touch",card1)
-- body
end
function singlePlayer:exitScene(event)
local group = self.view
card1:removeEventListener("touch",card1)
end
singlePlayer:addEventListener("createScene",singlePlayer)
singlePlayer:addEventListener("enterScene",singlePlayer)
singlePlayer:addEventListener("exitScene",singlePlayer)
return singlePlayer
Opponent scene:
local storyboard = require("storyboard")
local opponent_scene = storyboard.newScene()
function opponent_scene:createScene(event )
print("opponent_scene created")
-- body
end
function opponent_scene:enterScene(event )
print("opponent_scene enter")
local group = self.view
storyboard.removeScene("judge")
local text = display.newText("Opponent's turn",150,200)
storyboard.gotoScene("judge")
-- body
end
function opponent_scene:exitScene(event )
-- body
local group = self.view
end
opponent_scene:addEventListener("createScene",opponent_scene)
opponent_scene:addEventListener("enterScene",opponent_scene)
opponent_scene:addEventListener("exitScene",opponent_scene)
return opponent_scene
Judge scene:
local storyboard = require("storyboard")
local judge = storyboard.newScene()
function judge:createScene(event )
local group = self.view
local bg = display.newImage("destiny.png")
storyboard.removeScene("opponent_scene")
storyboard.gotoScene("singlePlayer")
-- body
end
judge:addEventListener("createScene",judge)
return judge
Will anyone explain to me what is going on with these scenes?
All i want is to make the game wait for the player's input (touching of the card)
After two clicks on the icon, storyboard is taken to opponent scene and it just shows on the screen the text "opponent's turn". What I want to do is for the text to appear briefly and then the scene to be taken to the player scene
Move your scene changing code inside a delayed function with timer.delay.. Here is what I did for my game to change screen after a brief time..
local function onSceneTouch( self, event )
if event.phase == "began" then
-- write all your other code here
-- function to change screen
function myClosure()
storyboard.gotoScene("opponent_scene")
end
-- Delay the call of closure function by 2 second (2000 milliseconds)
timer.performWithDelay( 2000, myClosure, 1 )
end
end
then call onSceneTouch function on touch of the screen
The single player enterscene code is wrong ,because you cant have two global function with the same name, the approach is not the right one . why do you call create scene inside the enter frame. suppose what you have done is correct then you should add event listeners to the function which you create in the enterscene as did earlier
singlePlayer:addEventListener("createScene",singlePlayer)
singlePlayer:addEventListener("enterScene",singlePlayer)
singlePlayer:addEventListener("exitScene",singlePlayer)
First of all please read the documentation of the storyboard and look the sample.
http://docs.coronalabs.com/api/library/storyboard/
But i request you to use composer instead of storyboard : http://docs.coronalabs.com/api/library/composer/index.html

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.

Corona storyboard keeping previous scene open

I'm attempting to switch scenes with the storyboard, but when I press the button, it keeps the current scene... I haven't been able to find a solution to this issue anywhere on the internet. The terminal indicates that it is getting to the method that prints out "leaving main menu", and my next scene is successfully opened, but the background and button remain from the original scene.
local storyboard = require( "storyboard" )
local widget = require "widget"
local scene = storyboard.newScene()
local function onButton(event)
--local btn = event.target
--storyboard.gotoScene("sceneTemplate")
if event.phase == "release" then
print("play pressed")
storyboard.gotoScene("sceneTemplate")
end
end
function scene:createScene( event )
local group = self.view
print("menu scene created")
end
function scene:enterScene( event )
local group = self.view
print("menu scene viewing!")
local bgImage = display.newImage("images/mainBG.png",0,0);
local playButton = widget.newButton{
default = "images/playUp.png",
over = "images/playDown.png",
onEvent = onButton
}
playButton.x = 80
playButton.y = 20
end
function scene:exitScene( event )
local group = self.view
print("leaving main menu")
storyboard.removeScene("menu")
storyboard.removeAll()
display.remove(group)
group:removeSelf()
end
I found a solution! If I add both the background image and the button to a new display group, and then remove them upon the scene exiting, it works out:
function scene:enterScene( event )
local group = self.view
print("menu scene viewing!")
local bgImage = display.newImage("images/mainBG.png",0,0);
local playButton = widget.newButton{
default = "images/playUp.png",
over = "images/playDown.png",
onEvent = onButton
}
displayGroup:insert(bgImage)
displayGroup:insert(playButton)
end
function scene:exitScene( event )
local group = self.view
print("leaving main menu")
display.remove(displayGroup)
storyboard.removeScene("menu")
end
Please refer the below link-
http://docs.coronalabs.com/api/library/storyboard/removeScene.html
This can help you.

In Corona SDK the background image always cover other images

I'm currently making a tower defense game with Corona SDK. However, while I'm making the gaming scene, The background scene always cover the monster spawn, I've tried background:toBack() ,however it's doesn't work.Here is my code:
module(..., package.seeall)
function new()
local localGroup = display.newGroup();
local level=require(data.levelSelected);
local currentDes = 1;
monsters_list = display.newGroup()
--The background
local bg = display.newImage ("image/levels/1/bg.png");
bg.x = _W/2;bg.y = _H/2;
bg:toBack();
--generate the monsters
function spawn_monster(kind)
local monster=require("monsters."..kind);
newMonster=monster.new()
--read the spawn(starting point) in level, and spawn the monster there
newMonster.x=level.route[1][1];newMonster.y=level.route[1][2];
monsters_list:insert(newMonster);
localGroup:insert(monsters_list);
return monsters_list;
end
function move(monster,x,y)
-- Using pythagoras to calauate the moving distace, Hence calauate the time consumed according to speed
transition.to(monster,{time=math.sqrt(math.abs(monster.x-x)^2+math.abs(monster.y-y)^2)/(monster.speed/30),x=x, y=y, onComplete=newDes})
end
function newDes()
currentDes=currentDes+1;
end
--moake monster move according to the route
function move_monster()
for i=1,monsters_list.numChildren do
move(monsters_list[i],200,200);
print (currentDes);
end
end
function agent()
spawn_monster("basic");
end
--Excute function above.
timer2 = timer.performWithDelay(1000,agent,10);
timer.performWithDelay(100,move_monster,-1);
timer.performWithDelay(10,update,-1);
move_monster();
return localGroup;
end
and the monster just stuck at the spawn point and stay there.
but, When i comment these 3 lines of code:
--local bg = display.newImage ("image/levels/1/bg.png");
--bg.x = _W/2;bg.y = _H/2;
--bg:toBack();
The problem disappear
Any ideas??Thanks for helping
Since you are using director, you should insert all your display objects into the localGroup.
You haven't inserted bg into localGroup.
SO director class inserts bg finally after inserting localGroup.
Modify your code as
--The background
local bg = display.newImage (localGroup,"image/levels/1/bg.png");
bg.x = _W/2;bg.y = _H/2;
bg:toBack();
or add the code
localGroup:insert(bg)
In more recent versions of Corona SDK:
Composer is the official scene (screen) creation and management library in Corona SDK.... The primary object in the Composer library is the scene object...and it contains a unique self.view.... This self.view is where you should insert visual elements pertaining to the scene.
So now in your scene:create() method, you should insert all DisplayObjects into self.view. It looks like this:
local composer = require( "composer" )
local scene = composer.newScene()
function scene:create()
local sceneGroup = self.view
local bg = display.newImage( ...
bg.x, bg.y = ...
local dude = display.newImage( ...
dude.x, dude.y = ....
sceneGroup:insert(bg)
sceneGroup:insert(dude)
end
The layering of DisplayObjects in this sceneGroup depends on the order in which they are added to the group, with the object added last on top. You can control this ordering with the toBack() and toFront() methods.

Resources