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
| _W = display.contentWidth
_H = display.contentHeight
local physics = require("physics")
physics.start()
local top = display.newRect(10,0,_W,10)
physics.addBody(top, "static")
top.name = "top"
local left = display.newRect(0,0,10, _H)
physics.addBody(left, "static")
left.name = "left"
local bottom = display.newRect(0,_H-10,_W, 10)
physics.addBody(bottom, "static")
bottom.name = "bottom"
local right = display.newRect(_W-10,0, 10, _H)
physics.addBody(right, "static")
right.name = "right"
local ball1 = display.newCircle(_W/2+50, _H/2, 30)
ball1.name = "ball1"
local ball2 = display.newCircle(_W/2-50, _H/2, 30)
ball2.name = "ball2"
local function onAccelerate( event )
if event.isShake == true then
physics.addBody(ball1, {friction=0.1, bounce =0.5, radius = 30})
ball1:applyLinearImpulse(10,10, ball1.x, ball1.y)
physics.addBody(ball2, {friction=0.1, bounce =0.5, radius = 30})
ball2:applyLinearImpulse(-10,-10, ball2.x, ball2.y)
end
end
Runtime:addEventListener ("accelerometer", onAccelerate)
local function onCollision(event)
if event.other.name == "top" or event.other.name == "bottom" or event.other.name == "left" or event.other.name == "right" then
event.target:setFillColor(math.random(1,255),math.random(1,255), math.random(1,255))
end
end
ball1:addEventListener("collision", onCollision)
ball2:addEventListener("collision", onCollision) |