Well, definitely you can add timers using the default Javascript setTimeout function.
But before doing that, from your code I see that on the press method you send the entity move in the negative Y. That means that if I click and hold the button pressed for a long time that entity will travel out of place as you say.
Normally you will execute code that runs only once in your release method.
Also, maybe you don’t want to translate the position, but instead might want to set the position. In this case, you might have an event that moves the button down on “press” and then restores it on “release” using “setPosition()”. So you’d have two events, this.pressed and this.release. The user would be unable to press again until he releases and that wouldn’t matter anyway because multiple setPosition()s would just put the object in the same position over and over again.
So, what I’ve said is avoid using the press function, instead use the release event callback. Because that is executed only once.
Now, after you do that, your code to be debugged requires some context on where it is running (what is the entity, how much is the displacement). Try creating a simple sample project that runs that code so we can debug it there.
Well yes, since your are setting your position using a formula that changes position.y by this.diplacement every time the function is called. So, if you have a continual stream of that function call happening (user spamming the button) where you subtract a value with each call, then, yes, the button will push away.
What I had in mind was that you would set a fixed value or a value that was pre-calculated outside your .press function. If I wanted to use this.diplacement as a universal offset that is used for a large number of buttons, then I’d probably parent the buttons to an empty entity (nullobject in animator speak) with no scaling or offset. I would use this empty entity for overall positioning of the button and then directly change the button position like this:
this.entity.setPosition(0, this.diplacement, 0);
You would reset it back to its normal position on release like this:
this.entity.setPosition(0, 0, 0);
To be clear, I’m not saying that this is the right, good, elegant or better way to do it. It is just “a” way to do it. It’s probably more animator brain at work than coder brain.
Leonidas’ suggestion to use the release method seems simpler. But then you have to add some other code to restore the button height.