-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.lua
91 lines (69 loc) · 2.67 KB
/
Ball.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
--Ball CLASS
module(..., package.seeall)
local Ball = {}
function Ball.new(locX, locY, whichPlayer, touchDuration)
print("new ball");
-- Draw a ball
--get how long the ball was touched over 1 second
local chargeUp = touchDuration - 1;
print("1 + chargeup is " .. 1 +chargeUp)
if (chargeUp > 3) then
chargeUp = 3
end
local ball
if (chargeUp > 0) then
ball = display.newCircle( 100, 100, 8*(1+chargeUp) )
physics.addBody( ball, { density=20.0, friction=0.8, bounce=0.3, radius = (8*(1+chargeUp)+3) } )
end
if (chargeUp <= 0) then
ball = display.newCircle( 100, 100, 8 )
physics.addBody( ball, { density=20.0, friction=0.8, bounce=0.3, radius = 11 } )
end
local ballTimerOn = true;
ball.x = locX
ball.y = locY;
-- ball.id = "ball";
ball.strokeWidth = 3;
ball:setStrokeColor(0,0,0,255);
if (whichPlayer == 1) then
ball:setFillColor(0,0,0,255)
end
if (whichPlayer == 2) then
ball:setFillColor(255,255, 255, 180)
end
ball.bodyType = "dynamic";
ball.isBullet = true;
local ballTimer = 0;
local function eachFrame()
--Every frame, keep track of how long the ball has been on screen.
if (ballTimerOn == true) then
ballTimer = ballTimer + 1
end
-- If the ball has been on screen a long time, remove it.
if (ballTimer == 200) then
print ("fading out because we've been on screen too long");
ball:fadeOut();
ballTimer = 0;
end
end
--Add an event listener to keep track of the ball every frame.
Runtime:addEventListener( "enterFrame", eachFrame );
function ball:destroy()
print("ball gone");
ball:removeSelf()
ball = nil
end
function ball:fadeOut()
print("fading out")
local function goAway()
-- print("...and going away");
Runtime:removeEventListener("enterFrame", eachFrame);
ball:removeSelf()
ball = nil
end
ballTimerOn = false;
transition.to( ball, {delay=1, time=1000, alpha=0.0, onComplete=goAway} )
end
return ball
end
return Ball