Corona SDK Tutorial: Passing Data Between Scenes

Posted by & filed under CoronaSDK, Tutorial.

Sometimes you need pass a few variable between scenes, for example if you have a level selection scene then you need to pass selected level number to your game scene. Here are the steps on how to do it.

Step 1: Create a structure that keeps our variables. Let’s create a new file and call it gamestate.lua
everything in the file should be self explanatory:

1
2
3
4
5
6
local M = {
level = 1,
score = 0
}

return M

Step 2: Add the gamestate to your level selection scene. In our case it’s the menu.lua file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
-- add our gamestate structure
local gameState = require( "gamestate" )

-- add Corona's Storyboard module
local storyboard = require( "storyboard" )

-- create a new scene
local scene = storyboard.newScene()

-- include Corona's "widget" library, we need it for buttons
local widget = require "widget"

-- forward declarations for our level selection buttons
-- needed to remove the buttons from the scene in destroyScene
local level1Btn, level2Btn

-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view

local label = display.newText(group, "Select level", 20, 20, "Verdana", 14 )
label:setTextColor( 255, 255, 255 )

-- create 2 buttons that load different levels
level1Btn = widget.newButton{
label="1",
labelColor = { default={255}, over={128} },
default="assets/images/button.png",
over="assets/images/button-over.png",
width=80, height=40,
onRelease = function()

-- This is the cream of this tutorial
-- set the level number to 1
gameState.level = 1

-- launch the game scene
storyboard.gotoScene( "game", "fade", 500 )
end
}
level1Btn.view:setReferencePoint( display.CenterReferencePoint )
level1Btn.view.x = 80
level1Btn.view.y = display.contentHeight * 0.5

level2Btn = widget.newButton{
label="2",
labelColor = { default={255}, over={128} },
default="assets/images/button.png",
over="assets/images/button-over.png",
width=80, height=40,
onRelease = function()

-- set the level number to 1
gameState.level = 2

-- launch the game scene
storyboard.gotoScene( "game", "fade", 500 )
end
}
level2Btn.view:setReferencePoint( display.CenterReferencePoint )
level2Btn.view.x = 240
level2Btn.view.y = display.contentHeight * 0.5

-- all display objects must be inserted into group
-- you must insert .view property for widgets
group:insert( level1Btn.view )
group:insert( level2Btn.view )
end

-- If scene's view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view

-- widgets must be manually removed, such as life
if level1Btn then
level1Btn:removeSelf()
level1Btn = nil
end

if level2Btn then
level2Btn:removeSelf()
level2Btn = nil
end

end

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", 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 )

--don't forget to return the scene
return scene

Step 3: And finally load game state inside your game scene, in this case game.lua file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-- add our gamestate structure
local gameState = require( "gamestate" )

-- add Corona's Storyboard module
local storyboard = require( "storyboard" )

-- create a new scene
local scene = storyboard.newScene()

-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view

-- LOOK HERE: this is where we're displaying the level number
local label = display.newText(group, ("You chose level #" .. gameState.level), 20, 20, "Verdana", 14 )
label:setTextColor( 0, 0, 0 )

-- create a white rectangle as the backdrop
local background = display.newRect( 0, 0, display.contentWidth , display.contentHeight )
background:setFillColor( 255 )

-- all display objects must be inserted into group
group:insert( background )
group:insert( label )
end

-- If scene's view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view

end

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", 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

Get the source code and assets from https://github.com/leetr/Corona-SDK-tutorials inside the DataBetweenScenes folder

Tags: , ,

One comment on “Corona SDK Tutorial: Passing Data Between Scenes

  1. Thanks a lot for this, really helped me out.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

1,744 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>