Double Action with the same button

Hi,

I tried to use the same button to do two different actions. First click, the circle to move 10 units on the ruler. Second click, I want the circle to move again to 0 (zero) unit. I have a problem with the second click to return the circle to 0. I tried using some JavaScript code, but I didn't see progress.

I really appreciate any help you can provide.

Double Action with Same Button.zip (139.1 KB)

Your code is generally correct, but placed in the wrong spot.

If you add a console.log message that prints the state of the isForward variable, you'll find that the more you click the button, the more calls to the movingCircle() function you'll see, and the last log always presents as true. Thus it is always running in the forward direction.

The issue is that your toggleTimeline() is set to run on the element's On Mouse Click handler in the Hype editor, and then you also that code make a new movingCircle() function each time and install its own click handler via addEventListener().

So your options would be:

  • Remove Hype On Mouse Click handler and instead only call the toggleTimeline() once via the Scene Inspector's On Scene Load handler. This will properly set it up and it will work. The downside of this approach is that if you have multiple scenes, you'll be installing multiple handlers on the click.

  • Instead just have the toggleTimeline() code use information Hype already has to determine if it should go forward or backwards. You could do something like see if the timeline is at the 0 time. If it is, then you know you need to go forward. So your code could just look like:

    if (hypeDocument.currentTimeInTimelineNamed('movingCircle') == 0) {
        // Avanzar en la línea de tiempo
        hypeDocument.continueTimelineNamed('movingCircle', hypeDocument.kDirectionForward, false);
    } else {
        // Retroceder en la línea de tiempo
        hypeDocument.continueTimelineNamed('movingCircle', hypeDocument.kDirectionReverse, false);
    }
1 Like

Thank you @jonathan!

Today I learned something new with you...the On Scene Load. :wink:

1 Like