axmol/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua

65 lines
1.6 KiB
Lua
Raw Normal View History

2013-05-24 11:42:27 +08:00
local function AccelerometerMainLayer()
2013-09-18 17:19:34 +08:00
local function title()
return "AccelerometerTest"
end
local layer = cc.Layer:create()
layer:setAccelerometerEnabled(true)
2013-05-24 11:42:27 +08:00
2013-09-18 17:19:34 +08:00
local label = cc.LabelTTF:create(title(), "Arial", 32)
layer:addChild(label, 1)
label:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 50) )
2013-05-24 11:42:27 +08:00
2013-09-18 17:19:34 +08:00
local ball = cc.Sprite:create("Images/ball.png")
ball:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y))
layer:addChild(ball)
ball:retain()
2013-05-24 11:42:27 +08:00
local function didAccelerate(x,y,z,timestamp)
2013-09-18 17:19:34 +08:00
if nil == ball then
return
end
local szBall = ball:getContentSize()
local ptNowX,ptNowY = ball:getPosition()
2013-05-24 11:42:27 +08:00
2013-09-18 17:19:34 +08:00
ptNowX = ptNowX - x
ptNowY = ptNowY - y
2013-05-24 11:42:27 +08:00
2013-09-18 17:19:34 +08:00
local minX = math.floor(VisibleRect:left().x + szBall.width / 2.0)
local maxX = math.floor(VisibleRect:right().x - szBall.width / 2.0)
if ptNowX < minX then
ptNowX = minX
elseif ptNowX > maxX then
ptNowX = maxX
end
local minY = math.floor(VisibleRect:bottom().y + szBall.height / 2.0)
local maxY = math.floor(VisibleRect:top().y - szBall.height / 2.0)
if ptNowY < minY then
ptNowY = minY
elseif ptNowY > maxY then
ptNowY = maxY
end
ball:setPosition(cc.p(ptNowX, ptNowY ))
2013-05-24 11:42:27 +08:00
end
2013-09-18 17:19:34 +08:00
layer:registerScriptAccelerateHandler(didAccelerate)
2013-05-24 11:42:27 +08:00
2013-09-18 17:19:34 +08:00
return layer
2013-05-24 11:42:27 +08:00
end
function AccelerometerMain()
cclog("AccelerometerMain")
local scene = cc.Scene:create()
2013-05-24 11:42:27 +08:00
scene:addChild(AccelerometerMainLayer())
scene:addChild(CreateBackMenuItem())
return scene
end