1
在Corona論壇沒有任何運氣,所以我想我會在這裏嘗試。簡單的物理對象在電暈/ Lua(OOP)
我只是試圖創建一個與樞軸關節的對象。看起來很簡單,但它不起作用。這裏有一個非常簡單的項目鏈接演示該問題:
http://www.megaupload.com/?d=TAN72VRK
我只是無法弄清楚如何組織添加到物理系統,如果這些機構是在一個創建的對象的一部分單獨的文件(類)。希望有人能夠幫助 - 一直在這個問題上掙扎數週。
編輯:
main.lua:
local JointedObj = require("JointedObj")
local physics = require("physics")
physics.start()
local o = JointedObj.new()
o.x = 50
o.y = 200
local wall = display.newRect(350, 10, 50, 300)
physics.addBody (wall, "static", {density=1, friction=1, bounce=.5})
local floor = display.newRect(0, 300, 400, 10)
physics.addBody (floor, "static", {density=1, friction=1, bounce=.5})
--toss the object against the wall
o:toss(120, -160, o.x, o.y)
JointedObj.lua:
module(..., package.seeall)
--constructor
function new()
local obj = display.newGroup()
local red = display.newImageRect("images/red.png", 27, 18)
local blue = display.newImageRect("images/blue.png", 11, 9)
blue.x = -16
obj:insert(red)
obj:insert(blue)
function obj:toss(xForce, yForce, xPos, yPos)
--THIS WORKS, BUT OBVIOUSLY THE OBJECT HAS NO JOINTS
--physics.addBody(obj, "dynamic", {density=1, friction=1, bounce=0.3})
--obj:applyForce(xForce, yForce, xPos, yPos)
--THIS IS WHAT I WANT TO DO. AS-IS, THE OBJECT JUST FALLS THROUGH EVERYTHING
physics.addBody(red, {density=1, friction=1, bounce=0.3})
physics.addBody(blue, {density=1, friction=1, bounce=0.3})
myJoint = physics.newJoint("pivot", red, blue, 0,0)
myJoint.isLimitEnabled = true
myJoint:setRotationLimits(-30, 30)
--obj:applyForce(xForce, yForce, xPos, yPos) --THIS THROWS A NIL ERROR IF UNCOMMENTED
end
return obj;
末
是的,想通了。真的讓我們很難擁有屬於物理系統一部分的OO模塊,但是我得到了它的工作,這要歸功於display.getCurrentStage():insert(red)和許多重構。 – clua7 2011-06-17 16:55:05