local isSimulator = "simulator" == system.getInfo("environment")
-- 模拟器不支持加速度计
if isSimulator then
-- 请显示警告框
end
-- 文本参数
local labelx = 50
local x = 220
local y = 95
local fontSize = 24
local frameUpdate = false
local xglabel = display.newText("重力x = ", labelx, y, native.systemFont, fontSize)
xglabel:setTextColor(255, 255, 255)
local xg = display.newText("0.0", x, y, native.systemFont, fontSize)
xg:setTextColor(255, 255, 255)
y = y + 25
local yglabel = display.newText("重力y = ", labelx, y, native.systemFont, fontSize)
local yg = display.newText("0.0", x, y, native.systemFont, fontSize)
yglabel:setTextColor(255, 255, 255)
yg:setTextColor(255, 255, 255)
y = y + 25
local zglabel = display.newText("重力z = ", labelx, y, native.systemFont, fontSize)
local zg = display.newText("0.0", x, y, native.systemFont, fontSize)
zglabel:setTextColor(255, 255, 255)
zg:setTextColor(255, 255, 255)
y = y + 50
local xilabel = display.newText("瞬时x = ", labelx, y, native.systemFont, fontSize)
local xi = display.newText("0.0", x, y, native.systemFont, fontSize)
xilabel:setTextColor(255, 255, 255)
xi:setTextColor(255, 255, 255)
y = y + 25
local yilabel = display.newText("瞬时y = ", labelx, y, native.systemFont, fontSize)
local yi = display.newText("0.0", x, y, native.systemFont, fontSize)
yilabel:setTextColor(255, 255, 255)
yi:setTextColor(255, 255, 255)
y = y + 25
local zilabel = display.newText("瞬时z = ", labelx, y, native.systemFont, fontSize)
local zi = display.newText("0.0", x, y, native.systemFont, fontSize)
zilabel:setTextColor(255, 255, 255)
zi:setTextColor(255, 255, 255)
-- 创建一个随加速器事件移动的圆形
local centerX = display.contentWidth / 2
local centerY = display.contentHeight / 2
Circle = display.newCircle(0, 0, 20)
Circle.x = centerX
Circle.y = centerY
Circle:setFillColor( 0, 0, 255 ) -- 蓝色
local textMessage = function( str, location, scrTime, size, color, font )
local x, t
size = tonumber(size) or 24
color = color or {255, 255, 255}
font = font or "Helvetica"
if "string" == type(location) then
if "Top" == location then
x = display.contentHeight/4
elseif "Bottom" == location then
x = (display.contentHeight/4)*3
else
x = display.contentHeight/2
end
else
x = tonumber(location) or display.contentHeight/2
end
scrTime = (tonumber(scrTime) or 3) * 1000
t = display.newText(str, 0, 0, font, size )
t.x = display.contentWidth/2
t.y = x
t:setTextColor( color[1], color[2], color[3] )
if scrTime ~= 0 then
local textMsgTimerEnd = function()
transition.to( t, {time = 500, alpha = 0},
function() t.removeSelf() end )
end
timer.performWithDelay( scrTime, textMsgTimerEnd )
end
return t
end -- textMessage()
本地函数 xyzFormat( obj, value)
obj.text = string.format( "%1.3f", value )
if not frameUpdate then return end
if value < 0.0 then
if obj.positive ~= false then
obj:setTextColor( 255, 0, 0 )
obj.positive = false
print("[---]")
end
else
if obj.positive ~= true then
obj:setTextColor( 255, 255, 255)
obj.positive = true
print("+++")
end
end
结束
本地函数 onAccelerate(事件)
xyzFormat( xg, event.xGravity)
xyzFormat( yg, event.yGravity)
xyzFormat( zg, event.zGravity)
xyzFormat( xi, event.xInstant)
xyzFormat( yi, event.yInstant)
xyzFormat( zi, event.zInstant)
frameUpdate = false
Circle.x = centerX + (centerX * event.xGravity)
Circle.y = centerY + (centerY * event.yGravity * -1)
if event.isShake == true then
textMessage( "Shake!", 400, 3, 52, {255, 255, 0} )
end
结束
local function onFrame()
frameUpdate = true
end
-- 添加运行时监听器
Runtime:addEventListener ("accelerometer", onAccelerate);
Runtime:addEventListener ("enterFrame", onFrame);
希望这段代码能够帮到你。