【问题标题】:Löve2D: Objects disappear but body still existsLöve2D:物体消失但身体仍然存在
【发布时间】:2015-02-10 05:28:48
【问题描述】:

我正在做一个平台游戏,我正在尝试为关卡做一个加载系统,但是我有一个问题,这是我的代码的一部分:

 --main.lua
require "level1" ; require "level2"
function love.load()
              love.physics.setMeter(64)
              world = love.physics.newWorld(0,9.81*64, true)
   --[...]
   level = 1
end

function love.draw()
   if level == 1 then draw_level1()
   elseif level == 2 then draw_level2() end
end

function love.update(dt)
   world:update(dt)
   if gs == "loading2" then unload_level2() ; load_level2() end
   --[...]
 end

function love.keypressed(key)
   if key == "u" then level = 2 gs = "loading2" else gs = "normal" end
end

level1.lua:

--level1.lua
function load_level1(world)
obj1 = {}
obj1.body = love.physics.newBody(world, 111,111, "dynamic")
obj1.shape = love.physics.newRectangleShape(28,28)
obj1.fixture = love.physics.newFixture(obj1.body,obj1.shape)
end
function unload_level1()
 obj1 = nil
end
function draw_level1()
   love.graphics.polygon("line", obj1.body:getWorldPoints(obj1.shape:getPoints()))
end

和 level2.lua 是一样的,只是多了一个矩形,函数类似于“draw_level2()”

问题是当我按下U键时,物体消失了,但它们的身体还在(当玩家触摸它们时,会发生碰撞但它们是不可见的)我该怎么办?

【问题讨论】:

    标签: lua love2d


    【解决方案1】:

    首先,obj1 = nil 不起作用。你也需要摧毁它们。这是通过 obj1:destroy(), as outlined here 完成的。

    其次,在 love.update 中,每个关卡都在销毁和创建关卡。加载新关卡后,您可能想要破解一个快速变量(loaded = true)。

    当然,一个长期的解决方案是编写一个适当的关卡加载“类” - 但假设你是初学者,现在应该没问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多