开发者

Make an object rotate to angle of tilt with accelerometer in CoronaSDK?

with my first game I have a doodle I control with the accelerometer. In many games I've seen that when the device is titled the object (in my case a doodle fish) rotate towards the tilt so it gives an illusion the "fish" swim down and if the device is tilted up and down the "fish" swims up and down.

How do I write that in lua when working with Corona SDK?

Here is my code for moving the doodle so far;

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.y开发者_StackOverflowGravity;
     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;

I've tried to make the player rotate to tilt but it's not working, it doesn't return any errors when I run it in the simulator, what am I doing wrong?

Basically what I want to do is when the accelerometer y value increase/decrease the player(fish) swim up and down but I only want it to tilt moderately (0 to +/- 45 degrees) or what ever seems realistic. Perhaps 25 -35 degrees is optimal?

My math is not up to date so I apologize for that, can someone please help me with this?

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)


Your overall code looks good. Several games I did use something similar.

What jumps out is you mentioning

I've tried to make the player rotate to tilt but it's not working, it doesn't return any errors when I run it in the simulator, what am I doing wrong?

Are you actually running the code on a device? The Corona SDK simulator does not support simulation for the accelerometer so the event will never fire.

If you are running on the device, include the Crawl Space Library. This will allow you to print() any form of data including tables. Then add

print(event)

to your onAccelerate() function, hook your device up to your development machine and watch the console for the data you actually receive. (Assuming you're developing for iOS using a Mac, use iPhone Configuration Utility to get the data). Then debug from there.

Include

io.output():setvbuf("no")

at the beginning of your main.lua to disable console output caching on the device first. Otherwise the console output of your device will be buffered and hard to debug.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