0

加速度計 onTilt Events の動きを上下左右に 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

質問は明確ではありませんが、これでできると思います:

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 に答える