-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.lua
240 lines (192 loc) · 6.66 KB
/
menu.lua
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
-----------------------------------------------------------------------------------------
--
-- menu.lua
--
-----------------------------------------------------------------------------------------
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require( "physics" )
-- include Corona's "widget" library
local widget = require "widget"
-- local scripts
local btnanimations = require( "scripts.btnanimations" )
local audiohandler = require( "scripts.audiohandler" )
--------------------------------------------
-- forward declarations and other locals
local playBtn
local shareBtn
local soundBtn
local imgSoundOn
local imgSoundOff
-- 'onRelease' event listener for playBtn
local function onPlayBtnRelease()
-- go to level1.lua scene
composer.gotoScene( "level1", "crossFade", 250 )
return true -- indicates successful touch
end
local function onShareBtnRelease()
-- show share btn
native.showPopup( "social" )
return true
end
local function onPlayBtnTouch( event )
btnanimations.shrinkBtnAnimation( event, playBtn )
end
local function onSoundBtnTouch( event )
btnanimations.shrinkBtnAnimation( event, soundBtn )
end
local function onSoundOnBtnTap( event )
-- turn off sound
if soundBtn.sound then
soundBtn.sound = false
soundBtn.fill = imgSoundOff
else
soundBtn.sound = true
soundBtn.fill = imgSoundOn
end
local appPreferences = {
prefSound = soundBtn.sound
}
system.setPreferences( "app", appPreferences )
return true
end
function scene:create( event )
local sceneGroup = self.view
physics.start()
physics.pause()
physics.setScale( 60 )
--physics.setDrawMode("hybrid")
-- 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.
-- display a background image
local background = display.newImageRect( "graphics/intro-bg.png", display.actualContentWidth, display.actualContentHeight )
background.anchorX = 0
background.anchorY = 0
background.x = 0 + display.screenOriginX
background.y = 0 + display.screenOriginY
-- create a logo
local titleLogo = display.newImageRect( "graphics/logo.png", 170, 25 )
titleLogo.x = display.contentCenterX
titleLogo.y = 150
titleLogo.alpha = 0
transition.to( titleLogo, { time = 1500, xScale = 1.2, yScale = 1.1, alpha = 1 } )
-- create a widget button (which will loads level1.lua on release)
playBtn = widget.newButton{
label="",
labelAlign="center",
labelColor = { default={255}, over={128} },
defaultFile="graphics/play-btn.png",
overFile="graphics/play-btn.png",
width=140, height=40,
onRelease = onPlayBtnRelease -- event listener function
}
playBtn.x = display.contentCenterX
playBtn.y = display.contentHeight - 125
playBtn.name = "playBtn"
playBtn:addEventListener( "touch", onPlayBtnTouch )
-- create a widget button (which wil show the share menu)
shareBtn = widget.newButton{
label="",
labelAlign="center",
labelColor = { default={255}, over={128} },
defaultFile="graphics/share-btn.png",
overFile="graphics/share-btn.png",
width=140, height=40,
onRelease = onShareBtnRelease -- event listener function
}
shareBtn.x = display.contentCenterX
shareBtn.y = display.contentHeight - 60
-- create sound on & off buttons
imgSoundOn = { type="image", filename="graphics/btn-sound-on.png" }
imgSoundOff = { type="image", filename="graphics/btn-sound-off.png" }
soundBtn = display.newRect( display.contentWidth - 30, 20, 30, 30 )
soundBtn.fill = system.getPreference( "app", "prefSound", "boolean" ) and imgSoundOn or imgSoundOff
soundBtn.sound = true
soundBtn:addEventListener( "tap", onSoundOnBtnTap )
soundBtn:addEventListener( "touch", onSoundBtnTouch )
-- show highscore
local highscore = system.getPreference( "app", "highscore_level1", "number" )
if highscore == nil then highscore = 0 end
local highScoreText = display.newText {
text = "Highscore: " .. tostring(highscore),
x = display.contentCenterX,
y = 20,
font = "pixelsplitter.ttf",
fontSize = 10,
align = "left"
}
-- create a physics body
local playBtnBody = display.newRect(display.contentCenterX, display.contentHeight - 125, 135, 35)
playBtnBody.alpha = 0
physics.addBody( playBtnBody, "static", { density = 1, friction = 1 } )
-- load the ball
local ball = display.newImageRect( "graphics/ball.png", 60, 60 )
ball.x = display.contentCenterX + math.random(-20, 20)
ball.y = 60
local function addBodyToBall()
physics.addBody( ball, { bounce = 0.5, friction = 0.5, density = 1.0, radius = 20.0})
ball:applyAngularImpulse( math.random ( -5 , 5 ) )
end
local function onCollision(self, event)
local function goUp()
if playBtn ~= nil then
transition.to( playBtn, { time = 200, y = playBtn.y - 15 } )
ball:applyLinearImpulse( 0, -1, ball.x, ball.y )
end
end
transition.to( playBtn, { time = 50, y=playBtn.y + 15, onComplete=goUp, transition=easing.outExpo } )
end
ball.collision = onCollision
ball:addEventListener( "collision" )
timer.performWithDelay( 250, addBodyToBall )
-- all display objects must be inserted into group
sceneGroup:insert( background )
sceneGroup:insert( titleLogo )
sceneGroup:insert( playBtn )
sceneGroup:insert( shareBtn )
sceneGroup:insert( ball )
sceneGroup:insert( playBtnBody )
sceneGroup:insert( soundBtn )
sceneGroup:insert( highScoreText )
-- ball in front
ball:toFront()
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
physics.start()
composer.removeHidden()
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
elseif phase == "did" then
-- Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
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