-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlevel1.lua
122 lines (93 loc) · 3.88 KB
/
level1.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
-----------------------------------------------------------------------------------------
--
-- level1.lua
--
-----------------------------------------------------------------------------------------
local composer = require( "composer" )
local scene = composer.newScene()
-- include Corona's "physics" library
local physics = require "physics"
--------------------------------------------
-- forward declarations and other locals
local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX
function scene:create( event )
-- 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.
local sceneGroup = self.view
-- We need physics started to add bodies, but we don't want the simulaton
-- running until the scene is on the screen.
physics.start()
physics.pause()
-- create a grey rectangle as the backdrop
-- the physical screen will likely be a different shape than our defined content area
-- since we are going to position the background from it's top, left corner, draw the
-- background at the real top, left corner.
local background = display.newRect( display.screenOriginX, display.screenOriginY, screenW, screenH )
background.anchorX = 0
background.anchorY = 0
background:setFillColor( .5 )
-- make a crate (off-screen), position it, and rotate slightly
local crate = display.newImageRect( "crate.png", 90, 90 )
crate.x, crate.y = 160, -100
crate.rotation = 15
-- add physics to the crate
physics.addBody( crate, { density=1.0, friction=0.3, bounce=0.3 } )
-- create a grass object and add physics (with custom shape)
local grass = display.newImageRect( "grass.png", screenW, 82 )
grass.anchorX = 0
grass.anchorY = 1
-- draw the grass at the very bottom of the screen
grass.x, grass.y = display.screenOriginX, display.actualContentHeight + display.screenOriginY
-- define a shape that's slightly shorter than image bounds (set draw mode to "hybrid" or "debug" to see)
local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 }
physics.addBody( grass, "static", { friction=0.3, shape=grassShape } )
-- all display objects must be inserted into group
sceneGroup:insert( background )
sceneGroup:insert( grass)
sceneGroup:insert( crate )
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.
physics.start()
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.)
physics.stop()
elseif phase == "did" then
-- Called when the scene is now off screen
end
end
function scene:destroy( event )
-- 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.
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