Free ActionScript
Home
AS3 Scripts
AS2 Scripts
Resources
About
Contact
AS3 update to my AS2 projectile weapon script . Features click-to-shoot (machine gun), collision detection, bullet gravity.
Available Settings
Bullet Speed – how fast the bullet travels
Max Distance – how far the bullet can go before dying
Reload Speed – how long it takes to reload the weapon
Barrel Length – how far from player does the bullet starts
Bullet Spread – how accurate does the bullet fly towards target
Gravity – gravity on/off
Preview
Download Fla Sample
Update Advanced multi-barrel OOP version of this weapon script is now available here: http://www.freeactionscript.com/2011/08/advanced-weapon-system/
This is an AS3 update to my old AS2 laser script with a few improvements:
You can now easily check if the laser is hitting multiple walls by adding movieclips to an array
I also added a delayed turret rotation – this way the turret doesn’t feel “glued” to the mouse
Optimized and cleaned up the code
You can set the following weapon settings:
turret rotation speed – how fast turret rotates towards mouse. default is 20
barrel length – how far from turret laser starts drawing. default is 15
laser range – default is 100
laser color – default is green #00FF00
laser width – default is 2
laser blur strength – default is 2
New enemy AI patrol script. Movieclip follows a predefined set of point markers stored in an array.
Features
Accelerate/Decelerate
Smooth movement & rotation
Bounce off walls on collision
Go to next point once destination is reached
Easily add/remove destinations
Brand new enemy AI game engine based on my previous smooth follow example.
Change direction once destination is reached
/** * Enemy AI - Random movement * --------------------- * VERSION: 1.0 * DATE: 1/25/2011 * AS3 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com **/ package { import flash. display . MovieClip ; import flash. events . Event ; import flash. events . MouseEvent ; public class Main extends MovieClip { // player settings private var _moveSpeedMax: Number = 1000 ; private var _rotateSpeedMax: Number = 15 ; private var _decay: Number = .98; private var _destinationX: int = 150 ; private var _destinationY: int = 150 ; private var _minX: Number = 0 ; private var _minY: Number = 0 ; private var _maxX: Number = 550 ; private var _maxY: Number = 400 ; // player private var _player: MovieClip ; // global private var _dx: Number = 0 ; private var _dy: Number = 0 ; private var _vx: Number = 0 ; private var _vy: Number = 0 ; private var _trueRotation: Number = 0 ; /** * Constructor */ public function Main ( ) { // create player object createPlayer ( ) ; // add listeners stage . addEventListener ( Event. ENTER_FRAME , enterFrameHandler ) ; } /** * Creates player */ private function createPlayer ( ) : void { _player = new Player ( ) ; _player. x = stage . stageWidth / 2 ; _player. y = stage . stageHeight / 2 ; stage . addChild ( _player ) ; } /** * EnterFrame Handlers */ private function enterFrameHandler ( event:Event ) : void { updateCollision ( ) ; updatePosition ( ) ; updateRotation ( ) ; } /** * Calculate Rotation */ private function updateRotation ( ) : void { // calculate rotation _dx = _player. x - _destinationX; _dy = _player. y - _destinationY; // which way to rotate var rotateTo: Number = getDegrees ( getRadians ( _dx, _dy ) ) ; // keep rotation positive, between 0 and 360 degrees if ( rotateTo > _player. rotation + 180 ) rotateTo -= 360 ; if ( rotateTo < _player. rotation - 180 ) rotateTo += 360 ; // ease rotation _trueRotation = ( rotateTo - _player. rotation ) / _rotateSpeedMax; // update rotation _player. rotation += _trueRotation; } /** * Calculate Position */ private function updatePosition ( ) : void { // update velocity _vx += ( _destinationX - _player. x ) / _moveSpeedMax; _vy += ( _destinationY - _player. y ) / _moveSpeedMax; // if close to target if ( getDistance ( _dx, _dy ) < 50 ) { getRandomDestination ( ) ; } // apply decay (drag) _vx * = _decay; _vy * = _decay; // update position _player. x += _vx; _player. y += _vy; } /** * updateCollision */ protected function updateCollision ( ) : void { // Check X // Check if hit top if ( ( ( _player. x - _player. width / 2 ) < _minX ) && ( _vx < 0 ) ) { _vx = -_vx; } // Check if hit bottom if ( ( _player. x + _player. width / 2 ) > _maxX && ( _vx > 0 ) ) { _vx = -_vx; } // Check Y // Check if hit left side if ( ( ( _player. y - _player. height / 2 ) < _minY ) && ( _vy < 0 ) ) { _vy = -_vy } // Check if hit right side if ( ( ( _player. y + _player. height / 2 ) > _maxY ) && ( _vy > 0 ) ) { _vy = -_vy; } } /** * Calculates a random destination based on stage size */ private function getRandomDestination ( ) : void { _destinationX = Math . random ( ) * ( _maxX - _player. width ) + _player. width / 2 ; _destinationY = Math . random ( ) * ( _maxY - _player. height ) + _player. height / 2 ; } /** * Get distance * @param delta_x * @param delta_y * @return */ public function getDistance ( delta_x: Number , delta_y: Number ) : Number { return Math . sqrt ( ( delta_x * delta_x ) + ( delta_y * delta_y ) ) ; } /** * Get radians * @param delta_x * @param delta_y * @return */ public function getRadians ( delta_x: Number , delta_y: Number ) : Number { var r: Number = Math . atan2 ( delta_y, delta_x ) ; if ( delta_y < 0 ) { r += ( 2 * Math . PI ) ; } return r; } /** * Get degrees * @param radians * @return */ public function getDegrees ( radians: Number ) : Number { return Math . floor ( radians / ( Math . PI / 180 ) ) ; } } }
Page 3 of 7
AS3 Effects
AS3 Gadgets
Game Examples AS2 Effects Animated Effects Dynamic Enemy AI Game Engine Game Weapons Images Interface Math Security
Effects Animated
Effects Dynamic
Enemy AI
Game Engine
Game Weapons
Images
Interface
Math
Security
Game Examples AS3 Basics Bitmap Blitting Collision Detection Enemy AI Movement Physics Space Shooter User Interface Weapon System
Basics
Bitmap Blitting
Collision Detection
Movement
Physics
Space Shooter
User Interface
Weapon System
Site Updates
718Hosting.com
Actionscript.org Forums
Flash Game Blogs
Flash Game License
Flashkit Forums
gotoAndLearn
IndieGames.com
Kirupa’s Tutorials
Kongregate Forums
MochiAds Resources
Philip Radvan
Photon Storm
Polygonal
Radvan Media
Tonypa’s Tutorials
Register
Log in
Entries RSS
Comments RSS
WordPress.org