Reputation: 1499
what i am trying to achieve here is... if i touch one image it gets disabled. then i want to enable it by touching another image. but after disabling i can't re-enable it. i have a touch event for first image which i call using
Runtime:addEventListener( "touch", diceFunction2 )
and i remove it using
Runtime:removeEventListener( "touch", diceFunction2 )
but i am trying to add it again using this touch event handler
local function guti11touched( event )
if event.phase == "began" then
if playerTurn%2==0 then
if rand == 6 and gutiStatus.guti11.status == false then
gutiStatus.guti11.status = true
gutiStatus.guti11.count = 1
local i = gutiStatus.guti11.count
transition.moveTo(guti11, {x=background.x+player1Sequence[i][1],y=background.y-player1Sequence[i][2]})
elseif gutiStatus.guti11.status == true and gutiStatus.guti11.count+rand <= 57 then
gutiStatus.guti11.count = gutiStatus.guti11.count + rand
local i = gutiStatus.guti11.count
transition.moveTo(guti11, {x=background.x+player1Sequence[i][1],y=background.y-player1Sequence[i][2]})
if i == 57 then
guti11.isClickable = false
end
end
end
elseif event.phase == "ended" then
Runtime:addEventListener( "touch", diceFunction2 )
end
return true
end
can someone please tell me what is the correct way to do it and what i'm doing wrong.
EDIT
this is my first image eventhandler. i already tried by adding dice:addEventListener( "touch", diceFunction2 ) , but in that case the image just keep rotating and never stop, and as i'm choosing images randomly so can i do it in place of runtime?
--for rotating dice
local function diceFunction2( event )
if event.phase == "began" then
local laserChannel = audio.play( laserSound )
rand=math.random(6)
dice = display.newImage("images/dice3droll.png")
dice.x = 75
dice.y = 480
dice:scale(1/scale_factor,1/scale_factor)
display.getCurrentStage():setFocus( dice )
dice.isFocus = true
Runtime:addEventListener( "enterFrame", rotateDice )
elseif dice.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
if rand == 1 then
Runtime:removeEventListener( "enterFrame", rotateDice)
dice:removeSelf()
local dice1 = display.newImage("images/ek.png")
dice1.x = 75
dice1.y = 480
dice1:addEventListener( "touch", diceFunction2 )
display.getCurrentStage():setFocus( nil )
dice.isFocus = false
elseif rand == 2 then
Runtime:removeEventListener( "enterFrame", rotateDice)
dice:removeSelf()
local dice2 = display.newImage("images/dui.png")
dice2.x = 75
dice2.y = 480
display.getCurrentStage():setFocus( nil )
dice.isFocus = false
elseif rand == 3 then
Runtime:removeEventListener( "enterFrame", rotateDice)
dice:removeSelf()
local dice3 = display.newImage("images/tin.png")
dice3.x = 75
dice3.y = 480
display.getCurrentStage():setFocus( nil )
dice.isFocus = false
elseif rand == 4 then
Runtime:removeEventListener( "enterFrame", rotateDice)
dice:removeSelf()
local dice4 = display.newImage("images/charr.png")
dice4.x = 75
dice4.y = 480
display.getCurrentStage():setFocus( nil )
dice.isFocus = false
elseif rand == 5 then
Runtime:removeEventListener( "enterFrame", rotateDice)
dice:removeSelf()
local dice5 = display.newImage("images/pach.png")
dice5.x = 75
dice5.y = 480
display.getCurrentStage():setFocus( nil )
dice.isFocus = false
elseif rand == 6 then
Runtime:removeEventListener( "enterFrame", rotateDice)
dice:removeSelf()
local dice6 = display.newImage("images/chokka.png")
dice6.x = 75
dice6.y = 480
display.getCurrentStage():setFocus( nil )
dice.isFocus = false
end
end
end
end
this is my rotatedice function
local function rotateDice()
dice.rotation = dice.rotation + 200
end
Upvotes: 0
Views: 1508
Reputation: 3063
Runtime touch handlers cover the whole screen. If you want a touch event on an object, you normally add the touch event directly to that object:
someImage:addEventListener("touch", myTouchFunction)
Then I would not worry about adding and removing the listeners but instead set some flags that would indicate if the touch was allowed to happen or not.
Upvotes: 1