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
| local physics = require "physics"
physics.start()
local rat = display.newCircle(20,100,10)
rat.myName = "rat"
local rope = display.newRect(0,130,480,2)
rope.myName = "rope"
local floor = display.newRect(0,310,480,10)
floor.myName = "floor"
physics.addBody(rope,"static")
physics.addBody(rat)
physics.addBody(floor,"static")
local function1,function2
function1 = function()
t1 = transition.to(rat,{x = 480 - rat.width * 0.5,time = 1500,onComplete = function2})
end
function2 = function()
t2 = transition.to(rat,{x = rat.width * 0.5,time = 1500,onComplete = function1})
end
function1()
local function addRocks(e)
local rock = display.newCircle(0,0,5)
rock.x = e.x
rock.y = e.y
rock.myName = "rock"
physics.addBody(rock,{isSensor = true})
end
Runtime:addEventListener("tap",addRocks)
local function onCollision( e )
if ( e.phase == "ended" ) then
if e.object1.myName == "rat" and e.object2.myName == "rock" then
print("called")
transition.cancel(t1)
transition.cancel(t2)
rat.isSensor = true
timer.performWithDelay(200,function() rat.isSensor = false end)
end
end
end
Runtime:addEventListener( "collision", onCollision ) |