如何有效处理使用Corona SDK删除对象?

我有一个按钮,每次点击按钮时会创建并施加力。 如何防止内存泄漏?

我正在使用director类从场景切换到场景。 以下是代码:

-- 从喷气式飞机位置发射火箭
local function fireTheRocket(event)
    if event.phase == "ended" then
      local fireBall = display.newImage( "rocket.png")
      fireBall.x = jet.x;
      fireBall.y = jet.y;

      GUI:insert(fireBall);

      physics.addBody(fireBall, "dynamic")
      fireBall:applyForce( 1000, 0, fireBall.x, fireBall.y )
    end
end
fireBtn:addEventListener("touch", fireTheRocket)
点赞
用户2893203
用户2893203

你可以尝试做这样的事情:

(在施加力之后插入)

local function DestroyRocket()
  fireBall:removeSelf()
  fireball = nil
end
timer.performWithDelay( 1000, DestroyRocket)

它会在1秒钟后摧毁你的火箭。或者,如果你想要在它与其他物体碰撞时销毁它,你可以在onCollision中处理。

2013-10-18 11:47:36