當地isSimulator = 「模擬器」 ==系統。的getInfo( 「環境」)
- 加速器不支持模擬器
如果isSimulator然後 - 請顯示一個警告框 結束
- 文本參數 當地labelx = 50 地方X = 220 局部Y = 95 本地fontSize的= 24
本地frameUpdate =假
local xglabel = display.newText(「gravity x =」,labelx,y,native.systemFont,fontSize) xglabel:setTextColor(255,255,255) local xg = display.newText(「0.0」,x,y,native。 localFont,fontSize) xg:setTextColor(255,255,255) y = y + 25 local yglabel = display.newText(「gravity 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(」gravity z =「,labelx,y ,native.systemFont,fontSize) local zg = display.newText(「0.0」,x,y,native.syst emFont,fontSize的) zglabel:setTextColor(255,255,255) ZG:setTextColor(255,255,255) Y = Y + 50 本地xilabel = display.newText( 「時刻x =」,labelx,Y,native.systemFont,fontSize的) 本地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(「instant y:setTextColor(255,255,255)yilabel:setTextColor(255,255,255) yi:set(x),y =「,labelx,y,native.systemFont,fontSize) local yi = display.newText(」0.0「,x,y,native.systemFont, y = y + 25 local zilabel = display.newText(「instant z =」,labelx,y,native.systemFont,fontSize) local zi = display.newText(「0.0」,x,y,native.systemFont,fontSize) zilabel:setTextColor (255,255,255) 滋:setTextColor(255,255,255)
- 創建與加速器事件移動的圓
本地的centerX = display.contentWidth/2 本地centerY = display.contentHeight/2
圈= display.newCircle(0,0,20) Circle.x = centerX Circle。Y = centerY 界:setFillColor(0,0,255) - 藍色
本地的TextMessage =函數(STR,位置,scrTime,大小,顏色,字體)
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
-- Assume middle location
x = display.contentHeight/2
end
else
-- Assume it's a number -- default to Middle if not
x = tonumber(location) or display.contentHeight/2
end
scrTime = (tonumber(scrTime) or 3) * 1000 -- default to 3 seconds (3000) if no time given
t = display.newText(str, 0, 0, font, size)
t.x = display.contentWidth/2
t.y = x
t:setTextColor(color[1], color[2], color[3])
-- Time of 0 = keeps on screen forever (unless removed by calling routine)
if scrTime ~= 0 then
-- Function called after screen delay to fade out and remove text message object
local textMsgTimerEnd = function()
transition.to(t, {time = 500, alpha = 0},
function() t.removeSelf() end)
end
-- Keep the message on the screen for the specified time delay
timer.performWithDelay(scrTime, textMsgTimerEnd)
end
return t -- return our text object in case it's needed
端 - 文字信息( )
本地函數xyzFormat(OBJ,值)
obj.text = string.format("%1.3f", value)
-- Exit if not time to update text color
if not frameUpdate then return end
if value < 0.0 then
-- Only update the text color if the value has changed
if obj.positive ~= false then
obj:setTextColor(255, 0, 0) -- red if negative
obj.positive = false
print("[---]")
end
else
if obj.positive ~= true then
obj:setTextColor(255, 255, 255) -- white if postive
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 -- update done
-- Move our object based on the accelerator values
Circle.x = centerX + (centerX * event.xGravity)
Circle.y = centerY + (centerY * event.yGravity * -1)
-- Display message and sound beep if Shake'n
if event.isShake == true then
-- str, location, scrTime, size, color, font
textMessage("Shake!", 400, 3, 52, {255, 255, 0})
end
末
本地函數onFrame() frameUpdate =真 結束
- 添加運行聽衆 運行:的addEventListener( 「加速度」,onAccelerate) ; 運行時:addEventListener(「enterFrame」,onFrame);
我希望這段代碼能幫到你。
您需要顯示事件處理程序和對象移動的代碼。您遇到的延遲沒有明顯的原因。只有代碼可以告訴我們發生了什麼...... – JeffK
當然,我的歉意!添加一個微型片給你一瞥。 – Danail