# How do I apply impulse on button click

Just started using Construct 2? Post your questions here

### » Thu Mar 17, 2016 3:00 am

For a mobile platform, how do I make it when I touch a sprite (button) it applies an impulse to an object making it move a certain speed, then wait a couple seconds, then impulse in the opposite direction to make the object stop. I want it to have a ramping acceleration and deceleration.
B
9
S
1
Posts: 49
Reputation: 551

### » Wed Mar 23, 2016 3:07 pm

First of all make sure you have Touch added (do so in layout view).

Then do something like:
On Object Tap
Apply Impulse
Wait
Apply Impulse in opposite direction

If you are using Physics it will take some playing with to get it how you want. You mentioned impulse but if you want a gradual acceleration/deceleration you should use force. Impulse is like a ball hitting a bat or an explosion; Force is like a car/bicycle/jetpack accelerating.

If I am picturing correctly in my head how you might want it to look try this:
Add a instance variable to the object (numerical). I will refer to this variable as state but call it what you want.
For our reference:
0: inactive
1: accelerating
2: float
3: decelerating

On touch if state = 0 then change state to 1

If state = 1
If velocity < (desired velocity here) then apply force
If velocity >= (desired velocity) then state = 2
If state = 2
Trigger only once -> Wait desired float time then state = 3
If state = 3
If velocity > 0 Then apply force in opposite direction or some other method to slow the object
If velocity = 0 (or less than 0 if gravity taking hold) Then state = 0 (thus being ready for the touch even again)

That's just off the top of my head so there might be a flaw in there but give it a try and let me know how it works
B
46
S
27
G
11
Posts: 528
Reputation: 10,680

### » Sun Mar 27, 2016 5:39 pm

I will try this out tonight. I am working on an advanced asteroids based game, originally was going to use a stationary ship, but decided for the gameplay factor I am going to go with a movable ship like the original. Will let you know after I attempt it if it works or if I need more advice. Thanks! Also, while I have you here, Unlike the old asteroids where the ship would traverse the level when you go off the screen, I'd like to make the screen a border so the ship cannot leave it (but the asteroids can leave it), any good advice on the best way to do that?
B
9
S
1
Posts: 49
Reputation: 551

### » Sun Mar 27, 2016 8:31 pm

Prowl wrote:... Unlike the old asteroids where the ship would traverse the level when you go off the screen, I'd like to make the screen a border so the ship cannot leave it (but the asteroids can leave it), any good advice on the best way to do that?

Isn't Bound To Layout behavior exactly what you are wanting?
B
46
S
27
G
11
Posts: 528
Reputation: 10,680