How to make actor move/change direction with a directional flick/swipe of the finger

I can make my actor move with the keyboard and with an onscreen joystick but I'm trying to move my actor and keep it moving with when I flick/swipe my finger in a specific direction, when I flick/swipe again it should change direction in the direction of the flick/swipe and continue moving. Perhaps link to a tutorial or demo?

Thanks

Comments

  • SocksSocks London, UK.Member Posts: 12,822
    edited September 2014

    @revoltandreverb said:
    I can make my actor move with the keyboard and with an onscreen joystick but I'm trying to move my actor and keep it moving with when I flick/swipe my finger in a specific direction, when I flick/swipe again it should change direction in the direction of the flick/swipe and continue moving. Perhaps link to a tutorial or demo?

    Thanks

    The simplest way would be to do it like this . . . . . my example is for a simple left/right swipe, but you could of course add up and down, or any direction you wanted, you can even base the direction of movement on the direction of the swipe . . . but to illustrate the idea I'll stick to left/right for now . . . .

    . . . . . . . .

    Make a game attribute - let's call it 'direction'.

    . . . . . . . .

    Make a swipe detector actor - place it in the scene to it covers the whole frame - make it invisible if you want.

    In the swipe detector actor - make two attributes - 'First' and 'Second' - these will record where you first touch the screen, and then where you finger is a fraction of a second later (and from this we can tell which direction your finger is swiping in).

    In the swipe detector make a rule:

    When touch is pressed - change attribute 'First' to mouse postionX

    -- Timer, After 0.01 seconds - change attribute 'Second' to mouse postionX

    . . . . . . . .

    Right, now we have where you first touched the screen, and where your finger was a fraction of second later recorded into these two attributes.

    So . . . if 'First' is bigger than ''Second' you must be swiping left . . . and if 'First' is smaller than ''Second' you must be swiping right.

    . . . . . . . .

    So stick in a couple of rules to use this information:

    When First is bigger than Second then change game.Direction to -1

    When First is smaller than Second then change game.Direction to 1

    . . . . . . . .

    Now your actor can use access direction information via the game.Direction attribute . . . for example using the Move behaviour, with the speed set to game.Direction *200

    Hope that makes sense.

    I'm make a quick demo . . . .

  • SocksSocks London, UK.Member Posts: 12,822
    edited September 2014
  • revoltandreverbrevoltandreverb Member Posts: 159
    edited September 2014

    Brilliant! Thanks for explaining and not just a demo, I love to see what things work the way they do.

    Would it be possible to detect the angle of the first and second touch so that the actor can move in angles and not just left/right/up/down?

    Thanks

  • SocksSocks London, UK.Member Posts: 12,822

    @revoltandreverb said:
    Brilliant! Thanks for explaining and not just a demo, I love to see what things work the way they do.

    Would it be possible to detect the angle of the first and second touch so that the actor can move in angles and not just left/right/up/down?

    Thanks

    Sure, just use vector-to-angle to find the angle between the First and Second touch values.

    Hold on, demo time . . . .

  • SocksSocks London, UK.Member Posts: 12,822

    Here are the basics, it needs a little tuning but gives you the basic idea:

    https://www.mediafire.com/?ntt1nkcyru4wi9x

  • SocksSocks London, UK.Member Posts: 12,822

    Same deal as above, but the speed of your swipe effects the actor:

    https://www.mediafire.com/?67jj7e8d13vbce1

  • revoltandreverbrevoltandreverb Member Posts: 159

    @Socks said:
    Here are the basics, it needs a little tuning but gives you the basic idea:

    https://www.mediafire.com/?ntt1nkcyru4wi9x

    Insane! The basic one doesn't move in angles at all, did you perhaps upload the wrong one? The speed one work perfectly, I just want to see one without speed so I can try and see what you've done differently.

  • SocksSocks London, UK.Member Posts: 12,822

    @revoltandreverb said:
    Insane! The basic one doesn't move in angles at all, did you perhaps upload the wrong one?

    Whoops ! Yes, you are right, I uploaded the wrong one !! :blush: Hold on, let me dig out the right one . . . . .

  • SocksSocks London, UK.Member Posts: 12,822

    @revoltandreverb said:
    Insane! The basic one doesn't move in angles at all, did you perhaps upload the wrong one? The speed one work perfectly, I just want to see one without speed so I can try and see what you've done differently.

    Actually it might be more useful to explain, and I enjoy typing words on a keyboard :smiley:

    . . . . . . . . . . . . . . .

    Basically take the second version - the one with speed detection . . . .

    https://www.mediafire.com/?67jj7e8d13vbce1

    . . . then in the swipe detector actor switch off the first change attribute behaviour inside the timer (the change attribute game.mag one).

    . . . then in the main actor (Mr Yellow) change the acceleration to 400 (or whatever you want). Or if you are using the Move behaviour (the lower one, currently switched off) then change the speed in that one to 400 or whatever.

    . . . . . . . . . . . . . . .

    Right, now it works as before but without the speed detection - one note, in the main actor's attributes I have set the Max Speed to 250, you might want to change that.

    . . . . . . . . . . . . . . .

    Basically the magnitude behaviour (which you just switched off in the swipe detector actor) measures the distance between two points . . . . so we record the first touch position (into self.x and self.y), then 0.1 seconds later we record the current mouse (or finger) position - and use magnitude to measure the distance between them, and seeing as the time between these two points is always 0.1 seconds we know that larger distances mean a faster swipe (you need to swipe faster to travel further before the 0.1 seconds is up) . . . . we then use that information to control the speed of the actor.

  • revoltandreverbrevoltandreverb Member Posts: 159
    edited September 2014
Sign In or Register to comment.