-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.lua
152 lines (116 loc) · 4.28 KB
/
main.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
-- ** load all libraries being used ** --
local ui = require( "libraries/ui" );
local imgs = require( "libraries/images" );
local subpub = require( "libraries/subpub" );
-- ** 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( "images/bg_tennis_ball_sm.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 = ant_player.x; -- 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 );
-- local new_ball = createBall( app_globals.total_balls );
--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, 430, 320, 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) ** --
-- ** set in middle of screen (on the floor) ** --
ant_player = display.newImage( "images/bg_ant.png" );
ant_player.x = 160;
ant_player.y = 405;
-- ** Teleport Method ** --
ant_teleport = function(e)
-- ** Make Ant disappear ** --
transition.to(ant_player, {time = 10, alpha = 0,y=600, onComplete=function()
-- ** Move the y axis (drop from stage) ** --
transition.to(ant_player, {time = 10, y=600, onComplete=function()
-- ** Move to the touch x axis to new location ** --
transition.to( ant_player, { time = 100, x=e.x, onComplete=function()
-- ** Reintroduce the y axis to the stage ** --
transition.to(ant_player, {time = 10, y=405, onComplete=function()
-- ** Make Ant appear ** --
transition.to(ant_player, {time = 10, alpha = 1} );
end});-- End Reintroduce original y axis
end} );-- End move to new x axis
end}); -- End Move ant backstage
end} );-- End Disappear
-- ** Notify application the ant has moved ** --
subpub.publish("ant_moved",antplayer);
end
-- ** Subscribe to "player_touched" ** --
subpub.subscribe("player_touched",ant_teleport);
-- ** add physics to ant ** --
physics.addBody( ant_player, "kinematic" );
-- ** start balls ** --
spawnBall();
-- ==========================================
-- = Listen: When player touches the screen =
-- ==========================================
Runtime:addEventListener("touch", function(e) subpub.publish("player_touched",e); end)