Control your HTML5 game character like in vvvvvv mobile game with Construct2

As you should know if you love to play retro games on your mobile devices, the mobile version of vvvvvv game is on the shelves and I love the way you control the character.

It’s nothing new, but I love how this perfectly fits in that kind of game. You move the character left and right by continuously dragging or dragging and holding your finger on the device, and I have to say it works way better than the old virtual pad because you can virtually use any spot on the device to move and, above all, there isn’t any “origin” you have to cross to change direction: no matter how far are you moving your finger from the point you start touching, once you move the finger in the opposite direction, the character will walk in the other direction.

I liked it, so I wanted to show you how to create this kind of control using Scirra’s Construct2.

Obviously this will applied in a “real-life” example during next days, but at the moment I want to focus on movement.

This is what you are going to do:

Click and drag left and right with the mouse to see the direction you are the moving the character onto.

It also features touch control, you can test it on your mobile device pointing directly at this link:

http://www.emanueleferonato.com/wp-content/uploads/2014/06/vvvcontrols/

Or accessing at this link through this QR Code.

Now, let’s have a look at what I did and comment it a bit:

First, a closer look at the variables:

walkingDir: a variable storing the walking direction, which could have three values: “left”, “right” or “stop”. We start with “stop” as the player is not walking at the beginning of the game. It could also have been “0”, “1”, “2” but it’s a bit easier to explain if I use “stop”, “left” and “right”

minMovement: this is a value, in pixels, which represents the minimum movement required in order to say “the player moved the mouse/finger”. It’s used to prevent unwanted movements.

followMovement: a Boolean variable – actually an integer because Construct2 does not allow to create global Boolean variables – to see if we have to follow mouse/finger movement or not.

detectedX: variable to keep track of the latest mouse/finger x position

deltaX: variable to store the difference between detectedX and current mouse/finger x position.

Now, let’s see the eight events used to handle this movment control:

Event 1: At every tick (every frame) the text printed on screen is updated with the content of walkingDir variable.

Event 2: When the player touches the screen and we aren’t already tracking movement, set followMovement to 1 to say we are tracking movement

Event 3: When the player stops touching the screen, walkingDir is set to “stop” again, and we aren’t tracking the movement anymore.

Event 4: Every time followMovement is set to 1, we first determine deltaX as the difference between the last known mouse/finger position and the current.

Event 5: If deltaX is not zero, that means the mouse/finger moved…

Event 6: If we moved enough pixels to the right, set walkingDir to “right”

Event 7: If we moved enough pixels to the left, set walkingDir to “left”

Event 8: Update detectedX with current mouse/finger position.

That’s all at the moment, next time I will show you how to apply this concept in a real vvvvvv game prototype, meanwhile download the source code.

  • MC

    That would be a “Swipe”?

    and how do you avoid to jump (or change gravity) if you just want to move?
    or how do you jump when you want to?

  • Wow! Tried your touch control on my Android tablet and it really works great! Have never thought it’s possible to achieve it using HTML5

  • Great. tks for share

  • Hi . very nice for share this article , good luck Emanuele

  • Great tutorial and saved me time figuring it out, not sure if this will be any use to anyone, but quickly made a version that does up and down and left and right.

    https://www.dropbox.com/s/r9f3f8mffkyxi3y/Swipe-direction.capx?dl=0

    I’m not sure how complicated it would be to add diagonal control in there too.