Free ActionScript

Home

AS3 Scripts

AS2 Scripts

Resources

About

Contact

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

Here’s the updated code:

/** * 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 & gt; 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 & lt; map. y ) { player. y = map. height ; } if ( player. y & gt; map. height ) { player. y = map. y ; } if ( player. x & lt; map. x ) { player. x = map. width ; } if ( player. x & gt; 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

Optimized version of this script is available here: 8-way keyboard player movement with rotation and velocity .

John November 9, 2009 at 7:02 pm This is a wonderful resource and i am happy to see that you started to convert your as2 projects to as3,finally! Thanks. Log in to Reply

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 at 5:08 pm 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 But I look forward to more great script nonetheless! Log in to Reply

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

But I look forward to more great script nonetheless!

MrMobz June 22, 2010 at 2:26 am 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. Log in to Reply

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 at 9:39 am 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 Log in to Reply

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 at 1:48 am 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 Log in to Reply

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

You must be logged in to post a comment.

Recent Posts ActionScript Math and Trigonometry Simple Physics with Friction & Gravity Simple Physics with Friction Simple Physics FreeActionScript’s New Look

ActionScript Math and Trigonometry

Simple Physics with Friction & Gravity

Simple Physics with Friction

Simple Physics

FreeActionScript’s New Look

Search by Tags! ActionScript ActionScript 3 actionscript effects ActionScript Enemy AI actionscript game effect actionscript game engine Actionscrtipt Game Weapons actionsctipt game libarary as2 AS2 effects AS2 Enemy Movement AS2 Game Engine as2 math as3 AS3 Effects as3 math AS3 player movement BitmapData examples character control drag drop snap Actionscript dynamic flash effects dynamic mouse trailer Dynamic particles enemy ai for flash explosion effect Flash AS2 Dynamic Effects Flash AS2 Effects Flash AS2 Game Scripts flash effects Flash Game AI flash game effects Flash Game Engine flash math flash smoke effect Flash Special Effects free flash game source files free shooter game source fla Game Examples AS2 game math Math mouse follow effect particle explosion Realistic Smoke Effect space shooter actionscript trails in Flash actionscript 2

Archives December 2010 November 2010 September 2010 August 2010 June 2010 April 2010 March 2010 February 2010 November 2009 October 2009 September 2009 August 2009 July 2009 June 2009 May 2009 April 2009 March 2009 February 2009 January 2009 December 2008 November 2008 October 2008 August 2008

December 2010

November 2010

September 2010

August 2010

June 2010

April 2010

March 2010

February 2010

November 2009

October 2009

September 2009

August 2009

July 2009

June 2009

May 2009

April 2009

March 2009

February 2009

January 2009

December 2008

November 2008

October 2008

August 2008

Links Philip Radvan Flashkit Forums Kirupa’s Tutorials Tonypa’s Tutorials Actionscript.org Forums gotoAndLearn 8bitrocket Flash Game Blogs MochiAds Resources Flash Game License Template Gal Meta Register Log in

Philip Radvan

Flashkit Forums

Kirupa’s Tutorials

Tonypa’s Tutorials

Actionscript.org Forums

gotoAndLearn

8bitrocket

Flash Game Blogs

MochiAds Resources

Flash Game License

Template Gal

Register

Log in