Question

I'm working on a simple "breakout" game and I have problem reloading a map. for example: if I start with level1, break some bricks and lose, than I'm loading the same map again. next time that the ball collides with the same brick I "touched" before, will give me an error Attempt to remove an object that has already been removed

local map = lime.loadMap("maps/" .. currentLevel .. ".tmx")
local layer = map:getTileLayer("bricks_1")
local visual = lime.createVisual(map)
local physical = lime.buildPhysical(map)


function removeBricks(event)
    if event.other.isBrick then
        local brick = event.other
        transition.to(brick, {time = 20, alpha = 0})

        score = score + brick.scoreValue
        ScoreNum.text = score 

        -- remove brick
        brick:removeSelf()
        brick = nil
        ...
Was it helpful?

Solution

i think the second time you go to your game the event.other is not created are you using storyboard if so you can try to remove the scene after the game is over so when you go to your game again it will recreate all the object

OTHER TIPS

Have you try this?

transition.to(brick, {time = 20, alpha = 0, onComplete = function()
    if brick then
        brick:removeSelf()
        brick = nil
    end
end})

If you are using physics you also have to do a physics.removeBody(brick) before you remove the object itself so that it detaches from the physics engine. If not physics thinks it's still there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top