luamarmalade-quick

remove all notes with same name


Hello I'm am using Marmalde Quick, so lua to create a game.

In my game when a park of the screen in tuched it creates a new note and add that note to physics.

    function bgTouched(event)
        if (director:getCurrentScene() == gameScene) then
      if (gameState == gameStates.playing) then 
        if event.phase == "began" then
          addToRoundScore()
            if bodyType == 0 then
                -- Create object1
                b = director:createSprite(event.x, event.y, "textures/beachball.png")
                b.name = "ball"
                b.strokeWidth=0
                b.xAnchor = 1; b.yAnchor = 0 -- test non-0 anchor point for circle
                physics:addNode(b, {radius=40})
            elseif bodyType == 1 then
                -- Create object2
                b = director:createSprite(event.x, event.y, "textures/crate.png")
                b.name = "crate"
                b.strokeWidth=0
                b.xAnchor = 0; b.yAnchor = 0.5 -- test non-0 anchor point for rectangle
                b.xScale = 2; b.yScale = 1 -- test different scale
                physics:addNode(b, {} )
            elseif bodyType == 2 then
                -- Create obejct3
                b = director:createSprite(event.x, event.y, "textures/triangle.png")
                b.name = "tri"
                b.xAnchor = 0.5; b.yAnchor = 1 -- test non-0 anchor point for polygon
                physics:addNode(b, {shape={0,0, 95,0, 48,81}} )end

        b.rotation = 22.5
        bodyType = (bodyType + 1) % 3

    end
  end

  end

end
bg:addEventListener ("touch", bgTouched) 

when an event happens I want to remove all the notes created, I tried using the following:

physics:removeNode(b)
b:removeFromParent()

but this only removes the last created not I want to remove them all, is there some way to do this.

Thanks


Solution

  • If I understand right that you want to clear the nodes table before the event.phase == "began" processing where you add nodes, you could reset the physics table:

    physics = {}
    

    If other parts of code are referencing the physics node and they can't be notified that physics points to a new table, you could loop over all items of table and nil them:

    for k,v in pairs(physics)
        physics[k] = nil
    end