물리 엔진에서 객체를 추가/제거하려고합니다 (addBody() 및 removeBody()). 내가 작업하고있는 응용 프로그램은 모듈 식이므로 두 파일 중 하나에 문제가 있습니다.Corona SDK 물리 단체 추가/충돌 허용하지 않음
오브젝트 파일 (TransmitterObject) 또는 (주) 주 파일 :
이 모두 관련 코드 :
main.lua
local physics = require("physics")
physics.start()
physics.setGravity(0,0)
physics.setDrawMode("debug")
local TransmitterObject = require("TransmitterObject")
function updateGame(event)
if(ITERATIONS % 100 == 0) then
tran1:activate() --create new physics object here
end
ITERATIONS = ITERATIONS + 1
--print(ITERATIONS)
end
Runtime:addEventListener("enterFrame", updateGame)
TransmitterObject.lua
function transmitter.new(props) --constructor
Transmitter =
{
x = props.x,
y = props.y,
receivers = props.receivers
}
return setmetatable(Transmitter, transmitter_mt)
end
function transmitter:activate()
local group = math.random(1, #self.receivers)
local receiver = math.random(1,#self.receivers[group])
local x , y = self.receivers[group][receiver][1], self.receivers[group][receiver][2]
local d = math.sqrt(math.pow((self.x-x),2) + math.pow((self.y-y),2))
local dx = math.abs(self.x - x)
local angle = math.deg(math.acos(dx/d))
local beam = display.newRect(self.x,self.y, d, 10)
beam:setReferencePoint(display.TopLeftReferencePoint)
beam.rotation = 180 + angle
beam:setFillColor(0,255,0)
beam.alpha = 0
local function add(event)
physics.addBody(beam, "static")
end
local function delete(event)
physics.removeBody(beam)
end
transition.to(beam, { time=1000, alpha=1.0, onComplete=add })
transition.to(beam, { time=1000, delay=2500, alpha=0, onComplete=delete})
end
이제 문제를 조금 더 자세히 설명하겠습니다. 기본적으로 'enterFrame'이 발생하는 매 100 시간마다 나는 송신기 객체 (tran1)에 함수 'activate'를 호출하도록 지시합니다. 그러면 좌표를 얻기 위해 기본 수학이 수행됩니다. 그런 다음 계산 된 정보를 사용하여 사각형 (보)을 만들고 일부 특성을 설정합니다. 그것은 모든 기본적인 것들입니다. 다음으로 나는 눈에 보이지 않는 상태 (알파 = 0)에서 1 초간의 가시 상태로 천이하라고 말한다. 객체를 물리 엔진에 추가하는 'add'함수를 언제 호출 할 것인가. 다음 행과 마찬가지로 객체를 제거합니다.
즉, physics.setDrawMode ("debug")를 설정하면 보 객체가 정적 본문으로 나타나지만 충돌을 허용하지 않습니다. 왜 위의 코드가 빔 객체에 대한 충돌을 허용하지 않는지 아는 사람이 있습니까?
물리 엔진에서 올바르게 작동하는 다른 객체가 있음을 기억하십시오.