-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
173 lines (129 loc) · 4.36 KB
/
main.lua
File metadata and controls
173 lines (129 loc) · 4.36 KB
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
-- ** load all libraries being used ** --
local ui = require( "libraries/ui" );
local imgs = require( "libraries/images" );
-- ** Set app data (as globals) ** --
app_globals = {
-- hide status bar of phone --
hide_status_bar = display.setStatusBar( display.HiddenStatusBar ),
-- set game background --
background = display.newImage( imgs.fetchImagePath( "bg_meadow.png" ) ),
-- calculate center of the x axis
centerX = display.contentWidth / 2,
-- keep count of total balls --
total_balls = 0,
-- limit of balls --
max_balls = 20,
-- ** universal gravity ** --
gravity = { x = 1.5, y = 20 },
-- ** determine if high res ** --
is_high_res = imgs.isHighRes()
};
-- ** cache math library functions ** --
local rand = math.random;
-- ** add and start physics, set gravity (explained in part 1) ** --
local physics = require("physics");
physics.start();
-- physics.pause();
physics.setGravity( app_globals.gravity.x, app_globals.gravity.y );
-- ** create tennis ball ** --
local function createBall( ball_count )
-- ** create new instance of tennis ball ** --
local tennis_ball = display.newImage( imgs.fetchImagePath( "bg_tennis_ball.png" ) );
-- ** set tennis ball id ** --
tennis_ball.tb_id = "tb_" .. ( ball_count + 1 );
-- ** initiate bounces of this ball ** --
tennis_ball.bounces = 0; -- set to zero --
-- ** position tennis ball ** --
tennis_ball.x = rand( 15, 305 ); -- set x axis --
tennis_ball.y = -15; -- set y axis --
tennis_ball.rotation = 1;
-- ** add physics to ball ** --
physics.addBody( tennis_ball, { bounce = 0.8, density = 1.0 } );
-- ** incremente count of balls ** --
app_globals.total_balls = app_globals.total_balls + 1;
-- ** return new object ** --
return tennis_ball;
end
-- ** spawn tennis ball(s) ** --
local function spawnBall()
-- ** check to see if max balls created ** --
if( app_globals.total_balls < app_globals.max_balls ) then
-- ** fetch new ball ** --
timer.performWithDelay( 100,
function()
-- ** create ball ** --
createBall( app_globals.total_balls );
end,
1 );
end
end
-- ** ball bounce event handler ** --
local function ballBounce( e )
-- ** save bouncing ball ** --
local ball = e.object2;
-- ** detect first bounce ** --
if ( e.phase == "began" and ball.bounces == 0 ) then
spawnBall();
--native.showAlert( "Corona", "test" );
end
-- ** track every bounce ** --
ball.bounces = ball.bounces + 1;
-- ** return ** --
return;
end
-- ** add ball bounce listener ** --
Runtime:addEventListener( "collision", ballBounce );
-- ** create floor (for physics) ** --
floor = display.newRect( 0, ( app_globals.is_high_res and 860 or 430 ), ( app_globals.is_high_res and 640 or 320 ), ( app_globals.is_high_res and 100 or 50 ) );
floor.alpha = 0;
-- adds phyiscs properties to the newly added floor
physics.addBody( floor, "static", { bounce = 0.4, density = 1.0 } );
-- ** add ant to stage (global access) ** --
ant_player = display.newImage( imgs.fetchImagePath( "bg_ant.png" ) );
-- ** set in middle of screen (on the floor) ** --
if( app_globals.is_high_res ) then
ant_player.x = 320;
ant_player.y = 810;
else
ant_player.x = 160;
ant_player.y = 405;
end
-- ** add physics to ant ** --
physics.addBody( ant_player, "kinematic" );
-- ** game controls ** --
-- ** interpret touch control ** --
local distance_traveled = ( app_globals.is_high_res and 60 or 30 );
local moveLeft = function( e )
-- ** move 30px to the left ** --
transition.to( ant_player, { time = 100, x= ( ant_player.x - distance_traveled ) } );
end
local moveRight = function( e )
-- ** move 30/60px to the right ** --
transition.to( ant_player, { time = 100, x= ( ant_player.x + distance_traveled ) } );
end
-- ** create controls ** --
local left_btn = ui.newButton{
default = imgs.fetchImagePath( "btn_left.png" ),
-- over = "btn_whoA.png",
onRelease = moveLeft
};
local right_btn = ui.newButton{
default = imgs.fetchImagePath( "btn_right.png" ),
-- over = "btn_whoA.png",
onRelease = moveRight
};
-- ** position controls ** --
if( app_globals.is_high_res ) then
left_btn.x = 52;
left_btn.y = 910;
right_btn.x = 590;
right_btn.y = 910;
else
left_btn.x = 26;
left_btn.y = 455;
right_btn.x = 295;
right_btn.y = 455;
end
-- ** start balls ** --
spawnBall();
--native.showAlert( "test", display.stageWidth .. " == " .. display.stageHeight );