diff --git a/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua b/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua index 709e1fbc6c..89739511f4 100644 --- a/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua +++ b/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua @@ -1,60 +1,57 @@ local function AccelerometerMainLayer() - local function title() - return "AccelerometerTest" - end - local pLayer = cc.Layer:create() - - pLayer:setAccelerometerEnabled(true) - - local pLabel = cc.LabelTTF:create(title(), "Arial", 32) - pLayer:addChild(pLabel, 1) - pLabel:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 50) ) + local function title() + return "AccelerometerTest" + end + local layer = cc.Layer:create() - local pBall = cc.Sprite:create("Images/ball.png") - pBall:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y)) - pLayer:addChild(pBall) + layer:setAccelerometerEnabled(true) - pBall:retain() + local label = cc.LabelTTF:create(title(), "Arial", 32) + layer:addChild(label, 1) + label:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 50) ) + + local ball = cc.Sprite:create("Images/ball.png") + ball:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y)) + layer:addChild(ball) + + ball:retain() local function didAccelerate(x,y,z,timestamp) - local pDir = cc.Director:getInstance() - if nil == pBall then - return - end + if nil == ball then + return + end - local szBall = pBall:getContentSize() - local ptNowX,ptNowY = pBall:getPosition() - - ptNowX = ptNowX - x - ptNowY = ptNowY - y + local szBall = ball:getContentSize() + local ptNowX,ptNowY = ball:getPosition() + + ptNowX = ptNowX - x + ptNowY = ptNowY - 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 - 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 - - pBall:setPosition(cc.p(ptNowX, ptNowY )) - - + 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 )) end - pLayer:registerScriptAccelerateHandler(didAccelerate) + layer:registerScriptAccelerateHandler(didAccelerate) - return pLayer + return layer end