Touch event: hold button pressed

  • Hi everyone,

    I want to charge up when holding the button, using touch controls.

    On release of the button I release the charge.

    Buttons have an on button clicked event which reacts to releasing, so the later part is covered.

    But the only hold event i find is touch object and that doesnt seem to work, not sure what it's supposed to do.

    Anyone have an idea how to get a OnholdButton

    Interaction registered?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You can try something like this:

    Add an instance variable timePressed to the button sprite.

    Create these events:

    Touch: On touched object ButtonSprite -> ButtonSprite set variable timePressed to (time)
    
    Touch: Is touching object ButtonSprite  
    ButtonSprite: compare variable timePressed not equal 0
                                         // use formula (time-ButtonSprite.timePressed) to get the duration of hold
    
    Touch: Is NOT touching object ButtonSprite 
    ButtonSprite: compare variable timePressed not equal 0 
    System: Trigger once
                                         -> ButtonSprite set variable timePressed to 0
                                        // release the charge based on hold duration
    [/code:1rbwue0c]
  • dop2000 thanks for the reply,

    now i have the additional challenge that i want to fill up a meter during that charge up. from what i understand you only calculate the charged amount on release, hence the value can be displayed growing during hold.

    i'd have to add a flag that i'd set upon first clicking, while this is valid i do the charge up, so i can display it growing. then on release i'd stop it.

    rather ugly, do you have a better suggestion?

  • facecrime

    No, see my code - you can calculate (time-ButtonSprite.timePressed) to fill up the meter while the button is being held in "Is Touching" event. You can add other conditions, say the meter starts filling up if holding time is more than 1 second, and stops after 3 seconds.

    The same formula can be used when touch is released or after finger has moved away from the button - in "Is NOT Touching" event, before resetting timePressed to 0.

    You don't need another flag, since you have timePressed variable. When it's 0, you know that the button is not in touch.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)