Player Movement with Velocity

Monday, November 9th, 2009 | Game Examples AS3, Space Shooter

Expending on the previous space shooter example, I’ve added velocity to player movement.

Here’s the updated code:

?View Code ACTIONSCRIPT
/**
* Player Movement with velocity
*
* Version: 1.0
* Author: Philip Radvan
* URL: http://www.freeactionscript.com
*/
 
package code
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
 
	public class GameEngine extends MovieClip
	{
		// Movement Flags
		public var up:Boolean = false;
		public var down:Boolean = false;
		public var left:Boolean = false;
		public var right:Boolean = false;
 
		// Movement Settings
		public var thrust:Number = .5;
		public var decay:Number = .97;
		public var maxSpeed:Number = 10;
		public var speedX:Number = 0;
		public var speedY:Number = 0;
		public var speed:Number = 0;
		public var rotationSpeed:Number = 5;
 
		/**
		 * Constructor
		 */
		public function GameEngine()
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN, myOnPress);
			stage.addEventListener(KeyboardEvent.KEY_UP, myOnRelease);
			stage.addEventListener(Event.ENTER_FRAME, myOnEnterFrame);
		}
 
		/**
		 * Main Game Loop
		 */
		public function myOnEnterFrame(event:Event):void
		{
			// Rotate right or left
			if( right ) {
				player.rotation += rotationSpeed;
			}
			if( left ) {
				player.rotation -= rotationSpeed;
			}
			if( up ) {
				// Calculate speed and trajectory based on rotation
				speedX += thrust*Math.sin(player.rotation*(Math.PI/180));
				speedY += thrust*Math.cos(player.rotation*(Math.PI/180));
				player.flames.visible = true;
			}
			else{
				// Deccelerate when Up Arrow key is released
				speedX *= decay;
				speedY *= decay;
				player.flames.visible = false;
			}
 
			// Maintain speed limit
			speed = Math.sqrt((speedX*speedX)+(speedY*speedY));
			if( speed > maxSpeed ){
				speedX *= maxSpeed/speed;
				speedY *= maxSpeed/speed;
			}
 
			// Move player based on calculations above
			player.y -= speedY;
			player.x += speedX;
 
			// Loop to opposite side of the map
			// when player travels off-screen
			if( player.y < map.y ){
				player.y = map.height;
			}
			if( player.y > map.height ){
				player.y = map.y;
			}
			if( player.x < map.x ){
				player.x = map.width;
			}
			if( player.x > map.width ){
				player.x = map.x;
			}
		}
 
		/**
		 * Keyboard Handlers
		 */
		public function myOnPress(event:KeyboardEvent):void
		{
			switch( event.keyCode )
			{
				case Keyboard.UP:
					up = true;
					break;
 
				case Keyboard.DOWN:
					down = true;
					break;
 
				case Keyboard.LEFT:
					left = true;
					break;
 
				case Keyboard.RIGHT:
					right = true;
					break;
			}
		}
 
		public function myOnRelease(event:KeyboardEvent):void
		{
			switch( event.keyCode )
			{
				case Keyboard.UP:
					up = false;
					break;
 
				case Keyboard.DOWN:
					down = false;
					break;
 
				case Keyboard.LEFT:
					left = false;
					break;
 
				case Keyboard.RIGHT:
					right = false;
					break;
			}
		}
 
	}// end of class
 
}// end of package
Download Fla Sample

Download Fla Sample


Tags: , , ,

5 Comments to Player Movement with Velocity

John
November 9, 2009

This is a wonderful resource and i am happy to see that you started to convert your as2 projects to as3,finally! Thanks.

StapledPuppet
November 12, 2009

Wow, this is a really helpful piece of actionScript, I will definitely be using it soon. The problem is I recently changed to Windows 7 so I haven’t got flash at the moment :D

But I look forward to more great script nonetheless!

MrMobz
June 22, 2010

Hi there, I’ve been trying to replicate this type of code in my own game. I’m having a slight problem however.

I’ve basically used all the same variables and codes for my acceleration with velocity. Problem is, there is no velocity. I can change my “Thrust” to any number, and my MaxSpeed to any number, it seems as though the only influencing factor is the “Thrust” variable.

Is there any reason why this is happening? I’d really like to get my acceleration working and this is the simplest method I’ve found.

Thanks.

pradvan
June 22, 2010

Hi MrMobz,
I’m not sure that I understand your problem. Max speed is working, as well as acceleration (thrust). Velocity is measured by the ’speed’ variable.
What seems to be the problem?
-PR

MrMobz
July 12, 2010

Hey I’ve managed to figure it out.
I accidentally put my variables inside my onTick function. So every time it ticks, my variables don’t get a chance to be affected by the changes.

Thanks anyways :)

Leave a comment

You must be logged in to post a comment.