Temporarily Remove Button Listener

Hi. I fear this will be the noob question #1 for the day, yet, as the title says,i’m having big troubles in finding a wayto temporarily remove a listener from a button.

Specifically, I need to limit the jump preventing the user to press the button again if the character hasn’t landed yet. I would need a way to disable the button after certain conditions are met (the user has lifted his finger or a certain amount of time has passed), and enable it again when the character lands again.

How would you do it? :x
Any advice\contribution would be really appreciated!
Here’s the code or, for those who may want it, the main file on dropbox: https://dl.dropbox.com/u/22024851/main.rar

[code]
local widget = require “widget”
local physics = require “physics”
physics.start();
physics.setDrawMode( “hybrid” )
pressbutton1 = false;
liftbutton1 = true;

–GROUND–
ground = display.newRect(0, 780, 1280, 5)
physics.addBody( ground, { density = 1, bounce=0 } )
ground.bodyType = “static”

–HERO–
hero = display.newRect(590, 500, 100, 100)
physics.addBody( hero, { density = 0.1, bounce=0 } )
hero.bodyType = “dynamic”
hero.isFixedRotation = true

–BUTTONS–
upbutton = display.newRect(10, 600, 150, 150)
downbutton = display.newRect(1120, 600, 150, 150)

–BUTTON1EVENT–
function button1event( event )
if event.phase == “began” then
pressbutton1 = true;
liftbutton1 = false;
–print(“button1pressed”)
elseif event.phase == “ended” then
pressbutton1 = false;
liftbutton1 = true;
–print(“button1lifted”)
end
end

–JUMPFUNCTION–
function jumping()
if pressbutton1 then
print(“button1beingpressed”)
hero:applyForce ( 0, -70)
end
end

–UPDATESTAGEFUNCTION + LISTENERS AND STUFF–
local function updatestage( event )
jumping()
end
timer.performWithDelay(1, updatestage, -1)
upbutton:addEventListener( “touch”, button1event, 1)
--------------------------------------------- [import]uid: 173509 topic_id: 30628 reply_id: 330628[/import]

Havent started game logic yet but i’d try the following providing you have a flat floor.

If not then maybe a collision detector to check if the player is touching the floor for the if statement.

[lua]–JUMPFUNCTION–
function jumping()
if pressbutton1 then
if YOURPLAYER > THEFLOOR then --If you have a flat floor
print(“button1beingpressedwhileinair”)
else
print(“button1beingpressed”)
hero:applyForce ( 0, -70)
end
end
end
----------------[/lua]

edit: there may be better ways of doing it, I am very new to all this :slight_smile: [import]uid: 62706 topic_id: 30628 reply_id: 122723[/import]

Thanks for the answer! Unfortunately, using the workaround you provided the jump will have a fixed height, given by the single -70 impulse the character gets when the collision condition is satisfied.

What i’d like to implement is a function like this one:
if (FINGER HAS BEEN LIFTED FROM JUMP BUTTON) or (2 SECONDS HAVE PASSED)
then
(DISABLE JUMP BUTTON UNTIL THE CHARACTER HAS COLLIDED WITH GROUND AGAIN)

so that every time interval between 0 and the lifting of the finger will give a different effect:

button pressed for 0.5 sec -> mild effect
button pressed for 1 sec -> stronger effect
button pressed for 2 sec -> max effect
I hope I clarified my point!
[import]uid: 173509 topic_id: 30628 reply_id: 122724[/import]

Maybe something like this?

Haven’t used some of the functions below, so not sure if it will work right or not…

[lua]local function updatestage( event )
if hero.x > ‘FLOOR X VALUE’ then
print “jumping to early”
else
Runtime:addEventListener(“enterFrame”, jumping) – Jumping function will only start if the player is on the ground
end
end

timer.performWithDelay(1, updatestage, -1)
upbutton:addEventListener( “touch”, button1event, 1)[/lua]

That should allow the jumping function only to be called if the player is on the ground and pass a timer to the jumping function

[lua]function jumping(event)
if event.time > 0 and < 1499 then
print(“little jump”)
hero:applyForce ( 0, -70)
elseif event.time > 1499 and < 1999 then
print(“med jump”)
hero:applyForce ( 0, -105)
else
print(“big jump”)
hero:applyForce ( 0, -140)
end
Runtime:removeEventListener(“enterFrame”, jumping) – remove event listener
end
end[/lua]

edit:
another option may be to have the hero’s force relate to the time you input

In the below example, if event.time == 1000 (1 second) then it would be
1000 - 2000 / 14 = -71.42 force applied

[lua]function jumping(event)
while event.time < 2000 then
hero:applyForce(0, event.time - (event.time * 2) / 14)
end
Runtime:removeEventListener(“enterFrame”, jumping)
end[/lua] [import]uid: 62706 topic_id: 30628 reply_id: 122729[/import]

