Löve2D:物体消失了,但身体仍然存在。

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

-- 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键时,对象消失了,但它们的物体仍然存在(当玩家碰到它们时,碰撞发生但它们是看不见的)。我该怎么办?

点赞
用户1517394
用户1517394

首先,obj1 = nil 是行不通的。你需要摧毁它们。可以通过 obj1:destroy() 来实现,在这里描述

其次,在 love.update 中,每个级别被销毁并重新创建。你可能想要钻一个快速的变量来切换一下 (loaded = true),一旦你已经加载了新的级别。

当然,长期的解决方案是编写一个适当的级别加载“类” - 但假设你是一个初学者,现在这应该还不错。

2015-02-09 21:27:29