如何将加速度计 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)