Modify Accelerometer Test sample

This commit is contained in:
samuele3hu 2013-09-20 22:05:37 +08:00
parent 7471a1224c
commit 54e0fd9a63
1 changed files with 14 additions and 11 deletions

View File

@ -23,30 +23,33 @@ local function AccelerometerMainLayer()
return
end
local director = cc.Director:getInstance()
local szBall = ball:getContentSize()
local ptNowX,ptNowY = ball:getPosition()
local ptTemp = director:convertToUI(cc.p(ptNowX,ptNowY))
ptNowX = ptNowX - x
ptNowY = ptNowY - y
ptTemp.x = ptTemp.x + x * 9.81
ptTemp.y = ptTemp.y - y * 9.81
local ptNext = director:convertToGL(cc.p(ptTemp.x,ptTemp.y))
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
if ptNext.x < minX then
ptNext.x = minX
elseif ptNext.x > maxX then
ptNext.x = 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
if ptNext.y < minY then
ptNext.y = minY
elseif ptNext.y > maxY then
ptNext.y = maxY
end
ball:setPosition(cc.p(ptNowX, ptNowY ))
ball:setPosition(cc.p(ptNext.x , ptNext.y))
end
layer:registerScriptAccelerateHandler(didAccelerate)