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 return
end end
local director = cc.Director:getInstance()
local szBall = ball:getContentSize() local szBall = ball:getContentSize()
local ptNowX,ptNowY = ball:getPosition() local ptNowX,ptNowY = ball:getPosition()
local ptTemp = director:convertToUI(cc.p(ptNowX,ptNowY))
ptNowX = ptNowX - x ptTemp.x = ptTemp.x + x * 9.81
ptNowY = ptNowY - y 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 minX = math.floor(VisibleRect:left().x + szBall.width / 2.0)
local maxX = math.floor(VisibleRect:right().x - szBall.width / 2.0) local maxX = math.floor(VisibleRect:right().x - szBall.width / 2.0)
if ptNowX < minX then if ptNext.x < minX then
ptNowX = minX ptNext.x = minX
elseif ptNowX > maxX then elseif ptNext.x > maxX then
ptNowX = maxX ptNext.x = maxX
end end
local minY = math.floor(VisibleRect:bottom().y + szBall.height / 2.0) local minY = math.floor(VisibleRect:bottom().y + szBall.height / 2.0)
local maxY = math.floor(VisibleRect:top().y - szBall.height / 2.0) local maxY = math.floor(VisibleRect:top().y - szBall.height / 2.0)
if ptNowY < minY then if ptNext.y < minY then
ptNowY = minY ptNext.y = minY
elseif ptNowY > maxY then elseif ptNext.y > maxY then
ptNowY = maxY ptNext.y = maxY
end end
ball:setPosition(cc.p(ptNowX, ptNowY )) ball:setPosition(cc.p(ptNext.x , ptNext.y))
end end
layer:registerScriptAccelerateHandler(didAccelerate) layer:registerScriptAccelerateHandler(didAccelerate)