View difference between Paste ID: sirNJx2K and DZGss6kR
SHOW: | | - or go back to the newest paste.
1
display.setStatusBar(display.HiddenStatusBar)
2
local storyboard = require "storyboard"
3
local scene = storyboard.newScene()
4
require("physics")
5
require("sprite")
6
7
----------------------------
8
-- Functions
9
----------------------------
10
11
local function collisionGround(event)
12
	event.other:removeSelf()
13
end
14
15
local function collisionCharacter(event)
16
	if (event.other.type == "penny") then
17
		storyboard.money = storyboard.money + 0.01
18
		event.other:removeSelf()
19
	elseif (event.other.type == "nickel") then
20
		storyboard.money = storyboard.money + 0.05
21
		event.other:removeSelf()
22
	elseif (event.other.type == "dime") then
23
		storyboard.money = storyboard.money + 0.10
24
		event.other:removeSelf()
25
	elseif (event.other.type == "quarter") then
26
		storyboard.money = storyboard.money + 0.25
27
		event.other:removeSelf()
28
	elseif (event.other.type == "rock") then
29-
		storyboard.gotoScene("gameover", "slideLeft", 1000)
29+
		timer.performWithDelay(1, function() storyboard.gotoScene("gameover", "slideLeft", 1000) end)
30
	end
31
	textMoney.text = string.format("$%.02f", storyboard.money)
32
end
33
34
local motion = 0
35
local isPaused = false
36
37
local function moveLeft(event)
38
	if not isPaused then
39
		motion = -9
40
		buttonLeft:prepare("leftt")
41
		buttonLeft:play()
42
		character:prepare("charml")
43
		character:play()
44
		character.type = "left"
45
	end
46
end
47
48
local function moveRight(event)
49
	if not isPaused then
50
		motion = 9
51
		buttonRight:prepare("rightt")
52
		buttonRight:play()
53
		character:prepare("charmr")
54
		character:play()
55
		character.type = "right"
56
	end
57
end
58
59
local function moveCharacter(event)
60
	character.x = character.x + motion
61
	if character.x < 50 then character.x = 50
62
	elseif character.x > 910 then character.x = 910 end
63
	buttonLeft:toFront()
64
	buttonRight:toFront()
65
end
66
67
local function stopCharacter(event)
68
	motion = 0
69
	buttonLeft:prepare("leftu")
70
	buttonLeft:play("leftu")
71
	buttonRight:prepare("rightu")
72
	buttonRight:play("rightu")
73
	if (character.type == "left") then
74
		character:prepare("charsl")
75
		character:play("charsl")
76
	elseif (character.type == "right") then
77
		character:prepare("charsr")
78
		character:play("charsr")
79
	end
80
end
81
82
local function pauseGame(scene)
83
	isPaused = not isPaused
84
	if isPaused then
85
		buttonPause:prepare("pauset")
86
		buttonPause:play()
87
		physics.pause()
88
		timer.pause(createCoinsTimer)
89
	else
90
		buttonPause:prepare("pauseu")
91
		buttonPause:play()
92
		physics.start()
93
		timer.resume(createCoinsTimer)
94
	end
95
end
96
97
----------------------------
98
-- Storyboard
99
----------------------------
100
101
function scene:createScene(event)
102
	local group = self.view
103
	physics.start()
104
	physics.pause()
105
	local background = display.newImage(group, "visuals/game_background.png", 0, 0)
106
	ground = display.newImage(group, "visuals/game_ground.png", 0, 590)
107
		physics.addBody(ground, "static")
108
		ground:addEventListener("collision", collisionGround)
109
	local buttonSheet = sprite.newSpriteSheet("visuals/game_buttons.png", 75, 75)	
110
	local buttonLeftSet = sprite.newSpriteSet(buttonSheet, 1, 2)
111
		sprite.add(buttonLeftSet, "leftu", 1, 1, 60000, 0)
112
		sprite.add(buttonLeftSet, "leftt", 2, 1, 60000, 0)
113
	local buttonRightSet = sprite.newSpriteSet(buttonSheet, 3, 2)
114
		sprite.add(buttonRightSet, "rightu", 3, 1, 60000, 0)
115
		sprite.add(buttonRightSet, "rightt", 4, 1, 60000, 0)
116
	local buttonPauseSet = sprite.newSpriteSet(buttonSheet, 5, 2)
117
		sprite.add(buttonPauseSet, "pauseu", 5, 1, 60000, 0)
118
		sprite.add(buttonPauseSet, "pauset", 6, 1, 60000, 0)
119
	buttonLeft = sprite.newSprite(buttonLeftSet)
120
		buttonLeft:addEventListener("touch", moveLeft)
121
		buttonLeft.x = 57.5
122
		buttonLeft.y = 582.5
123
		group:insert(buttonLeft)
124
	buttonRight = sprite.newSprite(buttonRightSet)
125
		buttonRight:addEventListener("touch", moveRight)
126
		buttonRight.x = 902.5
127
		buttonRight.y = 582.5
128
		group:insert(buttonRight)
129
	buttonPause = sprite.newSprite(buttonPauseSet)
130
		buttonPause:addEventListener("tap", function(event) pauseGame(self) end) -- Shorten?
131
		buttonPause.x = 902.5
132
		buttonPause.y = 57.5
133
		group:insert(buttonPause)
134
	local characterSheet = sprite.newSpriteSheet("visuals/game_character.png", 100, 100)	
135
	local characterSet = sprite.newSpriteSet(characterSheet, 1, 8)
136
		sprite.add(characterSet, "charsl", 1, 1, 60000, 0)
137
		sprite.add(characterSet, "charml", 2, 3, 150, 0)
138
		sprite.add(characterSet, "charsr", 5, 1, 60000, 0)
139
		sprite.add(characterSet, "charmr", 6, 3, 150, 0)
140
	character = sprite.newSprite(characterSet)
141
		physics.addBody(character, "static")
142
		character:addEventListener("collision", collisionCharacter)
143-
		self:addEventListener("enterFrame", moveCharacter)
143+
		self.view:addEventListener("enterFrame", moveCharacter)
144-
		self:addEventListener('Stop', stopCharacter)
144+
		self.view:addEventListener('Stop', stopCharacter)
145
		character.x = display.contentWidth / 2
146
		character.y = 540
147
		group:insert(character)
148
	storyboard.money = 0
149
	textMoney = display.newText(string.format("$%.02f", storyboard.money), 0, 20, native.systemFont, 75)
150
		textMoney:setReferencePoint(display.CenterReferencePoint)
151
		textMoney.x = display.contentWidth / 2
152
		textMoney:setTextColor(0, 0, 0)
153
		group:insert(textMoney)
154-
	Runtime:addEventListener('enterFrame', self)
154+
	Runtime:addEventListener('enterFrame', self.view)
155-
	Runtime:addEventListener('touch', self)
155+
	Runtime:addEventListener('touch', self.view)
156
end
157
158
local function createCoins(scene)
159
	local group = scene.view
160
	local coinSheet = sprite.newSpriteSheet("visuals/game_coins.png", 50, 50)   
161
	local coinSet = sprite.newSpriteSet(coinSheet, 1, 25)
162
		sprite.add(coinSet, "penny", 1, 5, 250, 0)
163
		sprite.add(coinSet, "nickel", 6, 5, 250, 0)
164
		sprite.add(coinSet, "dime", 11, 5, 250, 0)
165
		sprite.add(coinSet, "quarter", 16, 5, 250, 0)
166
		sprite.add(coinSet, "rock", 21, 5, 250, 0)
167
	local coin = sprite.newSprite(coinSet)
168
		physics.addBody(coin, "dynamic")
169
		coin.isSensor = true
170
		coin.isFixedRotation = true
171
	local chooseCoin = math.random(1, 100)
172
	if chooseCoin >= 1 and chooseCoin <= 25 then
173
		coin.type = "penny"
174
	elseif chooseCoin >= 26 and chooseCoin <= 45 then
175
		coin.type = "nickel"
176
	elseif chooseCoin >= 46 and chooseCoin <= 65 then
177
		coin.type = "dime"
178
	elseif chooseCoin >= 66 and chooseCoin <= 80 then
179
		coin.type = "quarter"
180
	elseif chooseCoin >= 81 and chooseCoin <= 100 then
181
		coin.type = "rock"
182
	end
183
	coin:prepare(coin.type)
184
	coin:play()
185
	local chooseLocation = math.random(0, 1)
186
		coin.y = 400
187
		coin.x =  -25 + chooseLocation * 1010
188
		coin:applyLinearImpulse(0.2 - (chooseLocation * 0.4), -math.random(0.50, 3.45) / 10, coin.x, coin.y)
189
	local nativeRemove = coin.removeSelf
190
	function coin:removeSelf(...)
191
		scene:removeEventListener('exitScene', self)
192
		nativeRemove(self, ...)
193
	end
194
	function coin:exitScene(event)
195
		timer.performWithDelay(1, 
196
			function(...) 
197-
				self:removeSelf()
197+
				scene:removeEventListener('exitScene', self)
198
			end
199
		)
200
		
201
	end
202
	scene:addEventListener('exitScene', coin)
203
	group:insert(coin)
204
end
205
206
local function sceneEnterFrame(self, event)
207
	if not isPaused then
208
		self:dispatchEvent(event)
209
	end
210
end
211
212
local function sceneTouch(self, event)
213
	if not isPaused and event.phase == "ended" then
214
		self:dispatchEvent{name = "Stop"}
215
	end
216
end
217
218
function scene:enterScene(event)
219
	physics.start()
220
	physics.setGravity(0, 9.8)
221-
    scene.enterFrame = sceneEnterFrame
221+
	scene.view.enterFrame = sceneEnterFrame
222-
    scene.touch = sceneTouch
222+
	scene.view.touch = sceneTouch
223-
    createCoinsTimer = timer.performWithDelay(500, function() createCoins(self) end, 0) -- Shorten?
223+
	createCoinsTimer = timer.performWithDelay(500, function() createCoins(self) end, 0) -- Shorten?
224
end
225
226
function scene:exitScene(event)
227
	physics.stop()
228-
	self.enterFrame = nil
228+
	self.view.enterFrame = nil
229-
	self.touch = nil
229+
	self.view.touch = nil
230
	timer.cancel(createCoinsTimer)
231
	storyboard.purgeScene("game")
232
end
233
234
function scene:destroyScene(event)
235
	Runtime:removeEventListener('enterFrame', self)
236-
	Runtime:removeEventListener('touch', moveCharacter)
236+
	Runtime:removeEventListener('touch', self)
237
238
end
239
240
scene:addEventListener("createScene", scene)
241
scene:addEventListener("enterScene", scene)
242
scene:addEventListener("exitScene", scene)
243
scene:addEventListener("destroyScene", scene)
244
return scene