forked from eudaimyst/Solar-2D-Particle-Designer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle_designer.lua
589 lines (494 loc) · 25.3 KB
/
particle_designer.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
-----------------------------------------------------------------------------------------
--
-- particle_designer.lua
--
-----------------------------------------------------------------------------------------
--common modules - solar2d
local composer = require("composer")
local physics = require("physics")
local easing = require("lib.easing")
local lfs = require("lfs")
local json = require("json")
--common modules
local g = require("lib.globals")
local util = require("lib.utilities")
local debug = require("lib.debug")
local key = require("lib.key_input")
--other modules
local mouse = require("lib.mouse_input")
local editor = require("lib.editor")
--create scene
local scene = composer.newScene()
local sceneGroup = display.newGroup()
local emitterGroup = display.newGroup()
local spawnPointGroup = display.newGroup( )
-- Change current working directory
local folderName = "Solar2D Particle Designer"
lfs.chdir( system.pathForFile( "", system.DocumentsDirectory ) )
lfs.mkdir( folderName )
jsonParamsPath = lfs.currentdir() .. "/" .. folderName
print("JSON PATH!!!!!!! = "..jsonParamsPath)
local emitterParams = { --params used by point and radial
maxParticles = 100,
angle = -90, angleVariance = 180,
emitterType = 0, --0 for point, 1 for radial
absolutePosition = false,
duration = -1, --seconds before removed
textureFileName = "content/particles/skull.png",
particleLifespan = 2, particleLifespanVariance = 0,
startParticleSize = 64, startParticleSizeVariance = 0,
finishParticleSize = 64, finishParticleSizeVariance = 0,
rotationStart = 0, rotationStartVariance = 0,
rotationEnd = 0, rotationEndVariance = 0,
blendFuncSource = 770, blendFuncDestination = 1,
speed = 100, speedVariance = 0,
sourcePositionVariancex = 0, sourcePositionVariancey = 0,
gravityx = 0, gravityy = 0,
radialAcceleration = 0, radialAccelVariance = 0,
tangentialAcceleration = 0, tangentialAccelVariance = 0,
maxRadius = 0, maxRadiusVariance = 0,
minRadius = 200, minRadiusVariance = 0,
rotatePerSecond = 0, rotatePerSecondVariance = 0
}
local emitterRestartParams = { "name", "textureFileName", "maxParticles", "blendFuncSource", "blendFuncDestination", "duration" } --list of params that require emitter to be restarted to take effect
local shortColorParams = { --particle color params
startColor = 0, startColorVariance = 0, finishColor = 0, finishColorVariance = 0
}
local appendColorParams = { Red = 0, Green = 0, Blue = 0, Alpha = 0 } --get added to short version
local particleColorParams = {}--final array holds all the color params
for shortKey, _ in pairs(shortColorParams) do
for appendKey, _ in pairs(appendColorParams) do
particleColorParams[tostring(shortKey)..tostring(appendKey)] = 0 --sets all variance color in short and append tables to 0
end
end
local blendModes = {
{name = "zero", value = 0}, {name = "one", value = 1}, {name = "dst color", value = 774}, {name = "one - dst color", value = 775},
{name = "src alpha", value = 770}, {name = "one - src alpha", value = 771}, {name = "dst alpha", value = 772}, {name = "one - dst alpha", value = 773},
{name = "src alpha sat", value = 776}, {name = "src color", value = 768}, {name = "one - src color", value = 769},
}
local particleFiles = { }
local fileCounter = 1
for file in lfs.dir( system.pathForFile(system.ResourceDirectory).."/content/particles/" ) do
--print("Found file: ".. file)
particleFile = {}
particleFile.name = file
particleFile.value = "content/particles/"..file
if (file ~= "." and file ~= "..") then
particleFiles[fileCounter] = particleFile
fileCounter = fileCounter + 1
end
end
local toolbarWindow --set when toolbar is created, only ever one
local emitterStore = {}
local function createEmitter() --used to pass paramaters if emitter is loaded
local emitter = {}
emitter.id = #emitterStore + 1
emitterStore[emitter.id] = emitter
function emitter.initParams() --adds the color params to the emitter params
local params = {}
params.name = "emitter"..emitter.id --sets a name for the emitter
for k, v in pairs(emitterParams) do
params[k] = v
end
for k, v in pairs(particleColorParams) do
if ( ( string.find(k, "startColor") or string.find(k, "finishColor") ) and not string.find(k, "Alpha") and not string.find(k, "Variance") ) then
params[k] = math.round(math.random() * 100)/100
elseif (string.find(k, "startColorAlpha")) then
params[k] = 1
else
params[k] = v
end
end
return params
end
emitter.params = emitter.initParams()
function emitter:createSaveTextWindow()
local objectParamsString = self.params.name.."Params = {\n"
for k, v in pairs(self.params) do
if (k ~= "name") then --skip name as it's not an actual emitter param
objectParamsString = objectParamsString.." "..tostring(k).." = "..tostring(v)..",\n"
end
end
objectParamsString = objectParamsString.."}"
local sectionData = { [1] = { label = "paramaters (copy + paste)", fullHeight = true, collapsable = false, elements = {
[1] = { { eType = editor.elementTypes.multiLineText, content = objectParamsString, editable = true } },
} } }
local width, height = display.actualContentWidth / 2, display.actualContentHeight * 3 / 4
local windowParams = {
x = width / 2, y = height / 4, width = width, height = height, label = "Save to text",
closable = true, movable = true, sceneGroup = sceneGroup, sectionData = sectionData, object = self
}
self.saveTextWindow = editor.createWindow( windowParams, sceneGroup )
end
function emitter:createLoadTextWindow()
local function loadParamsFromString()
local loadedParams = {} --stores params from string
local paramString = self.loadTextWindow.sectionStore[1].elementStore[2].textBox.text --gets the string from the textbox in the window (hardcoded using data below)
for k, v in string.gmatch( paramString, "([%w%.%/]+) = ([%w%.%/]+)" ) do --wow amazing string lib
--print("setting from load: "..k..", "..v)
if (k == "textureFileName") then --keep filename as a string
loadedParams[k] = v
else
loadedParams[k] = tonumber(v)
end
end
for param, value in pairs(loadedParams) do
--print("setting param: "..param.." to value: "..value.." from string load function")
self:updateParam(param, value)
end
self:restart() --restarts the emitter after load
end
local sectionData = { [1] = { label = "paramaters (copy + paste)", fullHeight = true, collapsable = false, elements = {
[1] = { { eType = editor.elementTypes.button, text = "Load", clickListener = loadParamsFromString} },
[2] = { { eType = editor.elementTypes.multiLineText, editable = true } }
} } }
local width, height = display.actualContentWidth / 2, display.actualContentHeight * 3 / 4
local windowParams = {
x = width / 2, y = height / 4, width = width, height = height, label = "Load from text",
closable = true, movable = true, sceneGroup = sceneGroup, sectionData = sectionData, object = self
}
self.loadTextWindow = editor.createWindow( windowParams, sceneGroup )
end
function emitter:createLoadJsonWindow()
local jsonParamFiles = {}
local fileCounter = 1
local fileToLoad
print("searching for files in: "..jsonParamsPath)
for file in lfs.dir( jsonParamsPath ) do
if (file ~= "." and file ~= "..") then
--print("Found file: ".. file)
jsonParamFiles[fileCounter] = file
fileCounter = fileCounter + 1
end
end
local function setFile(fileName) --called when item is selected in dropdown
fileToLoad = fileName
end
local function loadParamsFromFile() --called when load button is pressed
local file, errorString = io.open( jsonParamsPath.."/"..fileToLoad, "r" ) -- Open the file handle
if not file then
--print( "File error: " .. errorString ) -- Error occurred; output the cause
else
local jsonContents = file:read( "*a" ) -- Read data from file
--print( "Contents of file: \n" .. jsonContents ) -- Output the file contents
local loadedParams = json.decode( jsonContents )
for param, value in pairs(loadedParams) do
self:updateParam(param, value)
end
io.close( file ) -- Close the file handle
end
file = nil
self:restart() --restarts the emitter after load
end
local sectionData = { [1] = { label = "replace "..self.params.name.." params", collapsable = false, elements = {
[1] = { { label = "File name", eType = editor.elementTypes.dropdown, selectListener = setFile, table = jsonParamFiles } },
[2] = { { eType = editor.elementTypes.button, text = "Load", clickListener = loadParamsFromFile} }
} } }
local width, height = 200, 100
local windowParams = {
x = display.contentCenterX - width / 2, y = display.contentCenterY - height / 2, width = width, height = height, label = "Load from json",
closable = true, movable = true, sceneGroup = sceneGroup, sectionData = sectionData, object = self
}
self.loadJsonWindow = editor.createWindow( windowParams, sceneGroup )
end
function emitter.windowClosed(window) --called from editor when *any* window is closed, to nil necessary values for windows that need to be re-opened
if (window == emitter.settingsWindow) then
emitter.settingsWindow = nil
end
end
function emitter:updateParam(param, value)
--print("updating param: "..param.." to: "..value)
if self.emitterObject then --emitter exists, try to pass the param straight to it
self.emitterObject[param] = value
end
self.params[param] = value
--print ("updated "..param.." to "..tostring(value))
for k, v in pairs(self.params) do
--print(tostring(k).." = "..tostring(v))
end
if (param == "name") then editor.toolbarObjectList:update() end
for i = 1, #emitterRestartParams do
if param == emitterRestartParams[i] then
print("param.."..param.."..changed, restarting emitter")
self:restart()
end
end
self.settingsWindow:paramValueChanged(param, value)
end
function emitter:createWindowSettings()
local function startPressed()
if (self.emitterObject) then
self.emitterObject:start()
--print("emitter exists, starting emitter")
else
self:createObject()
self.emitterObject:start()
end
end
local function pausePressed()
if (self.emitterObject) then
self.emitterObject:pause( )
--print("pausing emitter")
end
end
local function stopPressed()
if (self.emitterObject) then
self.emitterObject:stop()
self.group:removeSelf()
self.emitterObject = nil
--print("stopping emitter")
end
end
local function updateParam(param, value) --called by editor when a parameter updates
self:updateParam(param, value) --moved contents to emitter so can be accessed when loading files
end
local function setAbsPosTrue()
updateParam("absolutePosition", true)
end
local function setAbsPosFalse()
updateParam("absolutePosition", false)
end
local function setTypePoint()
self.settingsWindow:setSectionVisibility("Point", true)
self.settingsWindow:setSectionVisibility("Radial", false)
updateParam("emitterType", 0)
self:restart()
end
local function setTypeRadial()
self.settingsWindow:setSectionVisibility("Radial", true)
self.settingsWindow:setSectionVisibility("Point", false)
updateParam("emitterType", 1)
self:restart()
end
local function showSpawnPoint()
emitter.spawnPoint.isVisible = true
end
local function hideSpawnPoint()
emitter.spawnPoint.isVisible = false
end
local function textSave()
self:createSaveTextWindow()
end
local function textLoad()
self:createLoadTextWindow()
end
local function jsonSave()
local saveData = json.encode( self.params , {indent = true} )
--print(saveData)
local filePath = jsonParamsPath.."/"..self.params.name..".json"
local file = nil
if ( filePath ) then
file, errorString = io.open( filePath, "w" )
end
if not file then
print( "File error: " .. errorString ) -- Error occurred; output the cause
else
file:write( saveData ) --write data to file
io.close( file ) --close file handle
end
file = nil --free memeory
saveData = nil
end
local function jsonLoad()
self:createLoadJsonWindow()
end
local t = editor.elementTypes --for readability
self.settingsWindowSections = { --sections in the settings window that hold elements, uses index for ordering in ui
[1] = { label = "Controls", elements = {
[1] = { { label = "Emitter", eType = t.toggleButtons, amount = 3, texts = { "start", "stop", "pause" }, clickListener = { startPressed, stopPressed, pausePressed } } }, --start, stop, pause
[2] = { { param = "name", label = "Name", width = 100, eType = t.inputField, inputListener = updateParam, inputType = "text" } },
[3] = { { label = "Save", eType = editor.elementTypes.button, text = "text", clickListener = textSave}, { eType = editor.elementTypes.button, text = "json", clickListener = jsonSave},
{ label = "Load", eType = editor.elementTypes.button, text = "text", clickListener = textLoad}, { eType = editor.elementTypes.button, text = "json", clickListener = jsonLoad} },
[4] = { { label = "Spawn Point", eType = t.toggleButtons, amount = 2, texts = { "show", "hide" }, clickListener = { showSpawnPoint, hideSpawnPoint } } },
} },
[2] = { label = "General", elements = {
[1] = { {param = "textureFileName", label = "Texture", eType = t.dropdown, selectListener = updateParam, table = particleFiles } },
[2] = { {param = "angle", label = "Angle", eType = t.inputField, inputListener = updateParam }, { param = "angleVariance", label = "Var", eType = t.inputField, inputListener = updateParam } },
[3] = { {param = "emitterType", label = "Emitter type", eType = t.toggleButtons, amount = 2, texts = {"point", "radial"}, values = {0, 1}, clickListener = { setTypePoint, setTypeRadial } } },
[4] = { {param = "absolutePosition", label = "Absolute Pos", eType = t.toggleButtons, amount = 2, texts = {"true", "false"}, values = {true, false}, clickListener = { setAbsPosTrue, setAbsPosFalse } } },
[5] = { {param = "duration", label = "Duration", eType = t.inputField, inputListener = updateParam } },
} },
[3] = { label = "Point", elements = {
[1] = { {param = "speed", label = "Speed", eType = t.inputField, inputListener = updateParam }, {param = "speedVariance", label = "Var", eType = t.inputField, inputListener = updateParam } },
[2] = { {param = "sourcePositionVariancex", label = "SrcPos VarX", eType = t.inputField, inputListener = updateParam }, {param = "sourcePositionVariancey", label = "SrcPos VarY", eType = t.inputField, inputListener = updateParam } },
[3] = { {param = "gravityx", label = "Gravity X", eType = t.inputField, inputListener = updateParam }, {param = "gravityy", label = "Gravity Y", eType = t.inputField, inputListener = updateParam } },
[4] = { {param = "radialAcceleration", label = "Radial Accel", eType = t.inputField, inputListener = updateParam }, {param = "radialAccelVariance", label = "Var", eType = t.inputField, inputListener = updateParam } },
[5] = { {param = "tangentialAcceleration", label = "Tangent Accel", eType = t.inputField, inputListener = updateParam }, {param = "tangentialAccelVariance", label = "Var", eType = t.inputField, inputListener = updateParam } },
} },
[4] = { label = "Radial", startHidden = true, elements = {
[1] = { {param = "maxRadius", label = "Max Radius", eType = t.inputField, inputListener = updateParam }, {param = "maxRadiusVariance", label = "Var", eType = t.inputField, inputListener = updateParam } },
[2] = { {param = "minRadius", label = "Min Radius", eType = t.inputField, inputListener = updateParam }, {param = "minRadiusVariance", label = "Var", eType = t.inputField, inputListener = updateParam } },
[3] = { {param = "rotatePerSecond", label = "Rotations/sec", eType = t.inputField, inputListener = updateParam }, {param = "rotatePerSecondVariance", label = "Var", eType = t.inputField, inputListener = updateParam } }
} },
[5] = { label = "Particles", elements = {
[1] = { {param = "maxParticles", label = "Max Particles", eType = t.inputField, inputListener = updateParam } },
[2] = { {param = "particleLifespan", label = "Lifespan", eType = t.inputField, inputListener = updateParam }, {param = "particleLifespanVariance", label = "Var", eType = t.inputField, inputListener = updateParam } },
[3] = { {param = "startParticleSize", label = "Start Size", eType = t.inputField, inputListener = updateParam }, {param = "startParticleSizeVariance", label = "Var", eType = t.inputField, inputListener = updateParam } },
[4] = { {param = "finishParticleSize", label = "End Size", eType = t.inputField, inputListener = updateParam }, {param = "finishParticleSizeVariance", label = "Var", eType = t.inputField, inputListener = updateParam } },
[5] = { {param = "rotationStart", label = "Start Rot", eType = t.inputField, inputListener = updateParam }, {param = "rotationStartVariance", label = "Var", eType = t.inputField, inputListener = updateParam } },
[6] = { {param = "rotationEnd", label = "End Rot", eType = t.inputField, inputListener = updateParam }, {param = "rotationEndVariance", label = "Var", eType = t.inputField, inputListener = updateParam } },
[7] = { {param = "blendFuncSource", label = "Blend Src", eType = t.dropdown, selectListener = updateParam, table = blendModes } },
[8] = { {param = "blendFuncDestination", label = "Blend Dest", eType = t.dropdown, selectListener = updateParam, table = blendModes } }
} },
[6] = { label = "Colours", elements = {
[1] = { {param = "startColorRed", label = "Start R", eType = t.inputField, inputListener = updateParam }, {param = "startColorGreen", label = "G", eType = t.inputField, inputListener = updateParam },
{param = "startColorBlue", label = "B", eType = t.inputField, inputListener = updateParam }, {param = "startColorAlpha", label = "A", eType = t.inputField, inputListener = updateParam } },
[2] = { {param = "startColorVarianceRed", label = "Var R", eType = t.inputField, inputListener = updateParam }, {param = "startColorVarianceGreen", label = "G", eType = t.inputField, inputListener = updateParam },
{param = "startColorVarianceBlue", label = "B", eType = t.inputField, inputListener = updateParam }, {param = "startColorVarianceAlpha", label = "A", eType = t.inputField, inputListener = updateParam } },
[3] = { {param = "finishColorRed", label = "Finish R", eType = t.inputField, inputListener = updateParam }, {param = "finishColorGreen", label = "G", eType = t.inputField, inputListener = updateParam },
{param = "finishColorBlue", label = "B", eType = t.inputField, inputListener = updateParam }, {param = "finishColorAlpha", label = "A", eType = t.inputField, inputListener = updateParam } },
[4] = { {param = "finishColorVarianceRed", label = "Var R", eType = t.inputField, inputListener = updateParam }, {param = "finishColorVarianceGreen", label = "G", eType = t.inputField, inputListener = updateParam },
{param = "finishColorVarianceBlue", label = "B", eType = t.inputField, inputListener = updateParam }, {param = "finishColorVarianceAlpha", label = "A", eType = t.inputField, inputListener = updateParam } }
} } }
self.settingsWindowParams = {
x = 20, y = 20, width = 300, height = 800,
label = "settings", closable = true, movable = true,
sceneGroup = sceneGroup, sectionData = self.settingsWindowSections, --put settingsWindowSections in emitter to access collision listeners from emitter
object = emitter
}
end
local function spawnPointClicked()
--print("spawnPoint clicked")
if (emitter.spawnPoint.isVisible == true) then
emitter.spawnPoint.isMoving = true
end
end
emitter.spawnPoint = display.newImage( spawnPointGroup, "content/ui/x.png")
emitter.spawnPoint.x, emitter.spawnPoint.y = display.contentCenterX, display.contentCenterY
emitter.spawnPoint:setFillColor( .5, .5, .5 )
physics.addBody( emitter.spawnPoint, "static" )
emitter.spawnPoint.clickListener = spawnPointClicked
emitter.spawnPoint.isMoving = false
function emitter:onFrame()
if (self.spawnPoint.isMoving) then
if (mouse.pressed) then
self.spawnPoint.x, self.spawnPoint.y = self.spawnPoint.x + mouse.delta.x, self.spawnPoint.y + mouse.delta.y --move object
self.x, self.y = self.spawnPoint.x, self.spawnPoint.y
if (self.emitterObject) then
self.emitterObject.x, self.emitterObject.y = self.spawnPoint.x, self.spawnPoint.y
end
else --only do movement of window when mouse button is held down
self.spawnPoint.isMoving = false
end
end
end
function emitter:restart() -- for changing some params, requires emitter to be stopped and started
if (self.emitterObject) then --emitter exists
if (self.emitterObject.state == "playing") then --only restart if emitter is already started
self.emitterObject:stop()
self.group:removeSelf()
self.emitterObject = nil
self:createObject()
self.emitterObject:start()
end
end
end
function emitter:createObject()
self.group = display.newGroup()
for k, v in pairs(self.params) do
--print("params: "..k, v)
end
self.emitterObject = display.newEmitter( self.params )
self.emitterObject.x, self.emitterObject.y = self.x or display.contentCenterX, self.y or display.contentCenterY
self.group:insert(self.emitterObject)
emitterGroup:insert(self.group)
--print("call toolbar window ")
end
emitter:createObject()
editor.toolbarObjectList:update() --calls the function in the editor toolbar to update the list of emitters
function emitter:createSettingsWindow() --called when emitter is created, but also when window is shown after closing
self.settingsWindow = editor.createWindow( self.settingsWindowParams, sceneGroup )
for param, value in pairs(self.params) do
self.settingsWindow:paramValueChanged(param, value) --set default values of all elements
end
end
emitter:createWindowSettings()
emitter:createSettingsWindow()
end
local function firstFrame()
debug.createGroup()
sceneGroup:insert(emitterGroup)
sceneGroup:insert(spawnPointGroup)
key.init() --initiate key input
local toolbarWindowSections = { --sections in the settings window that hold elements, uses index for ordering in ui
[1] = { label = "Emitters", collapsable = false, elements = {
[1] = { { label = "", eType = editor.elementTypes.button, text = "Create Emitter", clickListener = createEmitter } },
[2] = { { eType = editor.elementTypes.objectList, objectStore = emitterStore } }
} },
}
local toolbarWindowParams = {
x = display.actualContentWidth - 400, y = 20, width = 200, height = 200,
label = "toolbar", closable = false, movable = true,
sceneGroup = sceneGroup, sectionData = toolbarWindowSections
}
--local settingsWindow = createWindow( settingsWindowParams )
toolbarWindow = editor.createWindow( toolbarWindowParams, sceneGroup )
mouse.init() -- registers the mouse on frame event
end
local function onFrame( event )
for i = 1, #emitterStore do
emitterStore[i]:onFrame()
end
editor.onFrame() --update everything in editor interface
debug.onFrame()
end
function scene:create( event )
display.setDefault( "background", .09, .09, .09 )
-- Called when scene's view does not exist.
--
-- INSERT code here to initialize scene
-- e.g. add display objects to 'sceneGroup', add touch listeners, etc.
-- create scene group
local sceneGroup = self.view
-- We need physics started to add bodies
physics.start()
physics.setGravity( 0, 0 )
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == "will" then
-- Called when scene is still off screen and is about to move on screen
elseif phase == "did" then
-- Called when scene is now on screen
--
-- INSERT code here to make scene come alive
-- e.g. start timers, begin animation, play audio, etc.
--print("scene loaded")
firstFrame()
--add listerer for every frame to process all game logic
Runtime:addEventListener( "enterFrame", onFrame )
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == "will" then
-- Called when scene is on screen and is about to move off screen
--
-- INSERT code here to pause scene
-- e.g. stop timers, stop animation, unload sounds, etc.)
timer.cancel( "menu" ) --cancels all running timers
transition.cancelAll() --cancels all transitions
Runtime:addEventListener( "enterFrame", onFrame )
elseif phase == "did" then
-- Called when scene is now off screen
end
end
function scene:destroy( event )
-- Called prior to removal of scene's "view" (sceneGroup)
--
-- INSERT code here to cleanup scene
-- e.g. remove display objects, remove touch listeners, save state, etc.
local sceneGroup = self.view
package.loaded[physics] = nil
physics = nil
end
---------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-----------------------------------------------------------------------------------------
return scene