【问题标题】:How to offset accelerometer lua corona sdk如何偏移加速度计lua corona sdk
【发布时间】:2013-02-18 19:13:36
【问题描述】:

我可以使用加速度计控制“小鸟”,我唯一的问题是它可以在我的设备(手机/平板电脑)平放时工作,但当你把它握在手中玩时它不会空闲。有没有办法将加速度计的零点偏移到 y 轴上的 30°(度)?

display.setStatusBar (display.HiddenStatusBar);
system.setAccelerometerInterval( 50 );
system.setIdleTimer(false);
local background = display.newImage("bg.png")


--> GAME CHARACTER
local bird  = display.newImage ("helicopter.png")
bird.x      = 100;
bird.y      = 100;
bird:setReferencePoint(display.CenterReferencePoint);

-- Speed of Movement with tilt. You can change it ans see the effects.
local tiltSpeed         = 30;
local motionx           = 0;
local motiony           = 0;
local rotation          = 0;

local function onTilt(event)
    -- Its for the new position of the bird
    motionx = tiltSpeed * event.xGravity;
    motiony = tiltSpeed * event.yGravity;
end

local function moveBird (event)
    bird.x = motionx + bird.x;
    bird.y = bird.y - motiony;
    bird.rotation = rotation;
end

Runtime:addEventListener("accelerometer", onTilt)
Runtime:addEventListener("enterFrame", moveBird)

【问题讨论】:

  • 如果你有 event.zGravity 那么你可以简单地将 3D 重力矢量旋转到你想要的任何位置。
  • 我将如何使用它?

标签: lua coronasdk


【解决方案1】:
local delta = -30/180*math.pi  -- 30 degrees
local cos_delta, sin_delta = math.cos(delta), math.sin(delta)

local function onTilt(event)
    -- Its for the new position of the bird
    motionx = tiltSpeed * event.xGravity
    motiony = tiltSpeed * (cos_delta*event.yGravity + sin_delta*event.zGravity)
end

【讨论】:

    猜你喜欢
    • 2015-05-12
    • 2014-03-24
    • 2011-09-26
    • 2013-06-04
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 2012-05-08
    • 2014-04-22
    相关资源
    最近更新 更多