0
votes

Very quick, and probably easy question... but its left me stumped for the last 30 minutes... But how can I remove items from the canvas/screen?

I know this works with removeSelf(), but I'm not quite sure how to use that in my example:

display.setStatusBar( display.HiddenStatusBar )

function cat1()
    displayCategory(1)

    print( "clicked 1" ) 
end

function cat2()
    displayCategory(2)

    print( "clicked 2" ) 
end

function cat3()
    displayCategory(3)

    print( "clicked 3" ) 
end

--category display
function displayCategory(cat)

    if (cat == nil) then
        cat = 1
    end

    print( cat )

    if (cat == 1) then
        local item1 = display.newRect(100,100,100,100)
        item1:setFillColor(255,255,0)
    elseif (cat == 2) then
        local item2 = display.newRect(200,100,100,100)
        item2:setFillColor(255,0,255)
    elseif (cat == 3) then
        local item3 = display.newRect(300,100,100,100)
        item3:setFillColor(0,255,255)
    end

end

--category buttons
local catBtn1 = display.newRect(0,0,50,50)
catBtn1:setFillColor( 255,0,0 )
catBtn1:addEventListener( "tap", cat1 )

local catBtn2 = display.newRect(60,0,50,50)
catBtn2:setFillColor( 0,255,0 )
catBtn2:addEventListener( "tap", cat2 )

local catBtn3 = display.newRect(120,0,50,50)
catBtn3:setFillColor( 0,0,255 )
catBtn3:addEventListener( "tap", cat3 )

Right now, every time I click on of my buttons, the corresponding item pops up, but doesnt disappear when I click on any of the other buttons. How would I go about that?

1

1 Answers

1
votes

First, create those itens on start, before the displayCategory function. Then you set them to not visible, like this:

local item1 = display.newRect(100,100,100,100)
item1:setFillColor(255,255,0)
item1.isVisible = false

Later you set the proper itens to visible or not as appropriate, in case of item 2 it would be

item1.isVisible = false
item2.isVisible = true
item3.isVisible = false