2014-02-20 28 views
1
--*************************************************** 

-- drawButtons() --> 

--*************************************************** 

local flyPlane = function(event) 

    if event.phase == "began" and gameIsActive == true then 
     print("began") 
     physics.setGravity(0, -10) 
    elseif event.phase == "ended" and gameIsActive == true then 
     print("ended") 
     physics.setGravity(0, 7) 
     inBound = true 
    elseif event.phase == "moved" and gameIsActive == true then 
     -- do nothing 
    end 

end 

--*************************************************** 

-- keepPlaneInBound() --> 

--*************************************************** 

local keepPlaneInBound = function() 
    -- print("checking") 
    if paperPlane.y <= paperPlane.height + 10 and gameIsActive == true and inBound == true then 
     inBound = false 
     paperPlane.y = paperPlane.height + 12 
    end 

    if paperPlane.y > display.contentHeight - paperPlane.height - scale.height and gameIsActive == true then 
     paperPlane.y = display.contentHeight - paperPlane.height - scale.height 
    end 

end 

函數flyPlane在「touch」eventlistner上設置,keepPlaneInBound被設置爲「enterframe」eventlistner。我想達到的是當飛機超過上限。其之前的physcis.setGravity值將被完全刪除,並且在用戶擡起手指時(當事件=「結束」時)將分配給它的新值。 奇怪的是它不接受新的physics.setGravity()值。如果我使用physics.pause(),然後爲其分配新的重力值。在physics.start它首先完成,因此再向上移動它以前setGravity值的部分..:/賦值physics.setGravity一個不同的值

+0

有一個先天沒有錯,你的重力代碼,不知道的邊界檢查,因爲它看起來像有在'keepPlaneInBound'一個額外的「結束」,這將導致語法錯誤。所以請糾正它,並編輯你的問題,以顯示你註冊事件回調的代碼,這可能是錯誤的地方。 – Schollii

+0

@Schollii。嗨,謝謝你幫助我。這裏所說的「結局」是錯誤的。我已經編寫了代碼。現在代碼就是它在我的記事本++中的樣子。 –

+0

好,但正如我所提到的,我看不出您的代碼有什麼問題,我甚至嘗試在測試應用程序中更改引力,並且它工作正常,所以問題與代碼不顯示。請顯示您的addEventListener調用,以及您用來調用這兩個函數的任何其他代碼。 「函數flyPlane設置爲」touch「」enterframe「eventlistner」沒有意義,需要看代碼。 – Schollii

回答

0

你不應該有「flyPlane」,如「enterFrame事件」的回調。你應該有這樣的東西:

local function flyPlane(event) 
    your_code() 
end 

local function keepPlaneInBound(event) 
    your_code() 
end 

paperPlane:addEventListener("touch", flyPlane) 
Runtime:addEventListener("enterFrame", keepPlaneInBound) 

這就是你所需要的。 (鑑於你的遊戲邏輯是正確的)

+0

嗨,亞當,感謝您的幫助。是的,這讓我接近解決方案。但主要問題仍然存在。當玩家從屏幕上擡起手指時,我想分配給paperPlane對象的重力值。物體保持自己堅持以前分配的重力值(在「開始」事件中分配)。 –

+0

甚至有可能的唯一原因是如果gameIsActive布爾值被評估爲false。 - 順便說一句,你不必做 「如果boolean == true然後」,你可以說「如果布爾然後」 –

0

夥計。我得到了我正在尋找的東西。爲了給它分配一個新的重力值,完全消除施加到物理體上的力的技術是簡單地將其bodyType改變爲不同的類型,然後將所需的bodyType再次分配給對象。這樣,以前的重力值對你的物理對象的影響就完全消失了。這就是我所做的。它確實爲我工作。

-- drawButtons() --> 

--*************************************************** 

local flyPlane = function(event) 

    if event.phase == "began" and gameIsActive == true then 
     print("began") 
     -- paperPlane.bodyType = "dynamic" 
     physics.setGravity(0, -10) 
     -- physics.start() 
    elseif event.phase == "ended" and gameIsActive == true then 
     print("ended") 
     -- paperPlane.bodyType = "dynamic" 
     physics.setGravity(0, 7) 
     -- physics.start() 
    elseif event.phase == "moved" and gameIsActive == true then 
    end 

end 

--*************************************************** 

-- keepPlaneInBound() --> 

--*************************************************** 

local keepPlaneInBound = function() 
    -- print("checking") 
    if paperPlane.y <= paperPlane.height + 10 and gameIsActive == true then 
     print("Out of Bound -y: ", paperPlane.bodyType) 
     physics.pause() 
     paperPlane.bodyType = "static" 
     paperPlane.y = paperPlane.height + 11 
    elseif paperPlane.y > display.contentHeight - paperPlane.height - scale.height and gameIsActive == true then 
     print("Out of Bound +y: ", paperPlane.bodyType) 
     physics.pause() 
     paperPlane.bodyType = "static" 
     paperPlane.y = display.contentHeight - paperPlane.height - scale.height - 1 
    else 
     print("In Bound: ", paperPlane.bodyType) 
     paperPlane.bodyType = "dynamic" 
     physics.start() 
    end 

end 
+0

這不可能是因爲setGravity立即工作(即只要事件處理程序返回)在所有物體上,而不會暫停物理(我已驗證)。而且,爲了使新的重力生效,你不得不改變每一個物理體類型(完全)。這是一個黑客攻擊,其他的錯誤。 – Schollii