0

I have a little question about framer. Currently I have a little privat project in which I need to run different actions when the mouse is pressed or not.

To make it easier to talk about, I set up a simple scene with a rectangle. Now I want that the rectangle rotates to the right when the mouse is pressed and rotates to the left when the mouse isn't pressed.

layer = new Layer
    x: Align.center
    y: Align.center

layer.onMouseDown ->
    layer.rotation += 1

layer.onMouseUp ->
    layer.rotation -= 1

The problem is that the code only checks once if the mouse is pressed or not. I don't know how I let the program check continuously if the button is pressed or not.

Could anyone help me please?

sln
  • 83
  • 1
  • 2
  • 9

1 Answers1

0

If you want something to happen repeatedly without events being fired (because mouseUp and mouseDown only get fired once per mouseclick), you can use intervals. Here's an example that does what you describe:

intervalDown = null
intervalUp = null

layer.onMouseDown ->
    window.clearInterval(intervalUp)
    intervalDown = Utils.interval 0.1, ->
        layer.rotation += 1

layer.onMouseUp ->
    window.clearInterval(intervalDown)
    intervalUp = Utils.interval 0.1, ->
        layer.rotation -= 1

You need to create an interval on every event and clear it again using window.clearInterval when the other event fires.

Here's the full example: https://framer.cloud/SEYHy

Niels
  • 771
  • 5
  • 5