How do I apply impulse on button click

1 favourites
  • 4 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

  • 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?

  • ... 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?

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