IDAT102 – Flash Game – Movement 2 – 0007

While testing the player movement system I discovered that the player, when moving diagonally, exeeded the set speed. This was due to Pythagoras’ theorem. For example, if the horizontal and vertical speed was 10 then the diagonal speed would be 14.14, the square root of 10 squared plus 10 squared.

To resolve this I created 2 new variables, startSpeed to contain the desired speed and diagSpeed to contain the calculated speed that will result in the startSpeed when moving diagonally.

var startSpeed = 15;
var speed = startSpeed;
var diagSpeed = (Math.sqrt((startSpeed * startSpeed)*2))/2;

Into the masterLoop I added:

if ((leftIsPressed && upIsPressed)
(leftIsPressed && downIsPressed)
(rightIsPressed && upIsPressed)
(rightIsPressed && downIsPressed))
{speed = diagSpeed;}else{speed = startSpeed}

It detects if the player is moving diagonally and changes the speed to diagSpeed, if the player isn’t then it stays at or reverts to the startSpeed.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.