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

Read all posts about "" game

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers