-
Notifications
You must be signed in to change notification settings - Fork 1
/
getcoin.lua
147 lines (122 loc) · 4.17 KB
/
getcoin.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
local composer = require("composer")
local scene = composer.newScene()
local physics = require("physics")
physics.start()
function scene:create(event)
local player = display.newImageRect("images/bomb.png", 50, 50)
physics.addBody(player, "static")
player.x = display.contentCenterX
player.y = 400
local screenWidth = display.contentWidth
local line = display.newRect(0, 0, screenWidth * 2, 2)
line.y = 500
line:setFillColor(0, 0, 0)
physics.addBody(line, "static")
end
function scene:show(event)
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
local speed = 2
local touch_x
local score = 0
local on_touch
local scoreText = display.newText("Score: 0", display.contentCenterX, 10, native.systemFont, 20)
local function updateScore()
scoreText.text = "Score: " .. score
end
local move = function()
local target_x = player.x
if touch_x > player.x then
target_x = math.min(touch_x, player.x + 10)
elseif touch_x < player.x then
target_x = math.max(touch_x, player.x - 10)
end
if target_x ~= player.x then
transition.to(player,
{ x = target_x, time = math.abs(target_x - player.x) / speed, onComplete = on_touch })
end
end
on_touch = function(event)
if event.phase == "began" then
touch_x = event.x
move()
return true
elseif event.phase == "moved" then
touch_x = event.x
move()
elseif event.phase == "ended" then
touch_x = player.x
move()
else
move()
end
end
local min_x = 40
local max_x = 300
local min_delay = 0.3
local max_delay = 1.02
local coin = {}
local generateCoin
generateCoin = function()
local x = math.random(min_x, max_x)
local y = 100
local coin = display.newImageRect("images/coin.png", 50, 50)
coin.x = x
coin.y = y
physics.addBody(coin)
local drop_time = 4000
local drop_distance = 1000
transition.to(coin, { y = display.contentHeight, time = drop_time }) --,onComplete = function() coin:removeSelf() end})
end
Runtime:addEventListener("touch", on_touch)
local timeLeft = 10
local function updateTimer()
timeLeft = timeLeft - 1
print(timeLeft)
if timeLeft == 0 then
print("结束")
scene:hide()
return
else
generateCoin()
end
end
local timerID = timer.performWithDelay(1000, updateTimer, timeLeft)
local function touchcoin(event)
if (event.phase == "began") then
if (event.object1 == player) then
event.object2:removeSelf()
print("collision!")
score = score + 1
print("score:" .. score)
updateScore()
elseif (event.object2 == player) then
event.object1:removeSelf()
print("collision!")
score = score + 1
print("score:" .. score)
updateScore()
elseif (event.object1 == line) then
event.object2:removeSelf()
print("lost")
elseif (event.object2 == line) then
event.object1:removeSelf()
print("lost")
end
end
end
Runtime:addEventListener("collision", touchcoin)
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
display.removeSelf(player)
end
end
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
return scene