Help with event listener

I am trying to use a single button to perform multiple functions. To be specific I am making a card game and need a card to be dealt every time the button is clicked. Right now I have numbers randomly generated and numbers assigned to specific cards. So for the first listener I have a number generated and a card displayed on the screen. Then on the event.phase == “ended” I remove the event listener so it will generate the first card then stop. But the problem I am having is when I am trying to call the 2nd card. If I create another listener on the same button the second card will appear at the same time as the first does. I am having trouble getting the first card to appear on click one, the second on click two, etc…

Any suggestions? [import]uid: 139423 topic_id: 26208 reply_id: 326208[/import]

no love? [import]uid: 139423 topic_id: 26208 reply_id: 106449[/import]

You didn’t show any code so you are less likely to get a reply; without code everyone relies on a clear explanation and that isn’t always easy.

If you are saying removing and re-adding the event listener causes it to fire twice (which I think you are? having trouble picturing it) then you may want to reexamine event phases.

Again, code would help :wink: [import]uid: 52491 topic_id: 26208 reply_id: 106695[/import]

Alright sorry about that, i’ll try to elaborate. I’m pretty new so my explanations may not be perfectly clear.

First the game deals random cards. Then you should be able to press a button and get dealt more random cards (each time you press the button 1 more random card is dealt).

So for the first card dealt:
[lua]local ran5 = math.random(13) – random number generated to select card.
local function hit (event)

if event.phase == “began” then – This if statement is to select the card based on the random number
print(“ran num is:”, ran5)
if ran5 == 1 then
local ran11 = display.newImage(_1)
ran11.x = _W + 70; ran11.y = 340
elseif ran5 == 2 then
local ran12 = display.newImage(_2)
ran12.x = _W + 70; ran12.y = 340
.
.
.

elseif ran5 == 13 then
local ran23 = display.newImage(_13)
ran23.x = _W + 70; ran23.y = 340
else
print(“error”)
end

if ran5 > 10 then – It is a blackjack game so the jacks, queens etc. have a value of 10
ran5 = 10
end

playerScore:removeSelf() – This is to update the score shown and include the new card
playerScore2 = display.newText(ran3+ran4+ran5, 260,450,nil,15)

elseif event.phase == “ended” or event.phase == “cancelled” then
hitBtn:removeEventListener(“touch”, hit) – Removing event listener for the cards dealt
end

return true

end

hitBtn:addEventListener(“touch”, hit)

– Hit 2

local ran6 = math.random(13)

local function hit2 (event) – The second function to call a second card

if event.phase == “began” then
print(“ran num is:”, ran6)
if ran6 == 1 then
local ran11 = display.newImage(_1)
ran11.x = _W + 90; ran11.y = 340
elseif ran6 == 2 then
local ran12 = display.newImage(_2)
ran12.x = _W + 90; ran12.y = 340
.
.
.

elseif ran6 == 13 then
local ran23 = display.newImage(_13)
ran23.x = _W + 90; ran23.y = 340
else
print(“not”)
end

if ran6 > 10 then
ran6 = 10
end

elseif event.phase == “ended” or event.phase == “cancelled” then
hitBtn:removeEventListener(“touch”, hit2)
end

return true

end

hitBtn:addEventListener(“touch”, hit2)[/lua]

So as you probably know, when the button is touched both of these cards are dealt at the same time. What I am wanting to do is to touch the button and have only the first card, then if the same button is pressed again the second card is then dealt.

I have tried looking around for it but haven’t been able to find anything other than a button dedicated to doing just one thing.

[import]uid: 139423 topic_id: 26208 reply_id: 107150[/import]