与我的第一场比赛我有一个涂鸦我用加速度计控制。在很多游戏中,我已经看到,当设备标题为对象(在我的情况下为嘟嘟鱼)朝向倾斜旋转时,因此它给人一种错觉,即“鱼”向下游泳,并且如果设备在“鱼” “上下游泳。使用CoronaSDK中的加速度计将物体旋转至倾斜角度?
如何在使用Corona SDK时在lua中编写该代码?
这是我的代码移动涂鸦到目前为止;
display.setStatusBar(display.HiddenStatusBar)
system.setAccelerometerInterval(50)
_W = display.contentWidth
_H = display.contentHeight
local bg = display.newImageRect("images/bg.png", 480, 320)
bg:setReferencePoint(display.CenterReferencePoint)
bg.x = _W/2
bg.y = _H/2
local player = display.newImageRect("images/doodle.png", 128, 64)
player:setReferencePoint(display.CenterReferencePoint)
player.x = _W/2
player.y = _H/2
-- Set up the Accelerometer values in Landscape
local motionX = 0
local motionY = 0
local function onAccelerate(event)
motionX = 10 * event.yGravity;
motionY = 10 * event.xGravity;
end
Runtime:addEventListener ("accelerometer", onAccelerate);
-- Make the player move on tilt.
local function movePlayer (event)
player.x = player.x + motionX;
player.y = player.y + motionY;
end
Runtime:addEventListener("enterFrame", movePlayer)
local function screenBoundaries (event)
-- Left side boundaries
if player.x < 0 + player.width/2 then
player.x = 0 + player.width/2
end
-- Right side boundaries
if player.x > display.contentWidth - player.width/2 then
player.x = display.contentWidth - player.width/2
end
-- Upper boundaries
if player.y < 0 + player.height/2 then
player.y = 0 + player.height/2
end
-- Lower boundaries
if player.y > display.contentHeight - player.height/2 then
player.y = display.contentHeight - player.height/2
end
end
Runtime:addEventListener("enterFrame", screenBoundaries)
EDIT;
我试图让玩家旋转到倾斜,但它不工作,当我在模拟器中运行它时不会返回任何错误,我做错了什么?
基本上我想要做的是当加速度计y值增加/减少玩家(鱼)上下游动时,但我只希望它适度地倾斜(0至+/- 45度)或者看起来真实的。也许25-35度是最佳的?
我的数学不是最新的,所以我对此表示歉意,有人能帮助我吗?
display.setStatusBar(display.HiddenStatusBar)
system.setAccelerometerInterval(50)
_W = display.contentWidth
_H = display.contentHeight
local ceil = math.ceil
local pi = math.pi
local atan2 = math.atan2
local playerRotation
-- Put the doodle fish in the center if the screen.
local player = display.newImageRect("images/doodle.png", 128, 64)
player:setReferencePoint(display.CenterReferencePoint)
player.x = _W/2
player.y = _H/2
-- My rotation function, not sure how to do it right.
local function getPlayerRotationAngle(xGravity, yGravity)
angle = math.atan2(xGravity, yGravity)
angle = angle*(180/pi)
playerRotate = ceil(360 - angle(xGravity, yGravity))
return playerRotate
end
-- Set up the Accelerometer values in Landscape
local motionX = 0
local motionY = 0
local function onAccelerate(event)
motionX = 5 * event.yGravity;
motionY = 5 * event.xGravity;
-- Should I put the rotation here or down in the movePlayer function?
player.rotation = playerRotate(event.yGravity, event.xGravity);
end
Runtime:addEventListener ("accelerometer", onAccelerate);
-- Make the player move on tilt.
function movePlayer (event)
player.x = player.x + motionX
player.y = player.y + motionY
end
Runtime:addEventListener("enterFrame", movePlayer)
我使用cmote应用程序在模拟器上测试加速度计,它工作得非常好。加速度计工作正常,鱼跟随设备的倾斜。我想要帮助的是让鱼旋转一点,这样当Y值变化时,它看起来像在向上/向下游动。我使用print()函数,但不在这篇文章中。 – 2011-06-04 18:43:04
我实际上认为如果X值变化时鱼向上/向下旋转会更好?所以如果我可以得到一些getPlayerRotationAngle函数的帮助,因为它不会像现在这样旋转播放器,我不知道如何解决它。 – 2011-06-04 18:52:39
在这种情况下,立即跳出的是您的函数被称为“getPlayerRotationAngle”,但您在“player.rotation = playerRotate(event.yGravity,event.xGravity)中调用”playerRotate“(您的本地返回值的名称) ;”并永远不会调用实际的功能。 – 0x90 2011-06-04 20:35:40