Runtime:removeEventListener(“enterFrame”, jumping) doesn’t work that way (or I cant make it work).
I tried both options myself before posting here.

Sorry for getting you mad Deano :frowning:

Now that I think about it, I could physically remove the whole jump button instead of removing the listener (which i cant seem to achieve)… I’ll try that. [import]uid: 173509 topic_id: 30628 reply_id: 122732[/import]

Lol, it works! Silliest workaround ever :DD

I’ll just leave the post here so maybe someone can provide a less stupid method of doing it :pp [import]uid: 173509 topic_id: 30628 reply_id: 122734[/import]

Erm, i spoke too soon, it didnt work cause i get an error when the app calls for button1 and it doesn’t exists.

But i found yet another stupid workaround:

when if (FINGER HAS BEEN LIFTED FROM JUMP BUTTON) or (2 SECONDS HAVE PASSED)
i move button1.x and y to some place outside the viewable area, only to make it reappear later.

I placed a mockup button1 image under the real button so that the user doesn’t notice. lol [import]uid: 173509 topic_id: 30628 reply_id: 122796[/import]

Havent started game logic yet but i’d try the following providing you have a flat floor.

If not then maybe a collision detector to check if the player is touching the floor for the if statement.

[lua]–JUMPFUNCTION–
function jumping()
if pressbutton1 then
if YOURPLAYER > THEFLOOR then --If you have a flat floor
print(“button1beingpressedwhileinair”)
else
print(“button1beingpressed”)
hero:applyForce ( 0, -70)
end
end
end
----------------[/lua]

edit: there may be better ways of doing it, I am very new to all this :slight_smile: [import]uid: 62706 topic_id: 30628 reply_id: 122723[/import]

Thanks for the answer! Unfortunately, using the workaround you provided the jump will have a fixed height, given by the single -70 impulse the character gets when the collision condition is satisfied.

What i’d like to implement is a function like this one:
if (FINGER HAS BEEN LIFTED FROM JUMP BUTTON) or (2 SECONDS HAVE PASSED)
then
(DISABLE JUMP BUTTON UNTIL THE CHARACTER HAS COLLIDED WITH GROUND AGAIN)

so that every time interval between 0 and the lifting of the finger will give a different effect:

button pressed for 0.5 sec -> mild effect
button pressed for 1 sec -> stronger effect
button pressed for 2 sec -> max effect
I hope I clarified my point!
[import]uid: 173509 topic_id: 30628 reply_id: 122724[/import]

Maybe something like this?

Haven’t used some of the functions below, so not sure if it will work right or not…

[lua]local function updatestage( event )
if hero.x > ‘FLOOR X VALUE’ then
print “jumping to early”
else
Runtime:addEventListener(“enterFrame”, jumping) – Jumping function will only start if the player is on the ground
end
end

timer.performWithDelay(1, updatestage, -1)
upbutton:addEventListener( “touch”, button1event, 1)[/lua]

That should allow the jumping function only to be called if the player is on the ground and pass a timer to the jumping function

[lua]function jumping(event)
if event.time > 0 and < 1499 then
print(“little jump”)
hero:applyForce ( 0, -70)
elseif event.time > 1499 and < 1999 then
print(“med jump”)
hero:applyForce ( 0, -105)
else
print(“big jump”)
hero:applyForce ( 0, -140)
end
Runtime:removeEventListener(“enterFrame”, jumping) – remove event listener
end
end[/lua]

edit:
another option may be to have the hero’s force relate to the time you input

In the below example, if event.time == 1000 (1 second) then it would be
1000 - 2000 / 14 = -71.42 force applied

[lua]function jumping(event)
while event.time < 2000 then
hero:applyForce(0, event.time - (event.time * 2) / 14)
end
Runtime:removeEventListener(“enterFrame”, jumping)
end[/lua] [import]uid: 62706 topic_id: 30628 reply_id: 122729[/import]

Runtime:removeEventListener(“enterFrame”, jumping) doesn’t work that way (or I cant make it work).
I tried both options myself before posting here.

Sorry for getting you mad Deano :frowning:

Now that I think about it, I could physically remove the whole jump button instead of removing the listener (which i cant seem to achieve)… I’ll try that. [import]uid: 173509 topic_id: 30628 reply_id: 122732[/import]

Lol, it works! Silliest workaround ever :DD

I’ll just leave the post here so maybe someone can provide a less stupid method of doing it :pp [import]uid: 173509 topic_id: 30628 reply_id: 122734[/import]

Erm, i spoke too soon, it didnt work cause i get an error when the app calls for button1 and it doesn’t exists.

But i found yet another stupid workaround:

when if (FINGER HAS BEEN LIFTED FROM JUMP BUTTON) or (2 SECONDS HAVE PASSED)
i move button1.x and y to some place outside the viewable area, only to make it reappear later.

I placed a mockup button1 image under the real button so that the user doesn’t notice. lol [import]uid: 173509 topic_id: 30628 reply_id: 122796[/import]