2011-05-31 45 views
1

與我的第一場比賽我有一個塗鴉我用加速度計控制。在很多遊戲中,我已經看到,當設備標題爲對象(在我的情況下爲嘟嘟魚)朝向傾斜旋轉時,因此它給人一種錯覺,即「魚」向下游泳,並且如果設備在「魚」 「上下游泳。使用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) 

回答

0

您的整體代碼看起來不錯。 幾個遊戲我使用類似的東西。

什麼跳出來的是你提的

我試圖讓玩家旋轉,傾斜,但它不工作,當我在模擬器中運行它,它不返回任何錯誤,我是什麼做錯了?

您是否真的在設備上運行代碼? Corona SDK模擬器不支持加速計的模擬,所以事件永遠不會觸發。

如果您在設備上運行,請包括Crawl Space Library。 這將允許您打印()任何形式的數據,包括表格。 然後加入

print(event) 

您onAccelerate()函數,勾你的設備到你的開發機器,看你實際收到的數據控制檯。 (假設您使用Mac爲iOS開發,請使用iPhone配置實用程序來獲取數據)。然後從那裏調試。

包括

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

你main.lua的開始禁止設備的控制檯輸出緩存第一。否則,您的設備的控制檯輸出將被緩衝,難以調試。

+0

我使用cmote應用程序在模擬器上測試加速度計,它工作得非常好。加速度計工作正常,魚跟隨設備的傾斜。我想要幫助的是讓魚旋轉一點,這樣當Y值變化時,它看起來像在向上/向下遊動。我使用print()函數,但不在這篇文章中。 – 2011-06-04 18:43:04

+0

我實際上認爲如果X值變化時魚向上/向下旋轉會更好?所以如果我可以得到一些getPlayerRotationAngle函數的幫助,因爲它不會像現在這樣旋轉播放器,我不知道如何解決它。 – 2011-06-04 18:52:39

+0

在這種情況下,立即跳出的是您的函數被稱爲「getPlayerRotationAngle」,但您在「player.rotation = playerRotate(event.yGravity,event.xGravity)中調用」playerRotate「(您的本地返回值的名稱) ;」並永遠不會調用實際的功能。 – 0x90 2011-06-04 20:35:40