0

如何将加速度计 onTilt 事件上的移动限制为左、右、上、下 30 像素。此代码允许它在加速度计倾斜时移动,但对移动距离没有限制。

local screenGroup = self.view
local bg2  = display.newImage ("bg2.png")
bg2.x = display.contentWidth / 2; 
bg2.y = 200

local tiltSpeed         = 30;
local motionx           = 0;
local motiony           = 0;
local rotation          = 0;

delta = -50/180*math.pi
cos_delta, sin_delta = math.cos(delta), math.sin(delta)

local function onTilt(event)
    motionx = tiltSpeed * event.xGravity
    motiony = tiltSpeed * (cos_delta*event.yGravity + sin_delta*event.zGravity)
end

local function moveBg2 (event)
    bg2.x = motionx + bg2.x ;
    bg2.y = bg2.y - motiony;
end
Runtime:addEventListener("enterFrame", moveBg2)
Runtime:addEventListener("accelerometer", onTilt)
4

1 回答 1

0

The question is not super clear, but I guess this could do:

local function onTilt(event)
    motionx = tiltSpeed * event.xGravity
    motiony = tiltSpeed * (cos_delta*event.yGravity + sin_delta*event.zGravity)
    if motionx < -30 then 
        motionx = -30 
    elseif motionx > 30 then
        motionx = 30
    end
    if motiony < -30 then 
        motiony = -30 
    elseif motiony > 30 then
        motiony = 30
    end
end
于 2013-09-28T19:07:38.743 回答