2013-07-08 33 views
0

嗨,我有這個小天生的功能,從天上掉下雲。改變下拉速度的更好方法

local randomBad1 = function() 
    local badC1 = display.newImage("BCloud1.png") 
    badC1.x = math.random (0, 450); badC1.y = -50 
    physics.addBody(badC1, { density=.1, bounce=0.1, friction=.2, radius=45, filter=badc1CollisionFilter }) 
    badC1.name = "BCloud1"  
    badC1.isSensor = true 
    badC1.rotation = math.random(-20,20) -- Rotate the object 
    badC1.gravityScale = 0.40 
    local cleanup 
    cleanup = function() 
     if badC1 then 
      if badC1.y >600 then 
       badC1:removeSelf() 
       badC1 = nil 
      end 
     end 
    end 
    Runtime:addEventListener("enterFrame", cleanup) 
end 
randomBadC1 = timer.performWithDelay(3000, randomBad1, 0) 

所以我想知道是否有更好的方法來改變faling的速度而不是'gravityScale'?

Kevin-

回答

2

您可以使用applyForce來做到這一點雲。我增加了一些行到你的代碼如下:

local randomBad1 = function() 
    local badC1 = display.newImage("BCloud1.png") 
    badC1.x = math.random (0, 450); badC1.y = 50-- -50 
    physics.addBody(badC1, { density=.1, bounce=0.1, friction=.2, radius=45, filter=badc1CollisionFilter }) 
    badC1.name = "BCloud1"  
    badC1.isSensor = true 
    badC1.rotation = math.random(-20,20) -- Rotate the object 

    ------------------------------------------------------------------- 
      -- These lines will do it for you -- 
    ------------------------------------------------------------------- 
    yFor = math.random(1000) -- choosing a random y directional Force 
    print("yFor = "..yFor) 
    badC1:applyForce(0, yFor, badC1.x, badC1.y) -- apply the force to your cloud 
    ------------------------------------------------------------------- 

    badC1.gravityScale = 0.40 
    local cleanup 
    cleanup = function() 
    if badC1 then 
     if badC1.y >600 then 
      badC1:removeSelf() 
      badC1 = nil 
     end 
    end 
end 
Runtime:addEventListener("enterFrame", cleanup) 
end 
randomBadC1 = timer.performWithDelay(3000, randomBad1, 0) 

欲瞭解更多信息,可以參考:Corona Physics Body Properties.

保持編碼............ :)

+0

感謝krs:D這對我很有用! –

0

如果雲是唯一墜落的物體,我會完全改變重力。

physics.setGravity(gx, gy) 
+0

我有多個對象下降,我喜歡單獨改變速度 –