Attempting to have an object appear when a button is clicked and on being clicked for the second time, I want it to delete

My attempt so far is using “if” statements. My problem is that I make one “if” statement valid in the previous “if” statement. Is there a way to have the function stop after reading the end of the first “if” statement?

I commented in a example of what I’m trying to do in my sample code. Are there also “or” statements I should be familiar with?

Just to sum it up differently,
I included asterisks on two of the “if” statements. I would like for the function only go through with one of the two. This may be relatively simple but I’m honestly at a loss.

Thanks for any help!

[lua]
fromBox1 == false
local function boxListener (event)
if (event.phase == “began”) then

*if (fromBox1 == false) then

fromBox1 = true

box1GreenButton = display.newImageRect (“greenBall.png”, 20, 20 )
box1GreenButton.x = display.contentWidth/2.46
box1GreenButton.y = display.contentHeight/2.45
print (“1”)

–Example: stop boxListener Function here so that it doesn’t go on to read the next “if” statement.

end

*if (fromBox1 == true) then

print (“1b”)
fromBox1 = false
deleteFunction (box1GreenButton)

end

end
end[/lua] [import]uid: 35535 topic_id: 32059 reply_id: 332059[/import]

Yep, just join the two clauses with an “elseif” instead of doing two “if” clauses. It’ll check the first condition, and if true, execute that part and skip everything else. If the first clause fails, it’ll proceed to the “elseif” and check that. You can, of course, daisy-chain many “elseif” clauses in order of which one you want to “detect first”. And finally, the “else” clause (if you specify it) will execute if everything above it fails.

Brent [import]uid: 9747 topic_id: 32059 reply_id: 127790[/import]

Okay I’ll try that again but when I was experimenting with it I was getting an error code saying I needed or had too many “ends” even though I was careful. If I’m understanding correctly, my first “if” should stay as is and my second “if” should become “elseif”. Also for every “if” and “elseif” needs its corresponding “end”. Does that sound right to you? [import]uid: 35535 topic_id: 32059 reply_id: 127831[/import]

Yep, just join the two clauses with an “elseif” instead of doing two “if” clauses. It’ll check the first condition, and if true, execute that part and skip everything else. If the first clause fails, it’ll proceed to the “elseif” and check that. You can, of course, daisy-chain many “elseif” clauses in order of which one you want to “detect first”. And finally, the “else” clause (if you specify it) will execute if everything above it fails.

Brent [import]uid: 9747 topic_id: 32059 reply_id: 127790[/import]

Okay I’ll try that again but when I was experimenting with it I was getting an error code saying I needed or had too many “ends” even though I was careful. If I’m understanding correctly, my first “if” should stay as is and my second “if” should become “elseif”. Also for every “if” and “elseif” needs its corresponding “end”. Does that sound right to you? [import]uid: 35535 topic_id: 32059 reply_id: 127831[/import]

Never mind, it works fine! [import]uid: 35535 topic_id: 32059 reply_id: 128204[/import]

Never mind, it works fine! [import]uid: 35535 topic_id: 32059 reply_id: 128204[/import]