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
| local cancel=10 -- a tracing flag
local timerNew -- timer to cancel the joint
prevPause=0 -- a flag variable for pausing game
local notFall = 0 -- a tracing variable for the stone if it hasn't been thrown yet
local function onClick (event)
local phase = event.phase
function cancelEvent()
stones[c].tempJoint:removeSelf()
touched=false
cancel=cancel+1
print("cancel "..cancel)
notFall=1
end
if "began" == phase then
if(event.y>50) then
stones[c].bodyType = "dynamic"
stones[c].isBullet = true
touched = true
cancel=10
print("began")
notFall=0
deltaX = stones[c].x - event.x; -- in order to drag the object from
deltaY = stones[c].y - event.y; -- anywhere on the screen
stones[c].tempJoint = physics.newJoint( "touch", stones[c], stones[c].x, stones[c].y )
Runtime:removeEventListener("enterFrame", moveStone) -- as the stone is moving at some kind of path
timerf=timer.performWithDelay(50,cancelEvent,1) -- to not holding the stone too much
timerNew=timer.performWithDelay(100,newRound,1) -- generate a new stone
prevPause=0
end
elseif touched then
if(event.y > 10) then
stones[c].tempJoint:setTarget( event.x + deltaX, event.y + deltaY )
--///// calc. the velocity
vx, vy = stones[c]:getLinearVelocity()
print("velocityX : "..vx)
print("velocityY : "..vy)
if vx > 100 then
vx=100
elseif vx < -100 then
vx=-100
end
if vy>300 then
vy=300
elseif vy<-300 then
vy=-300
end
stones[c]:setLinearVelocity( vx, vy )
v1, v2 = stones[c]:getLinearVelocity()
print("new velocity X : "..v1)
print("new velocity Y : "..v2)
-- ////////////////
print("dragged")
if notFall==0 and cancel==10 then
timer.cancel(timerf)
timer.cancel(timerNew)
timerf=timer.performWithDelay(50,cancelEvent,1)
timerNew=timer.performWithDelay(100,newRound,1)
notFall=1
end
end
if "ended" == phase then
if cancel==10 then
notFall=1
timer.cancel(timerf)
timer.cancel(timerNew)
timerNew=timer.performWithDelay(10,newRound,1)
cancelEvent()
end
end
end
end
sky:addEventListener("touch", onClick) |