Guest post by Rasmus Wriedt Larsen.

Diagonal movement has often been requested for the Avoider Game tutorial, so here is a quick guide to adding it.

If you haven’t read my Character Movement tutorial yet, do so here. Second of all, you should have read the base tutorials by Michael James Williams, do that here. Then I will show you to implement the movement to the Avoider Game.

Open up AvoiderGame.as, and instead of this:

?View Code ACTIONSCRIPT3
				if ( downKeyIsBeingPressed )
				{
					avatar.moveABit( 0, 1 );
				}
				else if ( upKeyIsBeingPressed )
				{
					avatar.moveABit( 0, -1 );
				}
				else if ( leftKeyIsBeingPressed )
				{
					avatar.moveABit( -1, 0 );
				}
				else if ( rightKeyIsBeingPressed )
				{
					avatar.moveABit( 1, 0 );
				}

Write this:

?View Code ACTIONSCRIPT3
				var xSpeed:Number = 0;
				var ySpeed:Number = 0;
 
				if ( downKeyIsBeingPressed )
				{
					ySpeed++;
				}
				if ( upKeyIsBeingPressed )
				{
					ySpeed--;
				}
				if ( leftKeyIsBeingPressed )
				{
					xSpeed--;
				}
				if ( rightKeyIsBeingPressed )
				{
					xSpeed++;
				}
				if(xSpeed!=0 && ySpeed!=0)
				{
					xSpeed = (xSpeed * Math.sqrt(2))/2;
					ySpeed = (ySpeed * Math.sqrt(2))/2;
				}
				avatar.moveABit(xSpeed, ySpeed);

And that’s that – very easy. Check it out here (click it to play):

And you can grab the files in a zip here: ZIP LINK

13 Responses to “AS3: Diagonal Movement in the Avoider Game”

  1. MichaelJW says:

    Thanks for writing this, Rasmus :)

    It’s funny what a difference diagonal movement makes, isn’t it? I was surprised when I first played yours; it’s a much more fluid control system than I expected.

  2. Mushyrulez says:

    :P Much shorter than my diagonal movement – all of those if ( downKeyIsBeingPressed && ctrlKeyIsBeingPressed && rightKeyIsBeingPressed ) seriously slowed down my game.

    Nice tutorial!

  3. [...] You’ve got four-way movement sorted; now how about eight-way movement? Hint: you can check if ( ( downKeyIsPressed ) && ( leftKeyIsPressed ) ). Be careful — the Pythagorean Theorem tells us that if you move three pixels down and three pixels left within a single tick, you’ll travel further (and therefore faster) than if you just move three pixels down or three pixels left. How can you get around that? UPDATE: Rasmus Wriedt Larsen wrote a tutorial on doing exactly this. Check it out here. [...]

  4. [...] regular commenter Rasmus Wreidt Larsen has stepped in, and written a tutorial explaining how to add such movement to your [...]

  5. Kevin says:

    Hi, I attempted to add the code, and I got syntax errors. Is && supposed to be removed? I tried if(xSpeed!=0 & ySpeed!=0) and it gave the error “1067: Implicit coercion of a value of type Boolean to an unrelated type Number.”

  6. Kevin says:

    Fixed it, I was confused by the “amp” and “amp” in the code, I’m not familiar with what it means, I caught the problem when I typed “amp” in the last msg sent,. and saw it vanish with only && left behind.

  7. MichaelJW says:

    Whoops! WordPress turned it into & without me noticing. Fixed now, thanks for pointing it out.

  8. Hi Kevin, good that you worked it out – and also that you pointed it out. WordPress got a funny way of managing symbols.

  9. [...] really important — the rest of this tutorial is based on it! (Same with Rasmus’s Diagonal Character Movement [...]

  10. DudexD says:

    You should explain the code too, so that we noobs know exactly what we are doing :P

  11. Hi there Dude :)

    There is actually a link to my blog post at the top of the page, but I can just paste it here:
    http://gamedev.rasmuswriedtlarsen.com/2009/06/09/8-way-character-movement/

    If this doesn’t explain it enough, please leave a comment on that post :)

  12. DudexD says:

    Aha I missed that :P

  13. [...] avere problemi: elaborando i due movimenti contemporaneamente andremmo più lontano del previsto. Rasmus Wriedt Larsen ha scritto un articolo decisamente completo sull’argomento. E’ in inglese e per ora così rimane, magari in futuro lo [...]

Leave a Reply