I have a lot of difficulties with composer...it's not so easy!
My previous scene is "game" (game.lua) everything is ok
When i go to my scene "recolt" with this snippet inside my game lua :
local function goTo()
print("gotoscene")
composer.gotoScene( "recolt", { time = 1000, effect = "fromRight", params = params } )
end
timer.performWithDelay( 1000, goTo )
:The problem is that i see my previous scene behind my recolt scene and i can't see my background but i see my circle who's moving ??? however my sceneGroup is correct :
sceneGroup:insert(background,circle)
When i do :
sceneGroup:insert(circle,background)
I see my background who hide my previous scene and my circle. > behavior expected
What's wrong? i would like to see my background and my circle in y "recolt" scene. Could you help me ? Thanks a lot...Below the snippet "recolt.lua".
--recolt.lua
local composer = require( "composer" )
local scene = composer.newScene()
function scene:create( event )
local sceneGroup = self.view
params = event.params
local background=display.newImageRect("back02.png",display.contentWidth*.5,d isplay.contentHeight*.5,320,480)
background.x,background.y=display.contentWidth*.5,display.contentHeight*.5
background.xScale,background.yScale=2,2
background.alpha=1
local circle = display.newCircle(120,100,100)
sceneGroup:insert(background,circle)
timeT=150
local function tr4()
transition.to(circle,{time=timeT,x=200,y=300,alpha=1,transition=easing.linear, xScale=1,yScale=1})
end
local function tr3()
transition.to(circle,{time=200,x=100,y=300,transition=easing.linear, xScale=.2 ,yScale=.5,onComplete=tr4})
end
local function tr2()
transition.to(circle,{time=200,x=200,y=295,transition=easing.linear, xScale=.2 ,yScale=.2,alpha=.2,onComplete=tr3})
end
local function tr1()
transition.to(circle,{time=timeT,x=300,y=300,transition=easing.linear, xScale= .5,yScale=.5,onComplete=tr2})
end
timer.performWithDelay(700,tr1,-1)
end
function scene:show( event )
local sceneGroup = self.view
params = event.params
if event.phase == "did" then
--physics.start()
end
end
function scene:hide( event )
local sceneGroup = self.view
if event.phase == "will" then
--
-- Remove enterFrame listeners here
--
--physics.stop()
end
end
function scene:destroy( event )
local sceneGroup = self.view
end
---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene
You need to check the docs: object:insert()
The problem is you can only add 1 object into the scene with insert() function, so you only need to change
sceneGroup:insert(background,circle)
to
sceneGroup:insert(background)
sceneGroup:insert(circle)
The order matters, the first object you create go back in the scene.
Related
Need your help here !
I receive that error when I try to reload my scene with the help of a "cut-scene" that I called loader.lua :
?:0: attempt to index a nil value
stack traceback:
?: in function '_isWithinBounds'
?: in function '?'
?: in function <?:449>
?: in function <?:205>
loader.lua :
local composer = require("composer")
local scene = composer.newScene()
function scene:create( event )
local sceneGroup = self.view
composer.removeScene("game")
end
function scene:show( event )
local sceneGroup = self.view
composer.gotoScene("game")
end
function scene:hide( event )
local sceneGroup = self.view
end
function scene:destroy( event )
local sceneGroup = self.view
end
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene
And finally the caller function :
function buttonEvent( event )
if ( "ended" == event.phase ) then
print("Clicked")
composer.gotoScene("loader")
end
end
I tried to cancel my timers and added everything to the sceneGroup self.view to clear the scene but I got this error every time!
Any help will be more than welcome !
Thanks !
EDIT:
Got rid of the
composer.removeScene("game")
That was calling a nil value and now my error went out but I got an empty black screen :/
And added
local sceneGroup = self.view
if (event.phase == did) then
composer.gotoScene("game")
end
To the scene:show function of loader.lua
I tried to set :
composer.recycleOnSceneChange
To True and False but it only changed the Debug screen Output..
The Debug screen shows me nothing when
composer.recycleOnSceneChange = false
But shows me :
15:57:55.433 COMPOSER:
15:57:55.433 A total of [1] scene(s) have been removed.
15:57:55.433
When
composer.recycleOnSceneChange = true
In game.lua everything is set in the scene:create function and the game logic is in the scene:show.
I have a little problem with corona SDK composer API. I got 3 .lua files- main.lua, character.lua and job.lua. The problem is, when I run the game, it automatically transits to character.lua and everything's fine. Then I'm transiting to job.lua and it works as well, but when I'm trying to come back to character.lua- nothing happens (the background doesn't change).That's how it look like:
main.lua:
local composer = require("composer")
composer.gotoScene("character")
character.lua:
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 widget = require("widget")
local bottomTabButtons ={
{ width=32, height=32, defaultFile="character.png", overFile="character_active.png", selected="true"},
{ width=32, height=32, defaultFile="job.png", overFile="job_active.png", onPress=function() composer.gotoScene( "job" )end},
}
local bottomBar = widget.newTabBar{
top = display.contentHeight-40,
buttons = bottomTabButtons
}
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
job.lua:
local composer = require( "composer" )
local widget = require("widget")
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 background = display.newImage("background.png")
background.x=display.contentCenterX
background.y=display.contentCenterY
background.height=display.contentHeight
background.width=display.contentWidth
local function gotoCharacter(event)
composer.gotoScene("character")
print('asd')
end
local bottomTabButtons ={
{ width=32, height=32, defaultFile="character.png", overFile="character_active.png", onPress=function() composer.gotoScene( "character" ) end},
{ width=32, height=32, defaultFile="job.png", overFile="job_active.png", selected="true"},
}
local bottomBar = widget.newTabBar{
top = display.contentHeight-40,
buttons = bottomTabButtons
}
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
Thanks in advance!
The problem is your not inserting your display objects in to the scenegroup. Thats why its not getting deleted. Insert all your objects in to the scenegroup.
For example,
local background = display.newImage("background.png")
background.x=display.contentCenterX
background.y=display.contentCenterY
background.height=display.contentHeight
background.width=display.contentWidth
sceneGroup:insert(background)
--- This is the change. Same way insert all the objects in to this group.
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
I am getting the below error in corona simulator. Nothing has changed in my files that I know of. ( I have included the main.lua code and the menuMain.lua code.) If you have any ideas on what might be causing this would be appricated! =) thanks.
Corona Simulator Runtime Error
File: ?
Attempt to call method 'dispatchEvent' (a nil value)
stack traceback:
[c] in function 'dispatchEvent'
? in funciton gotoScene
...dwegrecki/main.lua:16 in main chunck
-- main.lua
-- Hide Status Bar
display.setStatusBar(display.HiddenStatusBar)
-- require controller module
local storyboard = require "storyboard"
-- load first screen
storyboard.gotoScene( "menuMain" )
-- menuMain.lua
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
---------------------------------------------------------------------------------
-- BEGINNING OF IMPLEMENTATION
---------------------------------------------------------------------------------
local mainBg
local titleIcon
local startBtn
local aboutBtn
local onStartBtnTouch = {}
local scene = {}
-- Touch event listener for startBtn
local function onStartBtnTouch( self, event )
if event.phase == "began" then
storyboard.gotoScene( "menuLevel", "fade", 400 )
return true
end
end
-- Touch event listener for cerditsBtn
local function onCreditsBtnTouch( self, event )
if event.phase == "began" then
storyboard.gotoScene( "zerocredits", "fade", 400 )
return true
end
end
-- Called when the scene's view does not exist:
function scene:createScene( event )
local screenGroup = self.view
mainBg = display.newImage('bg.png')
titleBg = display.newImage('title.png', 0, 100)
playBtn = display.newImage('playBtn.png', 200, 240)
creditsBtn = display.newImage('creditsBtn.png', 200, 290)
titleView = display.newGroup(titleBg, playBtn, creditsBtn)
screenGroup:insert( titleBg, playBtn, creditsBtn )
playBtn.touch = onStartBtnTouch
creditsBtn.touch = onCreditsBtnTouch
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
print( "1: enterScene event" )
-- remove previous scene's view
storyboard.purgeScene( "LevelMenu" )
storyboard.purgeScene( "credits" )
storyboard.purgeScene("main")
end
-- Called when scene is about to move offscreen:
function scene:exitScene( event )
print( "1: exitScene event" )
-- remove touch listener for image
mainBg:removeEventListener( "touch", mainBg )
end
-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
print( "((destroying scene 1's view))" )
end
---------------------------------------------------------------------------------
-- END OF IMPLEMENTATION
---------------------------------------------------------------------------------
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
---------------------------------------------------------------------------------
return scene
i recreate same project like your's and i'm getting an error on local scene = {} why did you make the variable scene a table ?
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.